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);
}
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();
}
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);
});
}
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());
}
}
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;
}
Aggregations