Search in sources :

Example 1 with QueryTraversal

use of graphql.analysis.QueryTraversal in project graphql-java by graphql-java.

the class FieldValidationSupport method validateFieldsAndArguments.

static List<GraphQLError> validateFieldsAndArguments(FieldValidation fieldValidation, ExecutionContext executionContext) {
    Map<ExecutionPath, List<FieldAndArguments>> fieldArgumentsMap = new LinkedHashMap<>();
    QueryTraversal queryTraversal = new QueryTraversal(executionContext.getGraphQLSchema(), executionContext.getDocument(), executionContext.getOperationDefinition().getName(), executionContext.getVariables());
    queryTraversal.visitPreOrder(traversalEnv -> {
        Field field = traversalEnv.getField();
        if (field.getArguments() != null && !field.getArguments().isEmpty()) {
            // 
            // only fields that have arguments make any sense to placed in play
            // since only they have variable input
            FieldAndArguments fieldArguments = new FieldAndArgumentsImpl(traversalEnv);
            ExecutionPath path = fieldArguments.getPath();
            List<FieldAndArguments> list = fieldArgumentsMap.getOrDefault(path, new ArrayList<>());
            list.add(fieldArguments);
            fieldArgumentsMap.put(path, list);
        }
    });
    FieldValidationEnvironment environment = new FieldValidationEnvironmentImpl(executionContext, fieldArgumentsMap);
    // this will allow a consumer to plugin their own validation of fields and arguments
    return fieldValidation.validateFields(environment);
}
Also used : Field(graphql.language.Field) ArrayList(java.util.ArrayList) List(java.util.List) QueryTraversal(graphql.analysis.QueryTraversal) ExecutionPath(graphql.execution.ExecutionPath) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

QueryTraversal (graphql.analysis.QueryTraversal)1 ExecutionPath (graphql.execution.ExecutionPath)1 Field (graphql.language.Field)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1