Search in sources :

Example 1 with ExecutionPath

use of graphql.execution.ExecutionPath 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)

Example 2 with ExecutionPath

use of graphql.execution.ExecutionPath in project graphql-java by graphql-java.

the class InstrumentationExamples method fieldValidation.

private void fieldValidation() {
    ExecutionPath fieldPath = ExecutionPath.parse("/user");
    FieldValidation fieldValidation = new SimpleFieldValidation().addRule(fieldPath, new BiFunction<FieldAndArguments, FieldValidationEnvironment, Optional<GraphQLError>>() {

        @Override
        public Optional<GraphQLError> apply(FieldAndArguments fieldAndArguments, FieldValidationEnvironment environment) {
            String nameArg = fieldAndArguments.getArgumentValue("name");
            if (nameArg.length() > 255) {
                return Optional.of(environment.mkError("Invalid user name", fieldAndArguments));
            }
            return Optional.empty();
        }
    });
    FieldValidationInstrumentation instrumentation = new FieldValidationInstrumentation(fieldValidation);
    GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();
}
Also used : FieldValidationInstrumentation(graphql.execution.instrumentation.fieldvalidation.FieldValidationInstrumentation) FieldValidationEnvironment(graphql.execution.instrumentation.fieldvalidation.FieldValidationEnvironment) Optional(java.util.Optional) SimpleFieldValidation(graphql.execution.instrumentation.fieldvalidation.SimpleFieldValidation) SimpleFieldValidation(graphql.execution.instrumentation.fieldvalidation.SimpleFieldValidation) FieldValidation(graphql.execution.instrumentation.fieldvalidation.FieldValidation) FieldAndArguments(graphql.execution.instrumentation.fieldvalidation.FieldAndArguments) ExecutionPath(graphql.execution.ExecutionPath)

Example 3 with ExecutionPath

use of graphql.execution.ExecutionPath in project graphql-java by graphql-java.

the class BatchedExecutionStrategy method executeImpl.

private void executeImpl(ExecutionContext executionContext, ExecutionStrategyParameters parameters, ExecutionNode root, ExecutionNode curNode, Queue<ExecutionNode> queueOfNodes, Iterator<String> curFieldNames, CompletableFuture<ExecutionResult> overallResult) {
    if (!curFieldNames.hasNext() && queueOfNodes.isEmpty()) {
        overallResult.complete(new ExecutionResultImpl(root.getParentResults().get(0).toObject(), executionContext.getErrors()));
        return;
    }
    if (!curFieldNames.hasNext()) {
        curNode = queueOfNodes.poll();
        curFieldNames = curNode.getFields().keySet().iterator();
    }
    String fieldName = curFieldNames.next();
    List<Field> currentField = curNode.getFields().get(fieldName);
    // 
    // once an object is resolved from a interface / union to a node with an object type, the
    // parent type info has effectively changed (it has got more specific), even though the path etc...
    // has not changed
    ExecutionTypeInfo currentParentTypeInfo = parameters.getTypeInfo();
    ExecutionTypeInfo newParentTypeInfo = newTypeInfo().type(curNode.getType()).fieldDefinition(currentParentTypeInfo.getFieldDefinition()).path(currentParentTypeInfo.getPath()).parentInfo(currentParentTypeInfo.getParentTypeInfo()).build();
    ExecutionPath fieldPath = curNode.getTypeInfo().getPath().segment(fieldName);
    GraphQLFieldDefinition fieldDefinition = getFieldDef(executionContext.getGraphQLSchema(), curNode.getType(), currentField.get(0));
    ExecutionTypeInfo typeInfo = newTypeInfo().type(fieldDefinition.getType()).fieldDefinition(fieldDefinition).path(fieldPath).parentInfo(newParentTypeInfo).build();
    ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.path(fieldPath).field(currentField).typeInfo(typeInfo));
    ExecutionNode finalCurNode = curNode;
    Iterator<String> finalCurFieldNames = curFieldNames;
    resolveField(executionContext, newParameters, fieldName, curNode).whenComplete((childNodes, exception) -> {
        if (exception != null) {
            handleNonNullException(executionContext, overallResult, exception);
            return;
        }
        queueOfNodes.addAll(childNodes);
        executeImpl(executionContext, newParameters, root, finalCurNode, queueOfNodes, finalCurFieldNames, overallResult);
    });
}
Also used : Field(graphql.language.Field) ExecutionTypeInfo(graphql.execution.ExecutionTypeInfo) ExecutionResultImpl(graphql.ExecutionResultImpl) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) ExecutionStrategyParameters(graphql.execution.ExecutionStrategyParameters) ExecutionPath(graphql.execution.ExecutionPath)

Example 4 with ExecutionPath

use of graphql.execution.ExecutionPath in project graphql-java by graphql-java.

the class BatchedExecutionStrategy method handleNonNullType.

private void handleNonNullType(ExecutionContext executionContext, FetchedValues fetchedValues) {
    ExecutionTypeInfo typeInfo = fetchedValues.getExecutionTypeInfo();
    NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, typeInfo);
    ExecutionPath path = fetchedValues.getPath();
    for (FetchedValue value : fetchedValues.getValues()) {
        nonNullableFieldValidator.validate(path, value.getValue());
    }
}
Also used : ExecutionTypeInfo(graphql.execution.ExecutionTypeInfo) NonNullableFieldValidator(graphql.execution.NonNullableFieldValidator) ExecutionPath(graphql.execution.ExecutionPath)

Example 5 with ExecutionPath

use of graphql.execution.ExecutionPath in project graphql-java by graphql-java.

the class SimpleFieldValidation method validateFields.

@Override
public List<GraphQLError> validateFields(FieldValidationEnvironment validationEnvironment) {
    List<GraphQLError> errors = new ArrayList<>();
    for (ExecutionPath fieldPath : rules.keySet()) {
        List<FieldAndArguments> fieldAndArguments = validationEnvironment.getFieldsByPath().get(fieldPath);
        if (fieldAndArguments != null) {
            BiFunction<FieldAndArguments, FieldValidationEnvironment, Optional<GraphQLError>> ruleFunction = rules.get(fieldPath);
            for (FieldAndArguments fieldAndArgument : fieldAndArguments) {
                Optional<GraphQLError> graphQLError = ruleFunction.apply(fieldAndArgument, validationEnvironment);
                graphQLError.ifPresent(errors::add);
            }
        }
    }
    return errors;
}
Also used : Optional(java.util.Optional) ArrayList(java.util.ArrayList) GraphQLError(graphql.GraphQLError) ExecutionPath(graphql.execution.ExecutionPath)

Aggregations

ExecutionPath (graphql.execution.ExecutionPath)5 ExecutionTypeInfo (graphql.execution.ExecutionTypeInfo)2 Field (graphql.language.Field)2 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 ExecutionResultImpl (graphql.ExecutionResultImpl)1 GraphQLError (graphql.GraphQLError)1 QueryTraversal (graphql.analysis.QueryTraversal)1 ExecutionStrategyParameters (graphql.execution.ExecutionStrategyParameters)1 NonNullableFieldValidator (graphql.execution.NonNullableFieldValidator)1 FieldAndArguments (graphql.execution.instrumentation.fieldvalidation.FieldAndArguments)1 FieldValidation (graphql.execution.instrumentation.fieldvalidation.FieldValidation)1 FieldValidationEnvironment (graphql.execution.instrumentation.fieldvalidation.FieldValidationEnvironment)1 FieldValidationInstrumentation (graphql.execution.instrumentation.fieldvalidation.FieldValidationInstrumentation)1 SimpleFieldValidation (graphql.execution.instrumentation.fieldvalidation.SimpleFieldValidation)1 InstrumentationExecutionStrategyParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)1 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1