Graphql with mutation spring boot

时间:2017-11-13 14:08:41

标签: spring-boot graphql

我的架构文件是

type Mutation {
createCustomer(name: String!, email: String!, product: [Product]): Customer
}

input Product {
    id: ID!
    name: String!
    price: Int
}

interface Person {
    id: ID!
    name: String!
    email: String!
}

type Customer implements Person {
    id: ID!
    name: String!
    email: String!
    product: [Product] 
}

我想在此处插入客户详细信息,其中包含产品列表作为输入。我的问题是

mutation {
  createCustomer(
    name: "kitte", 
    email: "kitte@gmail.com",
    product: [
      {
         name: "soap", 
             price: 435,
      }
    ]
  ) 
  {
    id
    name
    email
    product{name}

  }
}

但我得到例外

{
  "data": null,
  "errors": [
    {
      "validationErrorType": "WrongType",
      "message": "Validation error of type WrongType: argument value ArrayValue{values=[ObjectValue{objectFields=[ObjectField{name='name', value=StringValue{value='dars76788hi'}}, ObjectField{name='price', value=IntValue{value=123}}]}, ObjectValue{objectFields=[ObjectField{name='name', value=StringValue{value='darr'}}, ObjectField{name='price', value=IntValue{value=145}}]}]} has wrong type",
      "locations": [
        {
          "line": 5,
          "column": 5
        }
      ],
      "errorType": "ValidationError"
    }
  ]
}

我不明白错误是什么。以及如何将列表传递给变异。我已经提到了一些示例,但无法将产品作为列表插入。

1 个答案:

答案 0 :(得分:7)

确保将正确类型的对象传递给突变。 GraphQL需要输入字段的单独类型。在您的模式中,产品类​​型应该是这样的,您应该相应地更改变异。

type Product {
    id: ID!
    name: String!
    price: Int
}

input ProductInput {
    name: String!
    price: Int
}

input CustomerInput {
    ...
    products: [ProductInput]
}

文档中有几个非常有用的示例,请参阅Mutations and Input Types