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

时间:2019-03-25 06:50:44

标签: javascript graphql prisma

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

这是变异,我正在使用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”,未找到对象。

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

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

谢谢。

1 个答案:

答案 0 :(得分:0)

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

让我们从查看createOrganization突变开始。这是createOrganization突变的字段定义的样子-createOrganization(name: String!, staff: [employeeInput!]): Organization!,其中staff是类型employeeInput的关系字段,如下所示:

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

请注意,此处的ContactInformation字段是contactInfoInput的数组,如下所示:

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

如果我们看一下Prisma生成的架构,您会注意到,对于每个关系字段,我们都有嵌套的突变字段-createconnectupdateupsert等等,当我们称为棱柱绑定时,我们需要遵守Prisma的架构。

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

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

args.staff已作为此处的create输入正确传递,但问题出在contactInformation中的args.staff数组中。其值与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 || []
               }
            }))
        }
    })
}

相关问题