graphql.kickstart.execution.instrumentation.ConfigurableDispatchInstrumentation Maven / Gradle / Ivy
package graphql.kickstart.execution.instrumentation;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.execution.AsyncExecutionStrategy;
import graphql.execution.ExecutionContext;
import graphql.execution.ExecutionStrategy;
import graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.SimpleInstrumentationContext;
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation;
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationOptions;
import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters;
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters;
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
import graphql.language.OperationDefinition;
import graphql.schema.DataFetcher;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;
import org.dataloader.stats.Statistics;
@Slf4j
public class ConfigurableDispatchInstrumentation extends DataLoaderDispatcherInstrumentation {
private final DataLoaderDispatcherInstrumentationOptions options;
private final Function approachFunction;
/** Creates a DataLoaderDispatcherInstrumentation with the default options */
public ConfigurableDispatchInstrumentation(
Function approachFunction) {
this(DataLoaderDispatcherInstrumentationOptions.newOptions(), approachFunction);
}
/**
* Creates a DataLoaderDispatcherInstrumentation with the specified options
*
* @param options the options to control the behaviour
*/
public ConfigurableDispatchInstrumentation(
DataLoaderDispatcherInstrumentationOptions options,
Function approachFunction) {
this.options = options;
this.approachFunction = approachFunction;
}
@Override
public InstrumentationState createState(InstrumentationCreateStateParameters parameters) {
DataLoaderRegistry registry = parameters.getExecutionInput().getDataLoaderRegistry();
return new DataLoaderDispatcherInstrumentationState(
registry,
approachFunction.apply(registry),
parameters.getExecutionInput().getExecutionId());
}
@Override
public DataFetcher> instrumentDataFetcher(
DataFetcher> dataFetcher,
InstrumentationFieldFetchParameters parameters,
InstrumentationState instrumentationState) {
DataLoaderDispatcherInstrumentationState state =
InstrumentationState.ofState(instrumentationState);
if (state.isAggressivelyBatching()) {
return dataFetcher;
}
//
// currently only AsyncExecutionStrategy with DataLoader and hence this allows us to "dispatch"
// on every object if it's not using aggressive batching for other execution strategies
// which allows them to work if used.
return (DataFetcher