Search in sources :

Example 6 with ResultPath

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;
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) ArrayList(java.util.ArrayList) ResultPath(graphql.execution.ResultPath) GraphQLError(graphql.GraphQLError) GraphQLDirective(graphql.schema.GraphQLDirective)

Example 7 with ResultPath

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);
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) Internal(graphql.Internal) FetchedValue(graphql.execution.FetchedValue) ValuesResolver(graphql.execution.ValuesResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionContext(graphql.execution.ExecutionContext) ExecutableNormalizedField(graphql.normalized.ExecutableNormalizedField) Supplier(java.util.function.Supplier) DefaultValueUnboxer(graphql.execution.DefaultValueUnboxer) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ImmutableKit(graphql.collect.ImmutableKit) ExecutionId(graphql.execution.ExecutionId) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) ImmutableList(com.google.common.collect.ImmutableList) GraphQLError(graphql.GraphQLError) Map(java.util.Map) DataFetcher(graphql.schema.DataFetcher) GraphQLTypeUtil(graphql.schema.GraphQLTypeUtil) DataFetcherResult(graphql.execution.DataFetcherResult) Assert(graphql.Assert) LogKit(graphql.util.LogKit) Async(graphql.execution.Async) Logger(org.slf4j.Logger) ExceptionWhileDataFetching(graphql.ExceptionWhileDataFetching) MergedField(graphql.execution.MergedField) GraphQLOutputType(graphql.schema.GraphQLOutputType) ResultPath(graphql.execution.ResultPath) Field(graphql.language.Field) DataFetchingFieldSelectionSetImpl(graphql.schema.DataFetchingFieldSelectionSetImpl) ExecutableNormalizedOperation(graphql.normalized.ExecutableNormalizedOperation) List(java.util.List) QueryDirectivesImpl(graphql.execution.directives.QueryDirectivesImpl) CompletionStage(java.util.concurrent.CompletionStage) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) FpKit(graphql.util.FpKit) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Collections(java.util.Collections) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) ExecutableNormalizedOperation(graphql.normalized.ExecutableNormalizedOperation) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) ExecutableNormalizedField(graphql.normalized.ExecutableNormalizedField) MergedField(graphql.execution.MergedField) Field(graphql.language.Field) GraphQLOutputType(graphql.schema.GraphQLOutputType) ExecutableNormalizedField(graphql.normalized.ExecutableNormalizedField) ResultPath(graphql.execution.ResultPath) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) QueryDirectivesImpl(graphql.execution.directives.QueryDirectivesImpl) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Map(java.util.Map) ExecutionId(graphql.execution.ExecutionId)

Example 8 with ResultPath

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);
            }
        }
    };
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionStrategyInstrumentationContext(graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext) ResultPath(graphql.execution.ResultPath) ExecutionResult(graphql.ExecutionResult) List(java.util.List)

Example 9 with ResultPath

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);
}
Also used : Optional(java.util.Optional) ArrayList(java.util.ArrayList) ResultPath(graphql.execution.ResultPath) GraphQLError(graphql.GraphQLError)

Example 10 with ResultPath

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);
}
Also used : ResultPath(graphql.execution.ResultPath) GraphQLDirective(graphql.schema.GraphQLDirective)

Aggregations

ResultPath (graphql.execution.ResultPath)12 GraphQLError (graphql.GraphQLError)6 ArrayList (java.util.ArrayList)6 List (java.util.List)4 ExceptionWhileDataFetching (graphql.ExceptionWhileDataFetching)3 GraphQLInputType (graphql.schema.GraphQLInputType)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ImmutableList (com.google.common.collect.ImmutableList)2 ExecutionStrategyInstrumentationContext (graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext)2 Field (graphql.language.Field)2 SourceLocation (graphql.language.SourceLocation)2 GraphQLDirective (graphql.schema.GraphQLDirective)2 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)2 Assert (graphql.Assert)1 ErrorClassification (graphql.ErrorClassification)1 ExecutionResult (graphql.ExecutionResult)1 Internal (graphql.Internal)1 QueryTraverser (graphql.analysis.QueryTraverser)1 QueryVisitorFieldEnvironment (graphql.analysis.QueryVisitorFieldEnvironment)1 QueryVisitorStub (graphql.analysis.QueryVisitorStub)1