【问题标题】:Graphql with mutation spring boot带有突变弹簧靴的 Graphql
【发布时间】:2017-11-13 14:08:40
【问题描述】:

我的架构文件是

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"
    }
  ]
}

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

【问题讨论】:

  • 您是否尝试过在您的变异中获取一些产品字段,例如mutation { createCustomer(...) { id, name, email, product {name} } }
  • 它解决了这个错误。但出现新错误

标签: spring-boot graphql


【解决方案1】:

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

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

input ProductInput {
    name: String!
    price: Int
}

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

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

【讨论】:

  • 它将客户存储到数据库。但无法按产品返回产品 { name }
  • mutation Customer { createCustomer(name: "trrth", email: "night", product: [{id:"1", name: "thrh", price: 123}]) { id name电子邮件产品 { id name } } }
  • 它应该是 product {id, name} 根据您的架构。注意单个产品与产品,并在 id 和 name 之间添加逗号。
  • 别在意昏迷 :) 我看到你写得很快。您收到错误类型错误,因此请尝试在您的输入类型之外添加 type Product {...}
  • 感谢您的回答。我通过采用输入类型进行突变并返回输入类型的类型插入来解决了这个问题。所以我现在没有收到类型转换错误。
猜你喜欢
  • 2018-02-14
  • 1970-01-01
  • 2014-06-27
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 2021-02-16
  • 2018-11-22
相关资源
最近更新 更多