Search in sources :

Example 1 with InstrumentationExecuteOperationParameters

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

the class Execution method executeOperation.

private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext executionContext, InstrumentationExecutionParameters instrumentationExecutionParameters, Object root, OperationDefinition operationDefinition) {
    InstrumentationExecuteOperationParameters instrumentationParams = new InstrumentationExecuteOperationParameters(executionContext);
    InstrumentationContext<ExecutionResult> executeOperationCtx = instrumentation.beginExecuteOperation(instrumentationParams);
    OperationDefinition.Operation operation = operationDefinition.getOperation();
    GraphQLObjectType operationRootType;
    try {
        operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition);
    } catch (RuntimeException rte) {
        if (rte instanceof GraphQLError) {
            ExecutionResult executionResult = new ExecutionResultImpl(Collections.singletonList((GraphQLError) rte));
            CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture(executionResult);
            executeOperationCtx.onDispatched(resultCompletableFuture);
            executeOperationCtx.onCompleted(executionResult, rte);
            return resultCompletableFuture;
        }
        throw rte;
    }
    FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters().schema(executionContext.getGraphQLSchema()).objectType(operationRootType).fragments(executionContext.getFragmentsByName()).variables(executionContext.getVariables()).build();
    Map<String, List<Field>> fields = fieldCollector.collectFields(collectorParameters, operationDefinition.getSelectionSet());
    ExecutionPath path = ExecutionPath.rootPath();
    ExecutionTypeInfo typeInfo = newTypeInfo().type(operationRootType).path(path).build();
    NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, typeInfo);
    ExecutionStrategyParameters parameters = newParameters().typeInfo(typeInfo).source(root).fields(fields).nonNullFieldValidator(nonNullableFieldValidator).path(path).build();
    CompletableFuture<ExecutionResult> result;
    try {
        ExecutionStrategy executionStrategy;
        if (operation == OperationDefinition.Operation.MUTATION) {
            executionStrategy = mutationStrategy;
        } else if (operation == SUBSCRIPTION) {
            executionStrategy = subscriptionStrategy;
        } else {
            executionStrategy = queryStrategy;
        }
        log.debug("Executing '{}' query operation: '{}' using '{}' execution strategy", executionContext.getExecutionId(), operation, executionStrategy.getClass().getName());
        result = executionStrategy.execute(executionContext, parameters);
    } catch (NonNullableFieldWasNullException e) {
        // this means it was non null types all the way from an offending non null type
        // up to the root object type and there was a a null value some where.
        // 
        // The spec says we should return null for the data in this case
        // 
        // http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability
        // 
        result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
    }
    // note this happens NOW - not when the result completes
    executeOperationCtx.onDispatched(result);
    result = result.whenComplete(executeOperationCtx::onCompleted);
    return deferSupport(executionContext, result);
}
Also used : ExecutionResult(graphql.ExecutionResult) InstrumentationExecuteOperationParameters(graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionResultImpl(graphql.ExecutionResultImpl) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLError(graphql.GraphQLError) List(java.util.List) OperationDefinition(graphql.language.OperationDefinition)

Aggregations

ExecutionResult (graphql.ExecutionResult)1 ExecutionResultImpl (graphql.ExecutionResultImpl)1 GraphQLError (graphql.GraphQLError)1 InstrumentationExecuteOperationParameters (graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters)1 OperationDefinition (graphql.language.OperationDefinition)1 GraphQLObjectType (graphql.schema.GraphQLObjectType)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1