All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.execution.AsyncExecutionStrategy Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.execution;

import graphql.ExecutionResult;
import graphql.execution.instrumentation.Instrumentation;
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 {

        Instrumentation instrumentation = executionContext.getInstrumentation();
        InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext);

        InstrumentationContext> executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);

        InstrumentationContext>> beginFieldsCtx = instrumentation.beginFields(instrumentationParameters);

        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);
        }
        beginFieldsCtx.onEnd(fields, null);

        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