繁体   English   中英

如何解决graphql突变的嵌套输入类型

[英]How to resolve nested input types on graphql mutation

因此,在尝试解决包含另一个输入类型的嵌套输入类型的突变时遇到问题,如果我对模型进行的设计错误,请纠正我的问题。

这是变异,我正在使用Playground进行检查:

mutation{
  createOrganization(
    name: "Bitas"
    staff: [
      {
        firstName: "Albert"
        lastName: "Chavez"
        position: "Developer"
        contactInformation:[
            {
                email: "hola@mail.com"
                phone:"9187631"
                linkedin: "whatever"
            },
            {
                email: "hola2@mail.com"
                phone:"91876312"
                linkedin: "whatever2"
            }
          ]
        }
    ]
  ){
    name
    staff{
      firstName
      contactInformation{
        email
      }
    }
  }
}

这种变异正在组织和雇员之间建立关系,同时也在雇员和联系信息之间建立关系...以下是模式:

type Organization {
    id: ID!
    name: String!
    staff: [Employee!]!
}

type Employee {
    id: ID!
    firstName: String!
    lastName: String!
    position: String!
    contactInformation: [ContactInfo!]!
    belongsToOrg: Organization
}

input employeeInput {
    firstName: String!
    lastName: String!
    position: String!
    contactInformation: [contactInfoInput!]!
    belongsToOrg: ID
}

type ContactInfo {
    id: ID!
    email: String!
    phone: String!
    linkedin: String!
    belongsTo: Employee!
}

input contactInfoInput {
    email: String!
    phone: String!
    linkedin: String!
}

如果我没有正确创建突变,请纠正我

type Mutation {
    createOrganization(name: String!, staff: [employeeInput!]): Organization!
    createEmployee(firstName: String!, lastName: String!, position:String!, contactInformation: [contactInfoInput!]!): Employee!
}

这是要创建的函数:

function createEmployee(parent, args, context, info) {
    return context.prisma.createEmployee({
        firstName: args.firstName,
        lastName: args.lastName,
        position: args.position,
        contactInformation: {
            create: args.contactInformation
        },
    })
}

function createOrganization(parent, args, context, info) {
    return context.prisma.createOrganization({
        name: args.name,
        staff: {
            create: args.staff
        }
    })
}

function staff(parent, args, context) {
    return context.prisma.organization({id: parent.id}).staff();
}

function contactInformation(parent, args, context) {
    return context.prisma.employee({id: parent.id}).contactInformation()
}

function belongsTo(parent, args, context) {
    return context.prisma.contactInfo({id: parent.id}).belongsTo()
}

因此,当我在Playground上找到突变时,它给了我错误:

原因:'staff.create [0] .contactInformation'预期为'ContactInfoCreateManyWithoutEmployeeInput',未找到对象。

能请有人解释一下这是什么意思吗? 我没有正确设计架构或关系吗? 也许是因为嵌套输入的层次过多? 如果我在createOrganization函数上进行console.log的contactInformation字段,则该值是不确定的。

注意: 创建Employee时,嵌套变异工作正常。

提前致谢。

问题出在传递给pyramida绑定函数的输入中。

让我们先来看一下createOrganization突变。 这就是createOrganization突变的字段定义的样子createOrganization(name: String!, staff: [employeeInput!]): Organization! 其中staffemployeeInput类型的关系字段,如下所示:

input employeeInput {
    firstName: String!
    lastName: String!
    position: String!
    contactInformation: [contactInfoInput!]!
    belongsToOrg: ID
}

请注意,这里的ContactInformation字段是contactInfoInput数组, contactInfoInput所示:

type ContactInfo {
    id: ID!
    email: String!
    phone: String!
    linkedin: String!
    belongsTo: Employee!
}

如果我们看一下由Prisma的生成的模式,你会发现,每一个关系领域,我们嵌套的突变场- createconnectupdateupsert等,当我们调用PRISMA结合,我们需要坚持Prisma的的架构。

现在,如果我们看一下解析器,

function createOrganization(parent, args, context, info) {
    return context.prisma.createOrganization({
        name: args.name,
        staff: {
            create: args.staff
        }
    })
}

args.staff在此处正确地作为create输入传递,但是问题出在args.staff中的contactInformation数组中。 它的值与Prisma模式中定义的ContactInfoCreateManyWithoutEmployeeInput类型不匹配。 将上面的解析器更改为以下将为您解决问题,但我建议更好地设计突变的输入类型:

function createOrganization(parent, args, context, info) {
    const { staff } = args
    return context.prisma.createOrganization({
        name: args.name,
        staff: {
            create: staff.map((emp) => ({
               firstName: emp.firstName,
               lastName: emp.lastName,
               position: emp.position,
               contactInformation: {
                   create: emp.contactInformation || []
               }
            }))
        }
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM