我的架构文件是

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: "[email protected]",
    product: [
      {
         name: "soap",
             price: 435,
      }
    ]
  )
  {
    id
    name
    email
    product{name}

  }
}

但我越来越异常(exception)
{
  "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"
    }
  ]
}

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

最佳答案

确保您将正确类型的对象传递给您的突变。 GraphQL需要用于输入字段的单独类型。在您的架构中,产品类​​型应类似于此,并且您应相应地更改突变。

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

input ProductInput {
    name: String!
    price: Int
}

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

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

关于spring-boot - 带有突变 Spring 启动的Graphql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47266238/

10-15 14:36