Search in sources :

Example 1 with InstrumentationState

use of graphql.execution.instrumentation.InstrumentationState in project graphql-java by graphql-java.

the class GraphQL method parseValidateAndExecute.

private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
    AtomicReference<ExecutionInput> executionInputRef = new AtomicReference<>(executionInput);
    PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.get(executionInput.getQuery(), transformedQuery -> {
        // if they change the original query in the pre-parser, then we want to see it downstream from then on
        executionInputRef.set(executionInput.transform(bldr -> bldr.query(transformedQuery)));
        return parseAndValidate(executionInputRef.get(), graphQLSchema, instrumentationState);
    });
    if (preparsedDoc.hasErrors()) {
        return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors()));
    }
    return execute(executionInputRef.get(), preparsedDoc.getDocument(), graphQLSchema, instrumentationState);
}
Also used : Execution(graphql.execution.Execution) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) AbortExecutionException(graphql.execution.AbortExecutionException) ExecutionStrategy(graphql.execution.ExecutionStrategy) InstrumentationExecutionParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) UnaryOperator(java.util.function.UnaryOperator) InstrumentationState(graphql.execution.instrumentation.InstrumentationState) PreparsedDocumentEntry(graphql.execution.preparsed.PreparsedDocumentEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionId(graphql.execution.ExecutionId) Parser(graphql.parser.Parser) SubscriptionExecutionStrategy(graphql.execution.SubscriptionExecutionStrategy) Map(java.util.Map) AsyncExecutionStrategy(graphql.execution.AsyncExecutionStrategy) GraphQLSchema(graphql.schema.GraphQLSchema) NoOpPreparsedDocumentProvider(graphql.execution.preparsed.NoOpPreparsedDocumentProvider) Logger(org.slf4j.Logger) PreparsedDocumentProvider(graphql.execution.preparsed.PreparsedDocumentProvider) CompletionException(java.util.concurrent.CompletionException) InvalidSyntaxError.toInvalidSyntaxError(graphql.InvalidSyntaxError.toInvalidSyntaxError) ExecutionIdProvider(graphql.execution.ExecutionIdProvider) InstrumentationContext(graphql.execution.instrumentation.InstrumentationContext) Consumer(java.util.function.Consumer) InstrumentationValidationParameters(graphql.execution.instrumentation.parameters.InstrumentationValidationParameters) Document(graphql.language.Document) ValidationError(graphql.validation.ValidationError) List(java.util.List) Instrumentation(graphql.execution.instrumentation.Instrumentation) Assert.assertNotNull(graphql.Assert.assertNotNull) SimpleInstrumentation(graphql.execution.instrumentation.SimpleInstrumentation) AsyncSerialExecutionStrategy(graphql.execution.AsyncSerialExecutionStrategy) Validator(graphql.validation.Validator) PreparsedDocumentEntry(graphql.execution.preparsed.PreparsedDocumentEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 2 with InstrumentationState

use of graphql.execution.instrumentation.InstrumentationState in project graphql-java by graphql-java.

the class GraphQL method executeAsync.

/**
 * Executes the graphql query using the provided input object
 * <p>
 * This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult}
 * which is the result of executing the provided query.
 *
 * @param executionInput {@link ExecutionInput}
 *
 * @return a promise to an {@link ExecutionResult} which can include errors
 */
public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionInput) {
    try {
        log.debug("Executing request. operation name: '{}'. query: '{}'. variables '{}'", executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables());
        InstrumentationState instrumentationState = instrumentation.createState();
        InstrumentationExecutionParameters inputInstrumentationParameters = new InstrumentationExecutionParameters(executionInput, this.graphQLSchema, instrumentationState);
        executionInput = instrumentation.instrumentExecutionInput(executionInput, inputInstrumentationParameters);
        InstrumentationExecutionParameters instrumentationParameters = new InstrumentationExecutionParameters(executionInput, this.graphQLSchema, instrumentationState);
        InstrumentationContext<ExecutionResult> executionInstrumentation = instrumentation.beginExecution(instrumentationParameters);
        GraphQLSchema graphQLSchema = instrumentation.instrumentSchema(this.graphQLSchema, instrumentationParameters);
        CompletableFuture<ExecutionResult> executionResult = parseValidateAndExecute(executionInput, graphQLSchema, instrumentationState);
        // 
        // finish up instrumentation
        executionResult = executionResult.whenComplete(executionInstrumentation::onCompleted);
        // 
        // allow instrumentation to tweak the result
        executionResult = executionResult.thenCompose(result -> instrumentation.instrumentExecutionResult(result, instrumentationParameters));
        return executionResult;
    } catch (AbortExecutionException abortException) {
        ExecutionResultImpl executionResult = new ExecutionResultImpl(abortException);
        if (!abortException.getUnderlyingErrors().isEmpty()) {
            executionResult = new ExecutionResultImpl(abortException.getUnderlyingErrors());
        }
        return CompletableFuture.completedFuture(executionResult);
    }
}
Also used : Execution(graphql.execution.Execution) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) AbortExecutionException(graphql.execution.AbortExecutionException) ExecutionStrategy(graphql.execution.ExecutionStrategy) InstrumentationExecutionParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) UnaryOperator(java.util.function.UnaryOperator) InstrumentationState(graphql.execution.instrumentation.InstrumentationState) PreparsedDocumentEntry(graphql.execution.preparsed.PreparsedDocumentEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionId(graphql.execution.ExecutionId) Parser(graphql.parser.Parser) SubscriptionExecutionStrategy(graphql.execution.SubscriptionExecutionStrategy) Map(java.util.Map) AsyncExecutionStrategy(graphql.execution.AsyncExecutionStrategy) GraphQLSchema(graphql.schema.GraphQLSchema) NoOpPreparsedDocumentProvider(graphql.execution.preparsed.NoOpPreparsedDocumentProvider) Logger(org.slf4j.Logger) PreparsedDocumentProvider(graphql.execution.preparsed.PreparsedDocumentProvider) CompletionException(java.util.concurrent.CompletionException) InvalidSyntaxError.toInvalidSyntaxError(graphql.InvalidSyntaxError.toInvalidSyntaxError) ExecutionIdProvider(graphql.execution.ExecutionIdProvider) InstrumentationContext(graphql.execution.instrumentation.InstrumentationContext) Consumer(java.util.function.Consumer) InstrumentationValidationParameters(graphql.execution.instrumentation.parameters.InstrumentationValidationParameters) Document(graphql.language.Document) ValidationError(graphql.validation.ValidationError) List(java.util.List) Instrumentation(graphql.execution.instrumentation.Instrumentation) Assert.assertNotNull(graphql.Assert.assertNotNull) SimpleInstrumentation(graphql.execution.instrumentation.SimpleInstrumentation) AsyncSerialExecutionStrategy(graphql.execution.AsyncSerialExecutionStrategy) Validator(graphql.validation.Validator) InstrumentationState(graphql.execution.instrumentation.InstrumentationState) InstrumentationExecutionParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters) AbortExecutionException(graphql.execution.AbortExecutionException) GraphQLSchema(graphql.schema.GraphQLSchema)

Aggregations

Assert.assertNotNull (graphql.Assert.assertNotNull)2 InvalidSyntaxError.toInvalidSyntaxError (graphql.InvalidSyntaxError.toInvalidSyntaxError)2 AbortExecutionException (graphql.execution.AbortExecutionException)2 AsyncExecutionStrategy (graphql.execution.AsyncExecutionStrategy)2 AsyncSerialExecutionStrategy (graphql.execution.AsyncSerialExecutionStrategy)2 Execution (graphql.execution.Execution)2 ExecutionId (graphql.execution.ExecutionId)2 ExecutionIdProvider (graphql.execution.ExecutionIdProvider)2 ExecutionStrategy (graphql.execution.ExecutionStrategy)2 SubscriptionExecutionStrategy (graphql.execution.SubscriptionExecutionStrategy)2 Instrumentation (graphql.execution.instrumentation.Instrumentation)2 InstrumentationContext (graphql.execution.instrumentation.InstrumentationContext)2 InstrumentationState (graphql.execution.instrumentation.InstrumentationState)2 SimpleInstrumentation (graphql.execution.instrumentation.SimpleInstrumentation)2 InstrumentationExecutionParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters)2 InstrumentationValidationParameters (graphql.execution.instrumentation.parameters.InstrumentationValidationParameters)2 NoOpPreparsedDocumentProvider (graphql.execution.preparsed.NoOpPreparsedDocumentProvider)2 PreparsedDocumentEntry (graphql.execution.preparsed.PreparsedDocumentEntry)2 PreparsedDocumentProvider (graphql.execution.preparsed.PreparsedDocumentProvider)2 Document (graphql.language.Document)2