Search code examples
javagraphql-java

Detect query type( query, mutation, subscription) in graphql-java


Is there a way to detect the query type query , mutation or subscription , preferably without executing the query.

For example:

ExecutionInput executionInput = ExecutionInput
    .newExecutionInput()
    .query("subscription Test{test}")
    .build();

Is it possible to detect that this is a subscription request? subscription Test{test} without executing the query?

Something like executionInput.isSubscription()


Solution

  • You can use ParseAndValidate to parse the ExecutionInput :

    ParseAndValidateResult parseResult = ParseAndValidate.parse(executionInput);
            parseResult.getDocument().getDefinitionsOfType(OperationDefinition.class)
            .forEach(d->{
                System.out.println(d.getOperation());
            });
    

    The idea is to get the list of the OperationDefinition from Document (i.e query parsing result) and get its Operation type (an enum which has value QUERY,MUTATION,SUBSCRIPTION) from OperationDefinition.

    P.S. I test with graphql-java v17