graphql.execution.instrumentation.ChainedInstrumentation Maven / Gradle / Ivy
package graphql.execution.instrumentation;
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.InstrumentationDeferredFieldParameters;
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.language.Field;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLSchema;
import graphql.validation.ValidationError;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import static graphql.Assert.assertNotNull;
import static java.util.stream.Collectors.toList;
/**
* 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 List instrumentations;
public ChainedInstrumentation(List instrumentations) {
this.instrumentations = Collections.unmodifiableList(assertNotNull(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<>(instrumentations.stream()
.map(instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginExecution(parameters.withNewState(state));
})
.collect(toList()));
}
@Override
public InstrumentationContext beginParse(InstrumentationExecutionParameters parameters) {
return new ChainedInstrumentationContext<>(instrumentations.stream()
.map(instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginParse(parameters.withNewState(state));
})
.collect(toList()));
}
@Override
public InstrumentationContext> beginValidation(InstrumentationValidationParameters parameters) {
return new ChainedInstrumentationContext<>(instrumentations.stream()
.map(instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginValidation(parameters.withNewState(state));
})
.collect(toList()));
}
@Override
public InstrumentationContext beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
return new ChainedInstrumentationContext<>(instrumentations.stream()
.map(instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginExecuteOperation(parameters.withNewState(state));
})
.collect(toList()));
}
@Override
public ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
return new ChainedExecutionStrategyInstrumentationContext(instrumentations.stream()
.map(instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginExecutionStrategy(parameters.withNewState(state));
})
.collect(toList()));
}
@Override
public DeferredFieldInstrumentationContext beginDeferredField(InstrumentationDeferredFieldParameters parameters) {
return new ChainedDeferredExecutionStrategyInstrumentationContext(instrumentations.stream()
.map(instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginDeferredField(parameters.withNewState(state));
})
.collect(toList()));
}
@Override
public InstrumentationContext beginField(InstrumentationFieldParameters parameters) {
return new ChainedInstrumentationContext<>(instrumentations.stream()
.map(instrumentation -> {
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
return instrumentation.beginField(parameters.withNewState(state));
})
.collect(toList()));
}
@Override
public InstrumentationContext
© 2015 - 2025 Weber Informatics LLC | Privacy Policy