GraphQL突变不适用于RuntimeWiring.newRuntimeWiring()

时间:2018-11-03 02:56:15

标签: graphql graphql-java

我已经在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
}

通过邮递员进行的突变是:

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

请帮帮我。它没有选择Mutation Type,但是Query绝对正常。我想念什么吗?

1 个答案:

答案 0 :(得分:0)

我不确定错误指的是什么,但是在执行突变时,您确实需要包括操作(querymutation)。也就是说,您的请求应如下所示:

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

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