1 year ago

#380609

test-img

Ram

Get GraphQL query name from the entire query text in graphql-java

Assume that there is a query such as:

query test1 {
  students {
    id
    name
    address_pin
    address_city
    yearAdmitted
    laptopOS
    phoneOS
  }
}

I can access this entire query text at runtime in graphql-java.

Is there anyway to get the query-name 'students'?

Tried this and it works for basic queries, not sure if this is the correct way to do so.

    private String getQueryName(String input) {
    Document doc = new Parser().parseDocument(input);
    List<OperationDefinition> definitionList = doc.getDefinitionsOfType(OperationDefinition.class);
    for (OperationDefinition definition : definitionList) {
        if (definition != null && definition.getOperation().toString().equalsIgnoreCase("QUERY")) {
            Field field = definition.getSelectionSet().getSelectionsOfType(Field.class).stream().findFirst().orElse(null);
            if (field != null) {
                return field.getName();
            }
        }
    }
    return null;
}

graphql

graphql-java

0 Answers

Your Answer

Accepted video resources