graphql.execution.instrumentation.ChainedInstrumentation Maven / Gradle / Ivy
package graphql.execution.instrumentation;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.PublicApi;
import graphql.execution.Async;
import graphql.execution.ExecutionContext;
import graphql.execution.FieldValueInfo;
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.InstrumentationFieldCompleteParameters;
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters;
import graphql.execution.instrumentation.parameters.InstrumentationValidationParameters;
import graphql.language.Document;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLSchema;
import graphql.validation.ValidationError;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import static graphql.Assert.assertNotNull;
import static graphql.collect.ImmutableKit.map;
/**
* This allows you to chain together a number of {@link graphql.execution.instrumentation.Instrumentation} implementations
* and run them in sequence. The list order of instrumentation objects is always guaranteed to be followed and
* the {@link graphql.execution.instrumentation.InstrumentationState} objects they create will be passed back to the originating
* implementation.
*
* @see graphql.execution.instrumentation.Instrumentation
*/
@PublicApi
public class ChainedInstrumentation implements Instrumentation {
// This class is inspired from https://github.com/leangen/graphql-spqr/blob/master/src/main/java/io/leangen/graphql/GraphQLRuntime.java#L80
private final ImmutableList instrumentations;
public ChainedInstrumentation(List instrumentations) {
this.instrumentations = ImmutableList.copyOf(assertNotNull(instrumentations));
}
public ChainedInstrumentation(Instrumentation... instrumentations) {
this(Arrays.asList(instrumentations));
}
/**
* @return the list of instrumentations in play
*/
public List getInstrumentations() {
return instrumentations;
}
private InstrumentationState getState(Instrumentation instrumentation, InstrumentationState parametersInstrumentationState) {
ChainedInstrumentationState chainedInstrumentationState = (ChainedInstrumentationState) parametersInstrumentationState;
return chainedInstrumentationState.getState(instrumentation);
}
@Override
public InstrumentationState createState(InstrumentationCreateStateParameters parameters) {
return new ChainedInstrumentationState(instrumentations, parameters);
}
@Override
public InstrumentationContext beginExecution(final InstrumentationExecutionParameters parameters) {
return new ChainedInstrumentationContext<>(map(instrumentations, instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginExecution(parameters.withNewState(state));
}));
}
@Override
public InstrumentationContext beginParse(InstrumentationExecutionParameters parameters) {
return new ChainedInstrumentationContext<>(map(instrumentations, instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginParse(parameters.withNewState(state));
}));
}
@Override
public InstrumentationContext> beginValidation(InstrumentationValidationParameters parameters) {
return new ChainedInstrumentationContext<>(map(instrumentations, instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginValidation(parameters.withNewState(state));
}));
}
@Override
public InstrumentationContext beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
return new ChainedInstrumentationContext<>(map(instrumentations, instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginExecuteOperation(parameters.withNewState(state));
}));
}
@Override
public ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
return new ChainedExecutionStrategyInstrumentationContext(map(instrumentations, instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginExecutionStrategy(parameters.withNewState(state));
}));
}
@Override
public InstrumentationContext beginSubscribedFieldEvent(InstrumentationFieldParameters parameters) {
return new ChainedInstrumentationContext<>(map(instrumentations, instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginSubscribedFieldEvent(parameters.withNewState(state));
}));
}
@Override
public InstrumentationContext beginField(InstrumentationFieldParameters parameters) {
return new ChainedInstrumentationContext<>(map(instrumentations, instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginField(parameters.withNewState(state));
}));
}
@Override
public InstrumentationContext
© 2015 - 2025 Weber Informatics LLC | Privacy Policy