Search in sources :

Example 1 with QueryVisitorStub

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

the class FieldValidationSupport method validateFieldsAndArguments.

static List<GraphQLError> validateFieldsAndArguments(FieldValidation fieldValidation, ExecutionContext executionContext) {
    Map<ResultPath, List<FieldAndArguments>> fieldArgumentsMap = new LinkedHashMap<>();
    QueryTraverser queryTraverser = QueryTraverser.newQueryTraverser().schema(executionContext.getGraphQLSchema()).document(executionContext.getDocument()).operationName(executionContext.getOperationDefinition().getName()).variables(executionContext.getVariables()).build();
    queryTraverser.visitPreOrder(new QueryVisitorStub() {

        @Override
        public void visitField(QueryVisitorFieldEnvironment env) {
            Field field = env.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(env);
                ResultPath 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 : QueryVisitorStub(graphql.analysis.QueryVisitorStub) QueryVisitorFieldEnvironment(graphql.analysis.QueryVisitorFieldEnvironment) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Field(graphql.language.Field) QueryTraverser(graphql.analysis.QueryTraverser) ResultPath(graphql.execution.ResultPath) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 QueryTraverser (graphql.analysis.QueryTraverser)1 QueryVisitorFieldEnvironment (graphql.analysis.QueryVisitorFieldEnvironment)1 QueryVisitorStub (graphql.analysis.QueryVisitorStub)1 ResultPath (graphql.execution.ResultPath)1 Field (graphql.language.Field)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1