use of graphql.ExecutionResultImpl in project graphql-java by graphql-java.
the class ExecutionStrategy method completeValueForList.
/**
* Called to complete a list of value for a field based on a list type. This iterates the values and calls
* {@link #completeValue(ExecutionContext, ExecutionStrategyParameters)} for each value.
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
* @param result the result to complete, raw result
*
* @return an {@link ExecutionResult}
*/
protected CompletableFuture<ExecutionResult> completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object result) {
Iterable<Object> resultIterable = toIterable(executionContext, parameters, result);
resultIterable = parameters.getNonNullFieldValidator().validate(parameters.getPath(), resultIterable);
if (resultIterable == null) {
return completedFuture(new ExecutionResultImpl(null, null));
}
return completeValueForList(executionContext, parameters, resultIterable);
}
use of graphql.ExecutionResultImpl 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.ExecutionResultImpl in project graphql-java by graphql-java.
the class DeferredCall method addErrorsEncountered.
private ExecutionResult addErrorsEncountered(ExecutionResult executionResult) {
List<GraphQLError> errorsEncountered = errorSupport.getErrors();
if (errorsEncountered.isEmpty()) {
return executionResult;
}
ExecutionResultImpl sourceResult = (ExecutionResultImpl) executionResult;
ExecutionResultImpl.Builder builder = ExecutionResultImpl.newExecutionResult().from(sourceResult);
builder.addErrors(errorsEncountered);
return builder.build();
}
use of graphql.ExecutionResultImpl in project graphql-java by graphql-java.
the class DataLoaderDispatcherInstrumentation method instrumentExecutionResult.
@Override
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
if (!options.isIncludeStatistics()) {
return CompletableFuture.completedFuture(executionResult);
}
Map<Object, Object> currentExt = executionResult.getExtensions();
Map<Object, Object> statsMap = new LinkedHashMap<>();
statsMap.putAll(currentExt == null ? Collections.emptyMap() : currentExt);
Map<Object, Object> dataLoaderStats = buildStatsMap();
statsMap.put("dataloader", dataLoaderStats);
log.debug("Data loader stats : {}", dataLoaderStats);
return CompletableFuture.completedFuture(new ExecutionResultImpl(executionResult.getData(), executionResult.getErrors(), statsMap));
}
use of graphql.ExecutionResultImpl in project graphql-java by graphql-java.
the class BreadthFirstExecutionTestStrategy method execute.
@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
Map<String, List<Field>> fields = parameters.getFields();
Map<String, Object> fetchedValues = new LinkedHashMap<>();
// first fetch every value
for (String fieldName : fields.keySet()) {
Object fetchedValue = fetchField(executionContext, parameters, fields, fieldName);
fetchedValues.put(fieldName, fetchedValue);
}
// then for every fetched value, complete it
Map<String, Object> results = new LinkedHashMap<>();
for (String fieldName : fetchedValues.keySet()) {
List<Field> currentField = fields.get(fieldName);
Object fetchedValue = fetchedValues.get(fieldName);
ExecutionPath fieldPath = parameters.getPath().segment(fieldName);
ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath));
try {
completeValue(executionContext, results, fieldName, fetchedValue, newParameters);
} catch (NonNullableFieldWasNullException e) {
assertNonNullFieldPrecondition(e);
results = null;
break;
}
}
return CompletableFuture.completedFuture(new ExecutionResultImpl(results, executionContext.getErrors()));
}
Aggregations