use of graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters in project graphql-java by graphql-java.
the class BatchedExecutionStrategy method execute.
@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
InstrumentationContext<ExecutionResult> executionStrategyCtx = executionContext.getInstrumentation().beginExecutionStrategy(new InstrumentationExecutionStrategyParameters(executionContext, parameters));
GraphQLObjectType type = parameters.getTypeInfo().castType(GraphQLObjectType.class);
ExecutionNode root = new ExecutionNode(type, parameters.getTypeInfo(), parameters.getFields(), singletonList(MapOrList.createMap(new LinkedHashMap<>())), Collections.singletonList(parameters.getSource()));
Queue<ExecutionNode> nodes = new ArrayDeque<>();
CompletableFuture<ExecutionResult> result = new CompletableFuture<>();
executeImpl(executionContext, parameters, root, root, nodes, root.getFields().keySet().iterator(), result);
executionStrategyCtx.onDispatched(result);
result.whenComplete(executionStrategyCtx::onCompleted);
return result;
}
use of graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters in project graphql-java by graphql-java.
the class AsyncSerialExecutionStrategy method execute.
@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
Instrumentation instrumentation = executionContext.getInstrumentation();
InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
InstrumentationContext<ExecutionResult> executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);
Map<String, List<Field>> fields = parameters.getFields();
List<String> fieldNames = new ArrayList<>(fields.keySet());
CompletableFuture<List<ExecutionResult>> resultsFuture = Async.eachSequentially(fieldNames, (fieldName, index, prevResults) -> {
List<Field> currentField = fields.get(fieldName);
ExecutionPath fieldPath = parameters.getPath().segment(fieldName);
ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath));
return resolveField(executionContext, newParameters);
});
CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
executionStrategyCtx.onDispatched(overallResult);
resultsFuture.whenComplete(handleResults(executionContext, fieldNames, overallResult));
overallResult.whenComplete(executionStrategyCtx::onCompleted);
return overallResult;
}
use of graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters 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);
}
}
use of graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters in project graphql-java by graphql-java.
the class AsyncExecutionStrategy method execute.
@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
Instrumentation instrumentation = executionContext.getInstrumentation();
InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
InstrumentationContext<ExecutionResult> executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);
Map<String, List<Field>> fields = parameters.getFields();
List<String> fieldNames = new ArrayList<>(fields.keySet());
List<CompletableFuture<ExecutionResult>> futures = new ArrayList<>();
for (String fieldName : fieldNames) {
List<Field> currentField = fields.get(fieldName);
ExecutionPath fieldPath = parameters.getPath().segment(fieldName);
ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath));
if (isDeferred(executionContext, newParameters, currentField)) {
continue;
}
CompletableFuture<ExecutionResult> future = resolveField(executionContext, newParameters);
futures.add(future);
}
CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
executionStrategyCtx.onDispatched(overallResult);
Async.each(futures).whenComplete(handleResults(executionContext, fieldNames, overallResult));
overallResult.whenComplete(executionStrategyCtx::onCompleted);
return overallResult;
}
Aggregations