【问题标题】:GraphQL variables are not used in the mutationGraphQL 变量未在突变中使用
【发布时间】:2018-07-27 23:58:38
【问题描述】:

我的变异查询:

mutation ($matchId: String!) {
  assignMatch(matchId: $matchId) {
    id
  }
}

查询变量:

{ "matchId": "123" }

GraphQL 架构(突变定义):

type Mutation {
    assignMatch(matchId: String!): Assignment
}

GraphQL 服务器是用 Java 编写的。但我很确定请求不是事件到达它并且在 GraphQL 层上失败。无论如何,模式定义非常简单:GraphQL graphQL = GraphQL.newGraphQL(SchemaParser.newParser().file("schema.graphqls").build().makeExecutableSchema())

结果:错误消息Variable 'matchId' has coerced Null value for NonNull type 'String!

请注意,突变assignMatch(matchId: "123") 成功。

我是否以错误的方式定义查询变量?或者为什么不被 GraphiQL 采纳?

我尝试同时使用 GraphiQL 接口和 apollo-client 来发送带有变量的请求,但出现了同样的错误。 有什么想法吗?

【问题讨论】:

    标签: java graphql graphiql


    【解决方案1】:

    耶!!原因是我的GraphQLController 类没有从请求中解析变量:

    @RequestMapping(value = "/graphql", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String execute(@RequestBody Map<String, Object> request) throws IOException {
        ExecutionInput executionInput = ExecutionInput.newExecutionInput()
                .query((String) request.get("query"))
                .operationName((String) request.get("operationName"))
                // THE FOLLOWING LINE WAS ABSENT:
                .variables((Map<String, Object>) request.get("variables"))
                .build();
        ExecutionResult executionResult = graphQL.execute(executionInput);
        return mapper.writeValueAsString(executionResult);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 1970-01-01
      • 2021-04-17
      • 2018-06-20
      • 1970-01-01
      • 2018-09-22
      • 2019-11-19
      • 2017-10-18
      • 2019-06-15
      相关资源
      最近更新 更多