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);
}
}
Aggregations