Search in sources :

Example 1 with ResultPath

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

the class FieldLevelTrackingApproach method beginFieldFetch.

public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
    CallStack callStack = parameters.getInstrumentationState();
    ResultPath path = parameters.getEnvironment().getExecutionStepInfo().getPath();
    int level = path.getLevel();
    return new InstrumentationContext<Object>() {

        @Override
        public void onDispatched(CompletableFuture result) {
            boolean dispatchNeeded;
            synchronized (callStack) {
                callStack.increaseFetchCount(level);
                dispatchNeeded = dispatchIfNeeded(callStack, level);
            }
            if (dispatchNeeded) {
                dispatch();
            }
        }

        @Override
        public void onCompleted(Object result, Throwable t) {
        }
    };
}
Also used : InstrumentationContext(graphql.execution.instrumentation.InstrumentationContext) ExecutionStrategyInstrumentationContext(graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext) CompletableFuture(java.util.concurrent.CompletableFuture) ResultPath(graphql.execution.ResultPath)

Example 2 with ResultPath

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

Example 3 with ResultPath

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

the class InstrumentationExamples method fieldValidation.

private void fieldValidation() {
    ResultPath fieldPath = ResultPath.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) ResultPath(graphql.execution.ResultPath) SimpleFieldValidation(graphql.execution.instrumentation.fieldvalidation.SimpleFieldValidation) FieldValidation(graphql.execution.instrumentation.fieldvalidation.FieldValidation) FieldAndArguments(graphql.execution.instrumentation.fieldvalidation.FieldAndArguments)

Example 4 with ResultPath

use of graphql.execution.ResultPath in project solarthing by wildmountainfarms.

the class SolarThingExceptionHandler method onException.

/*
	It's worth nothing that SimpleDataFetcherExceptionHandler appends a "notprivacysafe." in front of the full class name for the logger.
	We aren't going to do that here, as there's no private information, but it's just worth noting the difference here.
	 */
@Override
public DataFetcherExceptionHandlerResult onException(DataFetcherExceptionHandlerParameters handlerParameters) {
    Throwable exception = handlerParameters.getException();
    SourceLocation sourceLocation = handlerParameters.getSourceLocation();
    ResultPath path = handlerParameters.getPath();
    if (exception instanceof DatabaseException) {
        // this is the most common exception, usually caused by a timeout
        LOGGER.info("Got database exception", exception);
    } else {
        LOGGER.warn("Got uncommon exception", exception);
    }
    ExceptionWhileDataFetching error = new ExceptionWhileDataFetching(path, exception, sourceLocation);
    return DataFetcherExceptionHandlerResult.newResult().error(error).build();
}
Also used : SourceLocation(graphql.language.SourceLocation) ResultPath(graphql.execution.ResultPath) ExceptionWhileDataFetching(graphql.ExceptionWhileDataFetching) DatabaseException(me.retrodaredevil.solarthing.rest.exceptions.DatabaseException)

Example 5 with ResultPath

use of graphql.execution.ResultPath in project molgenis-emx2 by molgenis.

the class GraphqlCustomExceptionHandler method onException.

@Override
public DataFetcherExceptionHandlerResult onException(DataFetcherExceptionHandlerParameters handlerParameters) {
    final Throwable exception = handlerParameters.getException();
    final SourceLocation sourceLocation = handlerParameters.getSourceLocation();
    final ResultPath path = handlerParameters.getPath();
    GraphQLError error = new ExceptionWhileDataFetching(path, exception, sourceLocation);
    final ErrorClassification errorType = error.getErrorType();
    if (exception instanceof MolgenisException) {
        error = new GraphQLError() {

            @Override
            public String getMessage() {
                return exception.toString();
            }

            @Override
            public List<SourceLocation> getLocations() {
                return List.of(sourceLocation);
            }

            @Override
            public ErrorClassification getErrorType() {
                return errorType;
            }
        };
    }
    return DataFetcherExceptionHandlerResult.newResult().error(error).build();
}
Also used : SourceLocation(graphql.language.SourceLocation) ErrorClassification(graphql.ErrorClassification) ResultPath(graphql.execution.ResultPath) MolgenisException(org.molgenis.emx2.MolgenisException) GraphQLError(graphql.GraphQLError) ExceptionWhileDataFetching(graphql.ExceptionWhileDataFetching) List(java.util.List)

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