簡體   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