Search code examples
javagraphqlgraphiql

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):

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

GraphQL server is written in Java. But I am pretty sure that the request is not event reaching it and fails on the GraphQL layer. Anyway, schema definition is pretty simple: 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!

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

Am I defining a query variable in a wrong way? Or why is not picked up by GraphiQL?

I tried using both GraphiQL interface and apollo-client to send the request with variables, but have the same error. Any ideas?


Solution

  • Yay!! The reason was that my GraphQLController class did not parse variables from the request:

    @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);
    }