Search in sources :

Example 1 with GraphQLException

use of graphql.GraphQLException in project graphql-java by graphql-java.

the class ExecutorServiceExecutionStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(final ExecutionContext executionContext, final ExecutionStrategyParameters parameters) {
    if (executorService == null) {
        return new AsyncExecutionStrategy().execute(executionContext, parameters);
    }
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
    InstrumentationContext<ExecutionResult> executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);
    Map<String, List<Field>> fields = parameters.getFields();
    Map<String, Future<CompletableFuture<ExecutionResult>>> futures = new LinkedHashMap<>();
    for (String fieldName : fields.keySet()) {
        final List<Field> currentField = fields.get(fieldName);
        ExecutionPath fieldPath = parameters.getPath().segment(fieldName);
        ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath));
        Callable<CompletableFuture<ExecutionResult>> resolveField = () -> resolveField(executionContext, newParameters);
        futures.put(fieldName, executorService.submit(resolveField));
    }
    CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
    executionStrategyCtx.onDispatched(overallResult);
    try {
        Map<String, Object> results = new LinkedHashMap<>();
        for (String fieldName : futures.keySet()) {
            ExecutionResult executionResult;
            try {
                executionResult = futures.get(fieldName).get().join();
            } catch (CompletionException e) {
                if (e.getCause() instanceof NonNullableFieldWasNullException) {
                    assertNonNullFieldPrecondition((NonNullableFieldWasNullException) e.getCause());
                    results = null;
                    break;
                } else {
                    throw e;
                }
            }
            results.put(fieldName, executionResult != null ? executionResult.getData() : null);
        }
        ExecutionResultImpl executionResult = new ExecutionResultImpl(results, executionContext.getErrors());
        overallResult.complete(executionResult);
        overallResult.whenComplete(executionStrategyCtx::onCompleted);
        return overallResult;
    } catch (InterruptedException | ExecutionException e) {
        executionStrategyCtx.onCompleted(null, e);
        throw new GraphQLException(e);
    }
}
Also used : Instrumentation(graphql.execution.instrumentation.Instrumentation) LinkedHashMap(java.util.LinkedHashMap) Field(graphql.language.Field) CompletableFuture(java.util.concurrent.CompletableFuture) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) List(java.util.List) GraphQLException(graphql.GraphQLException) ExecutionException(java.util.concurrent.ExecutionException) ExecutionResult(graphql.ExecutionResult) CompletionException(java.util.concurrent.CompletionException) ExecutionResultImpl(graphql.ExecutionResultImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)

Aggregations

ExecutionResult (graphql.ExecutionResult)1 ExecutionResultImpl (graphql.ExecutionResultImpl)1 GraphQLException (graphql.GraphQLException)1 Instrumentation (graphql.execution.instrumentation.Instrumentation)1 InstrumentationExecutionStrategyParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)1 Field (graphql.language.Field)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1