graphql.execution.ExecutionContext Maven / Gradle / Ivy
package graphql.execution;
import graphql.GraphQLError;
import graphql.Internal;
import graphql.PublicApi;
import graphql.execution.defer.DeferSupport;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.language.Document;
import graphql.language.FragmentDefinition;
import graphql.language.OperationDefinition;
import graphql.schema.GraphQLSchema;
import org.dataloader.DataLoaderRegistry;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
@SuppressWarnings("TypeParameterUnusedInFormals")
@PublicApi
public class ExecutionContext {
private final GraphQLSchema graphQLSchema;
private final ExecutionId executionId;
private final InstrumentationState instrumentationState;
private final ExecutionStrategy queryStrategy;
private final ExecutionStrategy mutationStrategy;
private final ExecutionStrategy subscriptionStrategy;
private final Map fragmentsByName;
private final OperationDefinition operationDefinition;
private final Document document;
private final Map variables;
private final Object root;
private final Object context;
private final Instrumentation instrumentation;
private final List errors = new CopyOnWriteArrayList<>();
private final DataLoaderRegistry dataLoaderRegistry;
private final DeferSupport deferSupport = new DeferSupport();
@Internal
ExecutionContext(Instrumentation instrumentation, ExecutionId executionId, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy, ExecutionStrategy subscriptionStrategy, Map fragmentsByName, Document document, OperationDefinition operationDefinition, Map variables, Object context, Object root, DataLoaderRegistry dataLoaderRegistry, List startingErrors) {
this.graphQLSchema = graphQLSchema;
this.executionId = executionId;
this.instrumentationState = instrumentationState;
this.queryStrategy = queryStrategy;
this.mutationStrategy = mutationStrategy;
this.subscriptionStrategy = subscriptionStrategy;
this.fragmentsByName = fragmentsByName;
this.document = document;
this.operationDefinition = operationDefinition;
this.variables = variables;
this.context = context;
this.root = root;
this.instrumentation = instrumentation;
this.dataLoaderRegistry = dataLoaderRegistry;
this.errors.addAll(startingErrors);
}
public ExecutionId getExecutionId() {
return executionId;
}
public InstrumentationState getInstrumentationState() {
return instrumentationState;
}
public Instrumentation getInstrumentation() {
return instrumentation;
}
public GraphQLSchema getGraphQLSchema() {
return graphQLSchema;
}
public Map getFragmentsByName() {
return fragmentsByName;
}
public Document getDocument() {
return document;
}
public OperationDefinition getOperationDefinition() {
return operationDefinition;
}
public Map getVariables() {
return variables;
}
public Object getContext() {
return context;
}
@SuppressWarnings("unchecked")
public T getRoot() {
return (T) root;
}
public FragmentDefinition getFragment(String name) {
return fragmentsByName.get(name);
}
public DataLoaderRegistry getDataLoaderRegistry() {
return dataLoaderRegistry;
}
/**
* This method will only put one error per field path.
*
* @param error the error to add
* @param fieldPath the field path to put it under
*/
public void addError(GraphQLError error, ExecutionPath fieldPath) {
//
// see http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability about how per
// field errors should be handled - ie only once per field if its already there for nullability
// but unclear if its not that error path
//
for (GraphQLError graphQLError : errors) {
List