首页 文章

在变异中不使用GraphQL变量

提问于
浏览
0

我的突变查询:

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

查询变量:

{ "matchId": "123" }

GraphQL架构(Mutation定义):

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

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

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

Please note that mutation assignMatch(matchId: "123") succeeds.

我是以错误的方式定义查询变量吗?或者为什么GraphiQL没有选择它?

我尝试使用GraphiQL接口和apollo-client来发送带变量的请求,但是具有相同的错误 . 有任何想法吗?

1 回答

  • 0

    好极了!!原因是我的 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);
    }
    

相关问题