graphql.execution.AsyncExecutionStrategy Maven / Gradle / Ivy
package graphql.execution;
import graphql.ExecutionResult;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters;
import graphql.language.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
/**
* The standard graphql execution strategy that runs fields asynchronously non-blocking.
*/
public class AsyncExecutionStrategy extends AbstractAsyncExecutionStrategy {
/**
* The standard graphql execution strategy that runs fields asynchronously
*/
public AsyncExecutionStrategy() {
super(new SimpleDataFetcherExceptionHandler());
}
/**
* Creates a execution strategy that uses the provided exception handler
*
* @param exceptionHandler the exception handler to use
*/
public AsyncExecutionStrategy(DataFetcherExceptionHandler exceptionHandler) {
super(exceptionHandler);
}
@Override
public CompletableFuture execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
InstrumentationContext> executionStrategyCtx = executionContext.getInstrumentation().beginExecutionStrategy(new InstrumentationExecutionStrategyParameters(executionContext));
Map> fields = parameters.fields();
List fieldNames = new ArrayList<>(fields.keySet());
List> futures = new ArrayList<>();
for (String fieldName : fieldNames) {
List currentField = fields.get(fieldName);
ExecutionPath fieldPath = parameters.path().segment(fieldName);
ExecutionStrategyParameters newParameters = parameters
.transform(builder -> builder.field(currentField).path(fieldPath));
CompletableFuture future = resolveField(executionContext, newParameters);
futures.add(future);
}
CompletableFuture overallResult = new CompletableFuture<>();
Async.each(futures).whenComplete(handleResults(executionContext, fieldNames, overallResult));
executionStrategyCtx.onEnd(overallResult,null);
return overallResult;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy