use of graphql.execution.ResultPath in project graphql-java-extended-validation by graphql-java.
the class TargetedValidationRules method walkListArg.
private List<GraphQLError> walkListArg(ValidationRule rule, ValidationEnvironment validationEnvironment, GraphQLList argumentType, List<Object> objectList) {
List<GraphQLError> errors = new ArrayList<>();
GraphQLInputType listItemType = Util.unwrapOneAndAllNonNull(argumentType);
List<GraphQLDirective> directives;
if (!(listItemType instanceof GraphQLDirectiveContainer)) {
directives = Collections.emptyList();
} else {
directives = ((GraphQLDirectiveContainer) listItemType).getDirectives();
}
int ix = 0;
for (Object value : objectList) {
ResultPath newPath = validationEnvironment.getValidatedPath().segment(ix);
ValidationEnvironment newValidationEnvironment = validationEnvironment.transform(builder -> builder.validatedPath(newPath).validatedValue(value).validatedType(listItemType).directives(directives));
List<GraphQLError> ruleErrors = runValidationImpl(rule, newValidationEnvironment, listItemType, value);
errors.addAll(ruleErrors);
ix++;
}
return errors;
}
use of graphql.execution.ResultPath in project graphql-java by graphql-java.
the class ValueFetcher method fetchValue.
public CompletableFuture<FetchedValue> fetchValue(ExecutionContext executionContext, Object source, Object localContext, MergedField sameFields, ExecutionStepInfo executionInfo) {
Field field = sameFields.getSingleField();
GraphQLFieldDefinition fieldDef = executionInfo.getFieldDefinition();
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
GraphQLFieldsContainer parentType = getFieldsContainer(executionInfo);
Supplier<Map<String, Object>> argumentValues = FpKit.intraThreadMemoize(() -> valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables()));
QueryDirectivesImpl queryDirectives = new QueryDirectivesImpl(sameFields, executionContext.getGraphQLSchema(), executionContext.getVariables());
GraphQLOutputType fieldType = fieldDef.getType();
Supplier<ExecutableNormalizedOperation> normalizedQuery = executionContext.getNormalizedQueryTree();
Supplier<ExecutableNormalizedField> normalisedField = () -> normalizedQuery.get().getNormalizedField(sameFields, executionInfo.getObjectType(), executionInfo.getPath());
DataFetchingFieldSelectionSet selectionSet = DataFetchingFieldSelectionSetImpl.newCollector(executionContext.getGraphQLSchema(), fieldType, normalisedField);
DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext).source(source).localContext(localContext).arguments(argumentValues).fieldDefinition(fieldDef).mergedField(sameFields).fieldType(fieldType).executionStepInfo(executionInfo).parentType(parentType).selectionSet(selectionSet).queryDirectives(queryDirectives).build();
ExecutionId executionId = executionContext.getExecutionId();
ResultPath path = executionInfo.getPath();
return callDataFetcher(codeRegistry, parentType, fieldDef, environment, executionId, path).thenApply(rawFetchedValue -> FetchedValue.newFetchedValue().fetchedValue(rawFetchedValue).rawFetchedValue(rawFetchedValue).build()).exceptionally(exception -> handleExceptionWhileFetching(field, path, exception)).thenApply(result -> unboxPossibleDataFetcherResult(sameFields, path, result, localContext)).thenApply(this::unboxPossibleOptional);
}
use of graphql.execution.ResultPath in project graphql-java by graphql-java.
the class FieldLevelTrackingApproach method beginExecutionStrategy.
ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
CallStack callStack = parameters.getInstrumentationState();
ResultPath path = parameters.getExecutionStrategyParameters().getPath();
int parentLevel = path.getLevel();
int curLevel = parentLevel + 1;
int fieldCount = parameters.getExecutionStrategyParameters().getFields().size();
synchronized (callStack) {
callStack.increaseExpectedFetchCount(curLevel, fieldCount);
callStack.increaseHappenedStrategyCalls(curLevel);
}
return new ExecutionStrategyInstrumentationContext() {
@Override
public void onDispatched(CompletableFuture<ExecutionResult> result) {
}
@Override
public void onCompleted(ExecutionResult result, Throwable t) {
}
@Override
public void onFieldValuesInfo(List<FieldValueInfo> fieldValueInfoList) {
boolean dispatchNeeded;
synchronized (callStack) {
dispatchNeeded = handleOnFieldValuesInfo(fieldValueInfoList, callStack, curLevel);
}
if (dispatchNeeded) {
dispatch();
}
}
@Override
public void onFieldValuesException() {
synchronized (callStack) {
callStack.increaseHappenedOnFieldValueCalls(curLevel);
}
}
};
}
use of graphql.execution.ResultPath 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 (ResultPath 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 ImmutableList.copyOf(errors);
}
use of graphql.execution.ResultPath in project graphql-java-extended-validation by graphql-java.
the class ResourceBundleMessageInterpolator method buildErrorClassification.
/**
* Override this method to build your own ErrorClassification
*
* @param messageTemplate the message template
* @param messageParams the parameters
* @param validationEnvironment the rule environment
*
* @return an ErrorClassification
*/
@SuppressWarnings("unused")
protected ErrorClassification buildErrorClassification(String messageTemplate, Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
ResultPath fieldOrArgumentPath = validationEnvironment.getValidatedPath();
GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
return new ValidationErrorType(fieldOrArgumentPath, directive);
}
Aggregations