简体   繁体   English

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

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

So I'm having issues trying to resolve a mutation that contains nested input types from another input type, correct me if I'm doing a wrong design with the models. 因此,在尝试解决包含另一个输入类型的嵌套输入类型的突变时遇到问题,如果我对模型进行的设计错误,请纠正我的问题。

This is the mutation, I'm using Playground to check it out: 这是变异,我正在使用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
      }
    }
  }
}

This mutation is creating a relationship between Organization and Employee, which at the same time is creating a relationship between Employee and Contact Information... here are the schemas: 这种变异正在组织和雇员之间建立关系,同时也在雇员和联系信息之间建立关系...以下是模式:

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!
}

Correct me if I'm not creating the mutations correctly 如果我没有正确创建突变,请纠正我

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

And here are the functions to create: 这是要创建的函数:

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()
}

So when I hit the mutation on Playground, it gives me the error: 因此,当我在Playground上找到突变时,它给了我错误:

Reason: 'staff.create[0].contactInformation' Expected 'ContactInfoCreateManyWithoutEmployeeInput', found not an object. 原因:'staff.create [0] .contactInformation'预期为'ContactInfoCreateManyWithoutEmployeeInput',未找到对象。

Could please somebody explain me what this means?? 能请有人解释一下这是什么意思吗? Am I not designing correctly the schema or relationships?? 我没有正确设计架构或关系吗? Or perhaps is because too many levels of nested inputs?? 也许是因为嵌套输入的层次过多? If I console.log the contactInformation field on the createOrganization function the value is undefined. 如果我在createOrganization函数上进行console.log的contactInformation字段,则该值是不确定的。

Note: When creating a Employee, the nested mutation works fine. 注意: 创建Employee时,嵌套变异工作正常。

Thanks in advance. 提前致谢。

The issue is in input passed to prisma binding function. 问题出在传递给pyramida绑定函数的输入中。

Let us start by looking at createOrganization mutation. 让我们先来看一下createOrganization突变。 This is how field definition for createOrganization mutation looks like - createOrganization(name: String!, staff: [employeeInput!]): Organization! 这就是createOrganization突变的字段定义的样子createOrganization(name: String!, staff: [employeeInput!]): Organization! where staff is relational field of type employeeInput which looks as below: 其中staffemployeeInput类型的关系字段,如下所示:

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

Note that ContactInformation field here is array of contactInfoInput which looks like: 请注意,这里的ContactInformation字段是contactInfoInput数组, contactInfoInput所示:

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

If we look at schema generated by Prisma, you will notice that for each relation field, we have nested mutation fields - create , connect , update , upsert etc. and when we call prisma binding, we need to adhere to Prisma's schema. 如果我们看一下由Prisma的生成的模式,你会发现,每一个关系领域,我们嵌套的突变场- createconnectupdateupsert等,当我们调用PRISMA结合,我们需要坚持Prisma的的架构。

Now, if we look at resolver, 现在,如果我们看一下解析器,

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

args.staff is correctly being passed as create input here but problem is in contactInformation array present in args.staff . args.staff在此处正确地作为create输入传递,但是问题出在args.staff中的contactInformation数组中。 Its value does not match with ContactInfoCreateManyWithoutEmployeeInput type defined in Prisma's schema. 它的值与Prisma模式中定义的ContactInfoCreateManyWithoutEmployeeInput类型不匹配。 Changing above resolver to following will fix issue for you but I would suggest better designing input types for mutations: 将上面的解析器更改为以下将为您解决问题,但我建议更好地设计突变的输入类型:

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