Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 21 additions & 41 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingFieldSelectionSet;
import graphql.schema.DataFetchingFieldSelectionSetImpl;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLCodeRegistry;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLFieldDefinition;
Expand Down Expand Up @@ -148,9 +149,7 @@ protected ExecutionStrategy(DataFetcherExceptionHandler dataFetcherExceptionHand
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
*
* @return a promise to an {@link ExecutionResult}
*
* @throws NonNullableFieldWasNullException in the future if a non null field resolves to a null value
*/
public abstract CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException;
Expand All @@ -166,9 +165,7 @@ protected ExecutionStrategy(DataFetcherExceptionHandler dataFetcherExceptionHand
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
*
* @return a promise to an {@link ExecutionResult}
*
* @throws NonNullableFieldWasNullException in the future if a non null field resolves to a null value
*/
protected CompletableFuture<ExecutionResult> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
Expand All @@ -186,17 +183,16 @@ protected CompletableFuture<ExecutionResult> resolveField(ExecutionContext execu
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
*
* @return a promise to a {@link FieldValueInfo}
*
* @throws NonNullableFieldWasNullException in the {@link FieldValueInfo#getFieldValue()} future if a non null field resolves to a null value
*/
protected CompletableFuture<FieldValueInfo> resolveFieldWithInfo(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext, parameters, parameters.getField().getSingleField());
Supplier<ExecutionStepInfo> executionStepInfo = FpKit.memoize(() -> createExecutionStepInfo(executionContext, parameters, fieldDef, null));

Instrumentation instrumentation = executionContext.getInstrumentation();
InstrumentationContext<ExecutionResult> fieldCtx = instrumentation.beginField(
new InstrumentationFieldParameters(executionContext, fieldDef, createExecutionStepInfo(executionContext, parameters, fieldDef, null))
new InstrumentationFieldParameters(executionContext, executionStepInfo)
);

CompletableFuture<FetchedValue> fetchFieldFuture = fetchField(executionContext, parameters);
Expand Down Expand Up @@ -226,9 +222,7 @@ protected CompletableFuture<FieldValueInfo> resolveFieldWithInfoToNull(Execution
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
*
* @return a promise to a fetched object
*
* @throws NonNullableFieldWasNullException in the future if a non null field resolves to a null value
*/
protected CompletableFuture<FetchedValue> fetchField(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
Expand All @@ -244,10 +238,9 @@ protected CompletableFuture<FetchedValue> fetchField(ExecutionContext executionC
QueryDirectives queryDirectives = new QueryDirectivesImpl(field, executionContext.getGraphQLSchema(), executionContext.getVariables());

// if the DF (like PropertyDataFetcher) does not use the arguments of execution step info then dont build any
Supplier<Map<String, Object>> argumentValues = FpKit.memoize(
() -> valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables()));
Supplier<ExecutionStepInfo> executionStepInfo = FpKit.memoize(
() -> createExecutionStepInfo(executionContext, parameters, fieldDef, parentType));
Supplier<Map<String, Object>> argumentValues = () -> executionStepInfo.get().getArguments();


DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext)
Expand Down Expand Up @@ -359,9 +352,7 @@ private void handleFetchingException(ExecutionContext executionContext,
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
* @param fetchedValue the fetched raw value
*
* @return a {@link FieldValueInfo}
*
* @throws NonNullableFieldWasNullException in the {@link FieldValueInfo#getFieldValue()} future if a non null field resolves to a null value
*/
protected FieldValueInfo completeField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, FetchedValue fetchedValue) {
Expand All @@ -371,19 +362,15 @@ protected FieldValueInfo completeField(ExecutionContext executionContext, Execut
ExecutionStepInfo executionStepInfo = createExecutionStepInfo(executionContext, parameters, fieldDef, parentType);

Instrumentation instrumentation = executionContext.getInstrumentation();
InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, executionStepInfo, fetchedValue);
InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, () -> executionStepInfo, fetchedValue);
InstrumentationContext<ExecutionResult> ctxCompleteField = instrumentation.beginFieldComplete(
instrumentationParams
);

GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());

NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, executionStepInfo);

ExecutionStrategyParameters newParameters = parameters.transform(builder ->
builder.executionStepInfo(executionStepInfo)
.arguments(argumentValues)
.source(fetchedValue.getFetchedValue())
.localContext(fetchedValue.getLocalContext())
.nonNullFieldValidator(nonNullableFieldValidator)
Expand Down Expand Up @@ -413,9 +400,7 @@ protected FieldValueInfo completeField(ExecutionContext executionContext, Execut
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
*
* @return a {@link FieldValueInfo}
*
* @throws NonNullableFieldWasNullException if a non null field resolves to a null value
*/
protected FieldValueInfo completeValue(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
Expand Down Expand Up @@ -476,7 +461,6 @@ private CompletableFuture<ExecutionResult> completeValueForNull(ExecutionStrateg
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
* @param result the result to complete, raw result
*
* @return a {@link FieldValueInfo}
*/
protected FieldValueInfo completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object result) {
Expand All @@ -499,17 +483,14 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
* @param iterableValues the values to complete, can't be null
*
* @return a {@link FieldValueInfo}
*/
protected FieldValueInfo completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Iterable<Object> iterableValues) {

Collection<Object> values = FpKit.toCollection(iterableValues);
ExecutionStepInfo executionStepInfo = parameters.getExecutionStepInfo();
GraphQLFieldDefinition fieldDef = parameters.getExecutionStepInfo().getFieldDefinition();
GraphQLObjectType fieldContainer = parameters.getExecutionStepInfo().getFieldContainer();

InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, createExecutionStepInfo(executionContext, parameters, fieldDef, fieldContainer), values);
InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, () -> executionStepInfo, values);
Instrumentation instrumentation = executionContext.getInstrumentation();

InstrumentationContext<ExecutionResult> completeListCtx = instrumentation.beginFieldListComplete(
Expand Down Expand Up @@ -575,7 +556,6 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
* @param parameters contains the parameters holding the fields to be executed and source object
* @param scalarType the type of the scalar
* @param result the result to be coerced
*
* @return a promise to an {@link ExecutionResult}
*/
protected CompletableFuture<ExecutionResult> completeValueForScalar(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLScalarType scalarType, Object result) {
Expand Down Expand Up @@ -606,7 +586,6 @@ protected CompletableFuture<ExecutionResult> completeValueForScalar(ExecutionCon
* @param parameters contains the parameters holding the fields to be executed and source object
* @param enumType the type of the enum
* @param result the result to be coerced
*
* @return a promise to an {@link ExecutionResult}
*/
protected CompletableFuture<ExecutionResult> completeValueForEnum(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLEnumType enumType, Object result) {
Expand All @@ -631,7 +610,6 @@ protected CompletableFuture<ExecutionResult> completeValueForEnum(ExecutionConte
* @param parameters contains the parameters holding the fields to be executed and source object
* @param resolvedObjectType the resolved object type
* @param result the result to be coerced
*
* @return a promise to an {@link ExecutionResult}
*/
protected CompletableFuture<ExecutionResult> completeValueForObject(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLObjectType resolvedObjectType, Object result) {
Expand Down Expand Up @@ -676,9 +654,7 @@ private Object handleCoercionProblem(ExecutionContext context, ExecutionStrategy
* Converts an object that is known to should be an Iterable into one
*
* @param result the result object
*
* @return an Iterable from that object
*
* @throws java.lang.ClassCastException if its not an Iterable
*/
@SuppressWarnings("unchecked")
Expand All @@ -687,7 +663,7 @@ protected Iterable<Object> toIterable(Object result) {
}

protected GraphQLObjectType resolveType(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLType fieldType) {
return resolvedType.resolveType(executionContext, parameters.getField(), parameters.getSource(), parameters.getArguments(), fieldType);
return resolvedType.resolveType(executionContext, parameters.getField(), parameters.getSource(), parameters.getExecutionStepInfo().getArguments(), fieldType);
}


Expand All @@ -714,7 +690,6 @@ private void handleTypeMismatchProblem(ExecutionContext context, ExecutionStrate
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
* @param field the field to find the definition of
*
* @return a {@link GraphQLFieldDefinition}
*/
protected GraphQLFieldDefinition getFieldDef(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Field field) {
Expand All @@ -728,7 +703,6 @@ protected GraphQLFieldDefinition getFieldDef(ExecutionContext executionContext,
* @param schema the schema in play
* @param parentType the parent type of the field
* @param field the field to find the definition of
*
* @return a {@link GraphQLFieldDefinition}
*/
protected GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLObjectType parentType, Field field) {
Expand All @@ -746,7 +720,6 @@ protected GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLObject
*
* @param e this indicates that a null value was returned for a non null field, which needs to cause the parent field
* to become null OR continue on as an exception
*
* @throws NonNullableFieldWasNullException if a non null field resolves to a null value
*/
protected void assertNonNullFieldPrecondition(NonNullableFieldWasNullException e) throws NonNullableFieldWasNullException {
Expand Down Expand Up @@ -794,29 +767,36 @@ protected ExecutionResult handleNonNullException(ExecutionContext executionConte
* @param parameters contains the parameters holding the fields to be executed and source object
* @param fieldDefinition the field definition to build type info for
* @param fieldContainer the field container
*
* @return a new type info
*/
protected ExecutionStepInfo createExecutionStepInfo(ExecutionContext executionContext,
ExecutionStrategyParameters parameters,
GraphQLFieldDefinition fieldDefinition,
GraphQLObjectType fieldContainer) {
GraphQLOutputType fieldType = fieldDefinition.getType();
MergedField field = parameters.getField();
List<Argument> fieldArgs = field.getArguments();
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), fieldArgs, executionContext.getVariables());
ExecutionStepInfo parentStepInfo = parameters.getExecutionStepInfo();
GraphQLOutputType fieldType = fieldDefinition.getType();
List<GraphQLArgument> fieldArgDefs = fieldDefinition.getArguments();
Map<String, Object> argumentValues = Collections.emptyMap();
//
// no need to create args at all if there are none on the field def
//
if (!fieldArgDefs.isEmpty()) {
List<Argument> fieldArgs = field.getArguments();
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldArgDefs, fieldArgs, executionContext.getVariables());
}


return newExecutionStepInfo()
.type(fieldType)
.fieldDefinition(fieldDefinition)
.fieldContainer(fieldContainer)
.field(field)
.path(parameters.getPath())
.parentInfo(parameters.getExecutionStepInfo())
.parentInfo(parentStepInfo)
.arguments(argumentValues)
.build();

}


Expand Down
16 changes: 1 addition & 15 deletions src/main/java/graphql/execution/ExecutionStrategyParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class ExecutionStrategyParameters {
private final ExecutionStepInfo executionStepInfo;
private final Object source;
private final Object localContext;
private final Map<String, Object> arguments;
private final MergedSelectionSet fields;
private final NonNullableFieldValidator nonNullableFieldValidator;
private final ResultPath path;
Expand All @@ -29,7 +28,6 @@ private ExecutionStrategyParameters(ExecutionStepInfo executionStepInfo,
Object source,
Object localContext,
MergedSelectionSet fields,
Map<String, Object> arguments,
NonNullableFieldValidator nonNullableFieldValidator,
ResultPath path,
MergedField currentField,
Expand All @@ -41,7 +39,6 @@ private ExecutionStrategyParameters(ExecutionStepInfo executionStepInfo,
this.localContext = localContext;
this.fields = assertNotNull(fields, () -> "fields is null");
this.source = source;
this.arguments = arguments;
this.nonNullableFieldValidator = nonNullableFieldValidator;
this.path = path;
this.currentField = currentField;
Expand All @@ -62,10 +59,6 @@ public MergedSelectionSet getFields() {
return fields;
}

public Map<String, Object> getArguments() {
return arguments;
}

public NonNullableFieldValidator getNonNullFieldValidator() {
return nonNullableFieldValidator;
}
Expand Down Expand Up @@ -124,7 +117,6 @@ public static class Builder {
Object source;
Object localContext;
MergedSelectionSet fields;
Map<String, Object> arguments;
NonNullableFieldValidator nonNullableFieldValidator;
ResultPath path = ResultPath.rootPath();
MergedField currentField;
Expand All @@ -146,7 +138,6 @@ private Builder(ExecutionStrategyParameters oldParameters) {
this.source = oldParameters.source;
this.localContext = oldParameters.localContext;
this.fields = oldParameters.fields;
this.arguments = oldParameters.arguments;
this.nonNullableFieldValidator = oldParameters.nonNullableFieldValidator;
this.currentField = oldParameters.currentField;
this.path = oldParameters.path;
Expand Down Expand Up @@ -185,11 +176,6 @@ public Builder localContext(Object localContext) {
return this;
}

public Builder arguments(Map<String, Object> arguments) {
this.arguments = arguments;
return this;
}

public Builder nonNullFieldValidator(NonNullableFieldValidator nonNullableFieldValidator) {
this.nonNullableFieldValidator = Assert.assertNotNull(nonNullableFieldValidator, () -> "requires a NonNullValidator");
return this;
Expand Down Expand Up @@ -217,7 +203,7 @@ public Builder parent(ExecutionStrategyParameters parent) {


public ExecutionStrategyParameters build() {
return new ExecutionStrategyParameters(executionStepInfo, source, localContext, fields, arguments, nonNullableFieldValidator, path, currentField, listSize, currentListIndex, parent);
return new ExecutionStrategyParameters(executionStepInfo, source, localContext, fields, nonNullableFieldValidator, path, currentField, listSize, currentListIndex, parent);
}
}
}
Loading