简体   繁体   中英

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

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 (ie query parsing result) and get its Operation type (an enum which has value QUERY , MUTATION , SUBSCRIPTION ) from OperationDefinition .

PS I test with graphql-java v17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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