简体   繁体   English

GraphQL变量未在突变中使用

[英]GraphQL variables are not used in the mutation

My mutation query: 我的变异查询:

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

Query variables: 查询变量:

{ "matchId": "123" }

GraphQL schema (Mutation definition): GraphQL模式(突变定义):

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

GraphQL server is written in Java. GraphQL服务器是用Java编写的。 But I am pretty sure that the request is not event reaching it and fails on the GraphQL layer. 但是我很确定该请求不是事件到达它,并且在GraphQL层上失败。 Anyway, schema definition is pretty simple: GraphQL graphQL = GraphQL.newGraphQL(SchemaParser.newParser().file("schema.graphqls").build().makeExecutableSchema()) 无论如何,模式定义非常简单: GraphQL graphQL = GraphQL.newGraphQL(SchemaParser.newParser().file("schema.graphqls").build().makeExecutableSchema())

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

Please note that mutation assignMatch(matchId: "123") succeeds. 请注意,突变assignMatch(matchId: "123")成功。

Am I defining a query variable in a wrong way? 我是否以错误的方式定义了查询变量? Or why is not picked up by GraphiQL? 还是为什么GraphiQL不接?

I tried using both GraphiQL interface and apollo-client to send the request with variables, but have the same error. 我尝试同时使用GraphiQL接口和apollo-client发送带有变量的请求,但有相同的错误。 Any ideas? 有任何想法吗?

Yay!! 好极了!! The reason was that my GraphQLController class did not parse variables from the request: 原因是我的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM