如何创建具有3个或更多嵌套关系的变异?

时间:2019-09-11 20:18:24

标签: laravel-lighthouse

我正在根据我研究的业务创建一个全能CRUD。问题是我尝试进行lighthouse docs中所示的嵌套变异 但由于{客户有很多联系人,而联系人有很多电子邮件}

我在schema.graphql中执行以下操作:

type Mutation {
    createClient (input: createClientInput! @spread): Client @create
}

input createClientInput {
    name: String!
    address: String!
    NIT: String
    ...    
    contracts: CreateContactsRelation
}

input CreateContactsRelation {
    create: [CreateContactInput!]
}

input CreateContactInput {
    name: String
    position: String
    emails: CreateEmailRelation # this does'n work
}

input CreateEmailRelation {
    create: [CreateEmailInput!]
}

input CreateEmailInput {
    email: String!
}

运行突变

mutation{
  crearCliente(input: {
    nombre: "prueba graphql crear 4"
    direccion: "prueba"
    NIT: "3225"
    abreviatura: "asas"
    OSDE: "prub"
    organismo: "asas"
    observaciones: "con contacto"
    contactos: {
      create: [
        {
          nombre: "periqito peres"
          cargo: "director"
          correos: {
            create: [
              {
                email: "perez@gmail.com"
              }
              {
                email: "perez@enet.cu"
              }
            ]
          }
        }
        {
          nombre: "joaqin gomez"
          cargo: "contador"
          correos: {
            create: [
              {
                email: "joaqin@gmail.com"
              }
              {
                email: "joaqin@enet.cu"
              }
            ]
          }
        }
      ]
    }
  }){
    id
    nombre
    contactos{
      id
      nombre
      cargo
      correos{
        id
        email
      }
    }
  }
}

输出为

{
  "data": {
    "crearCliente": {
      "id": "53",
      "nombre": "prueba graphql crear 4",
      "contactos": [
        {
          "id": "46",
          "nombre": "periqito peres",
          "cargo": "director",
          "correos": [] <--------------not email 
        },
        {
          "id": "47",
          "nombre": "joaqin gomez",
          "cargo": "contador",
          "correos": [] <--------------not email
        }
      ]
    }
  }
}

如您所见,未创建电子邮件。有人知道如何解决吗?

1 个答案:

答案 0 :(得分:0)

您必须使用id连接每个表关系,我创建了此示例以供您理解:

enter image description here

enter image description here