棱镜中嵌套类型的相应graphql-tag突变

时间:2019-10-02 07:36:18

标签: reactjs apollo-client prisma graphql-tag

我想知道如何在嵌套类型中使用mutation来应用graphql-tag,因为在棱镜中,我使用create来分配字段

我的datamodel.graphql

    type Item {
      id: ID! @id @unique
      title: String
      description: String 
      price: Int
      laptop: Laptop @relation(link: INLINE)


   }

    type Laptop {
       id: ID! @id
       brand: String

   }

我的schema.graphql

    type Mutation {
      createItem(
        title: String
        description: String
        price: Int
        laptop: LaptopCreateOneInput
     ): Item!
    }

到目前为止,我确实尝试过此方法,但没有成功

    const CREATE_ITEM_MUTATION = gql`
       mutation CREATE_ITEM_MUTATION(
         $title: String!
         $description: String!
         $price: Int!
         $laptop: LaptopCreateOneInput

      ) {
        createItem(
          title: $title
          description: $description
          price: $price
          laptop:$laptop
        ) {
           id
          }
     }
   `;

我可以像这样在graphql操场上应用突变

    mutation {
      createItem(
        title: "computer"
        description: "some computer"
        price: 1000
        laptop: { create: { brand: "some brand" } }
     ) {
      title
     description
     }
   }

1 个答案:

答案 0 :(得分:0)

我确实通过用create字更新状态来使它正常工作

state = {
  title: '',
  price: 0,
  laptop: {
    create: {
      brand: ''
    }
  },
  description: '',
};