【问题标题】:GraphQL Mutation not working with RuntimeWiring.newRuntimeWiring()GraphQL 突变不适用于 RuntimeWiring.newRuntimeWiring()
【发布时间】:2019-04-07 06:06:02
【问题描述】:

我在 GraphQL Java 中定义了一个简单的模式来添加 Book 并获取 [Book]。我的架构如下:

schema {
 query: Query
 mutation : Mutation
}

type Query {
 allBooks: [Book]
 book(id: String): Book
}

type Book {
  isn: String
  title: String
  publisher: String
  author: String
  publishedDate: String
}

input BookType {
  isn: String
  title: String
  publisher: String
  author: String
  publishedDate: String
}

type Mutation {
  newBook(
      isn: String!,
      title: String!,
      publisher: String!,
      authors:String!,
      publishedDate: String!
  ): Book
}

我已经创建了这样的运行时接线:

RuntimeWiring.newRuntimeWiring()
  .type("Query", typeWiring ->
    typeWiring.dataFetcher("allBooks", allBooksDataFetcher))
  .type("Mutation", typeWiring ->
    typeWiring.dataFetcher("newBook", addBooksDataFetcher))
  .build();

allBooks 查询工作正常,但 newBook 突变不工作并给出以下错误:

"errors": [
  {
    "validationErrorType": "FieldUndefined",
    "message": "Validation error of type FieldUndefined: Field oneBook is undefined",
    "locations": [
      {
        "line": 3,
        "column": 3
      }
    ],
    "errorType": "ValidationError"
  }
],
  "data": null,
  "extensions": null
}

通过 Postman 的变异是:

{
  newBook(
    isn:"1",
    title: "test title",
    publisher: "test publisher",
    authors:"test author",
    publishedDate: "2 Nov 2018"
  ) {
    title
  }
}

请帮帮我。它没有选择突变类型,但查询工作得非常好。我错过了什么吗?

【问题讨论】:

    标签: graphql graphql-java


    【解决方案1】:

    我不确定错误指的是什么,但您确实需要在执行突变时包含操作(querymutation)。也就是说,您的请求应该看起来更像这样:

    mutation ArbitraryOperationName {
      newBook(
        isn:"1",
        title: "test title",
        publisher: "test publisher",
        authors:"test author",
        publishedDate: "2 Nov 2018"
      ) {
        title
      }
    }
    

    在编写查询时,您可以使用简写符号并省略 query 关键字,但突变必须始终包含 mutation 以表明您正在进行突变而不是查询。

    【讨论】:

    • 谢谢丹尼尔,它起作用了:) 我缺少包含突变关键字。非常感谢:)
    猜你喜欢
    • 2018-11-10
    • 2017-10-18
    • 2019-10-29
    • 2019-09-23
    • 2021-02-17
    • 2017-02-17
    • 2020-02-18
    • 2018-06-20
    • 2021-11-09
    相关资源
    最近更新 更多