Search in sources :

Example 1 with InstrumentationExecutionParameters

use of graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters in project graphql-java by graphql-java.

the class Execution method execute.

public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput, InstrumentationState instrumentationState) {
    NodeUtil.GetOperationResult getOperationResult = NodeUtil.getOperation(document, executionInput.getOperationName());
    Map<String, FragmentDefinition> fragmentsByName = getOperationResult.fragmentsByName;
    OperationDefinition operationDefinition = getOperationResult.operationDefinition;
    ValuesResolver valuesResolver = new ValuesResolver();
    Map<String, Object> inputVariables = executionInput.getVariables();
    List<VariableDefinition> variableDefinitions = operationDefinition.getVariableDefinitions();
    Map<String, Object> coercedVariables;
    try {
        coercedVariables = valuesResolver.coerceArgumentValues(graphQLSchema, variableDefinitions, inputVariables);
    } catch (RuntimeException rte) {
        if (rte instanceof GraphQLError) {
            return completedFuture(new ExecutionResultImpl((GraphQLError) rte));
        }
        throw rte;
    }
    ExecutionContext executionContext = newExecutionContextBuilder().instrumentation(instrumentation).instrumentationState(instrumentationState).executionId(executionId).graphQLSchema(graphQLSchema).queryStrategy(queryStrategy).mutationStrategy(mutationStrategy).subscriptionStrategy(subscriptionStrategy).context(executionInput.getContext()).root(executionInput.getRoot()).fragmentsByName(fragmentsByName).variables(coercedVariables).document(document).operationDefinition(operationDefinition).build();
    InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState);
    executionContext = instrumentation.instrumentExecutionContext(executionContext, parameters);
    return executeOperation(executionContext, parameters, executionInput.getRoot(), executionContext.getOperationDefinition());
}
Also used : FragmentDefinition(graphql.language.FragmentDefinition) VariableDefinition(graphql.language.VariableDefinition) NodeUtil(graphql.language.NodeUtil) ExecutionResultImpl(graphql.ExecutionResultImpl) GraphQLError(graphql.GraphQLError) InstrumentationExecutionParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters) OperationDefinition(graphql.language.OperationDefinition)

Example 2 with InstrumentationExecutionParameters

use of graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters in project graphql-java by graphql-java.

the class GraphQL method parse.

private ParseResult parse(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
    InstrumentationContext<Document> parseInstrumentation = instrumentation.beginParse(new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState));
    Parser parser = new Parser();
    Document document;
    try {
        document = parser.parseDocument(executionInput.getQuery());
    } catch (ParseCancellationException e) {
        parseInstrumentation.onCompleted(null, e);
        return ParseResult.ofError(e);
    }
    parseInstrumentation.onCompleted(document, null);
    return ParseResult.of(document);
}
Also used : ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) InstrumentationExecutionParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters) Document(graphql.language.Document) Parser(graphql.parser.Parser)

Example 3 with InstrumentationExecutionParameters

use of graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters 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

InstrumentationExecutionParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters)3 Document (graphql.language.Document)2 Parser (graphql.parser.Parser)2 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)2 Assert.assertNotNull (graphql.Assert.assertNotNull)1 ExecutionResultImpl (graphql.ExecutionResultImpl)1 GraphQLError (graphql.GraphQLError)1 InvalidSyntaxError.toInvalidSyntaxError (graphql.InvalidSyntaxError.toInvalidSyntaxError)1 AbortExecutionException (graphql.execution.AbortExecutionException)1 AsyncExecutionStrategy (graphql.execution.AsyncExecutionStrategy)1 AsyncSerialExecutionStrategy (graphql.execution.AsyncSerialExecutionStrategy)1 Execution (graphql.execution.Execution)1 ExecutionId (graphql.execution.ExecutionId)1 ExecutionIdProvider (graphql.execution.ExecutionIdProvider)1 ExecutionStrategy (graphql.execution.ExecutionStrategy)1 SubscriptionExecutionStrategy (graphql.execution.SubscriptionExecutionStrategy)1 Instrumentation (graphql.execution.instrumentation.Instrumentation)1 InstrumentationContext (graphql.execution.instrumentation.InstrumentationContext)1 InstrumentationState (graphql.execution.instrumentation.InstrumentationState)1 SimpleInstrumentation (graphql.execution.instrumentation.SimpleInstrumentation)1