graphql.execution.instrumentation.Instrumentation Maven / Gradle / Ivy
package graphql.execution.instrumentation;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.ExperimentalApi;
import graphql.PublicSpi;
import graphql.execution.ExecutionContext;
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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import static graphql.execution.instrumentation.SimpleInstrumentationContext.noOp;
/**
* Provides the capability to instrument the execution steps of a GraphQL query.
*
* For example you might want to track which fields are taking the most time to fetch from the backing database
* or log what fields are being asked for.
*
* Remember that graphql calls can cross threads so make sure you think about the thread safety of any instrumentation
* code when you are writing it.
*
* Each step gives back an {@link graphql.execution.instrumentation.InstrumentationContext} object. This has two callbacks on it,
* one for the step is `dispatched` and one for when the step has `completed`. This is done because many of the "steps" are asynchronous
* operations such as fetching data and resolving it into objects.
*/
@PublicSpi
public interface Instrumentation {
/**
* This will be called just before execution to create an object, in an asynchronous manner, that is given back to all instrumentation methods
* to allow them to have per execution request state
*
* @param parameters the parameters to this step
*
* @return a state object that is passed to each method
*/
@Nullable
default CompletableFuture createStateAsync(InstrumentationCreateStateParameters parameters) {
InstrumentationState state = createState(parameters);
return state == null ? null : CompletableFuture.completedFuture(state);
}
/**
* This method is retained for backwards compatibility reasons so that previous {@link Instrumentation} implementations
* continue to work. The graphql-java code only called {@link #createStateAsync(InstrumentationCreateStateParameters)}
* but the default implementation calls back to this method.
*
* @param parameters the parameters to this step
*
* @return a state object that is passed to each method
*/
@Nullable
default InstrumentationState createState(InstrumentationCreateStateParameters parameters) {
return null;
}
/**
* This is called right at the start of query execution, and it's the first step in the instrumentation chain.
*
* @param parameters the parameters to this step
* @param state the state created during the call to {@link #createStateAsync(InstrumentationCreateStateParameters)}
*
* @return a nullable {@link InstrumentationContext} object that will be called back when the step ends (assuming it's not null)
*/
@Nullable
default InstrumentationContext beginExecution(InstrumentationExecutionParameters parameters, InstrumentationState state) {
return noOp();
}
/**
* This is called just before a query is parsed.
*
* @param parameters the parameters to this step
* @param state the state created during the call to {@link #createStateAsync(InstrumentationCreateStateParameters)}
*
* @return a nullable {@link InstrumentationContext} object that will be called back when the step ends (assuming it's not null)
*/
@Nullable
default InstrumentationContext beginParse(InstrumentationExecutionParameters parameters, InstrumentationState state) {
return noOp();
}
/**
* This is called just before the parsed query document is validated.
*
* @param parameters the parameters to this step
* @param state the state created during the call to {@link #createStateAsync(InstrumentationCreateStateParameters)}
*
* @return a nullable {@link InstrumentationContext} object that will be called back when the step ends (assuming it's not null)
*/
@Nullable
default InstrumentationContext> beginValidation(InstrumentationValidationParameters parameters, InstrumentationState state) {
return noOp();
}
/**
* This is called just before the execution of the query operation is started.
*
* @param parameters the parameters to this step
* @param state the state created during the call to {@link #createStateAsync(InstrumentationCreateStateParameters)}
*
* @return a nullable {@link InstrumentationContext} object that will be called back when the step ends (assuming it's not null)
*/
@Nullable
default InstrumentationContext beginExecuteOperation(InstrumentationExecuteOperationParameters parameters, InstrumentationState state) {
return noOp();
}
/**
* This is called each time an {@link graphql.execution.ExecutionStrategy} is invoked, which may be multiple times
* per query as the engine recursively descends over the query.
*
* @param parameters the parameters to this step
* @param state the state created during the call to {@link #createStateAsync(InstrumentationCreateStateParameters)}
*
* @return a nullable {@link ExecutionStrategyInstrumentationContext} object that will be called back when the step ends (assuming it's not null)
*/
@Nullable
default ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters, InstrumentationState state) {
return ExecutionStrategyInstrumentationContext.NOOP;
}
/**
* This is called each time an {@link graphql.execution.ExecutionStrategy} object resolution is called, which may be multiple times
* per query as the engine recursively descends over the query.
*
* @param parameters the parameters to this step
* @param state the state created during the call to {@link #createStateAsync(InstrumentationCreateStateParameters)}
*
* @return a nullable {@link ExecutionStrategyInstrumentationContext} object that will be called back when the step ends (assuming it's not null)
*/
@Nullable
default ExecuteObjectInstrumentationContext beginExecuteObject(InstrumentationExecutionStrategyParameters parameters, InstrumentationState state) {
return ExecuteObjectInstrumentationContext.NOOP;
}
/**
* This is called just before a deferred field is resolved into a value.
*
* This is an EXPERIMENTAL instrumentation callback. The method signature will definitely change.
*
* @param state the state created during the call to {@link #createStateAsync(InstrumentationCreateStateParameters)}
*
* @return a nullable {@link ExecutionStrategyInstrumentationContext} object that will be called back when the step ends (assuming it's not null)
*/
@ExperimentalApi
default InstrumentationContext