Skip to content

Commit 264cb12

Browse files
authored
Lazy ExecutionStepInfo for instrumentation and alo reducing the number of calls to this (#2023)
1 parent be01c7f commit 264cb12

7 files changed

Lines changed: 52 additions & 83 deletions

‎src/main/java/graphql/execution/ExecutionStrategy.java‎

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import graphql.schema.DataFetchingEnvironment;
2525
import graphql.schema.DataFetchingFieldSelectionSet;
2626
import graphql.schema.DataFetchingFieldSelectionSetImpl;
27+
import graphql.schema.GraphQLArgument;
2728
import graphql.schema.GraphQLCodeRegistry;
2829
import graphql.schema.GraphQLEnumType;
2930
import graphql.schema.GraphQLFieldDefinition;
@@ -148,9 +149,7 @@ protected ExecutionStrategy(DataFetcherExceptionHandler dataFetcherExceptionHand
148149
*
149150
* @param executionContext contains the top level execution parameters
150151
* @param parameters contains the parameters holding the fields to be executed and source object
151-
*
152152
* @return a promise to an {@link ExecutionResult}
153-
*
154153
* @throws NonNullableFieldWasNullException in the future if a non null field resolves to a null value
155154
*/
156155
public abstract CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException;
@@ -166,9 +165,7 @@ protected ExecutionStrategy(DataFetcherExceptionHandler dataFetcherExceptionHand
166165
*
167166
* @param executionContext contains the top level execution parameters
168167
* @param parameters contains the parameters holding the fields to be executed and source object
169-
*
170168
* @return a promise to an {@link ExecutionResult}
171-
*
172169
* @throws NonNullableFieldWasNullException in the future if a non null field resolves to a null value
173170
*/
174171
protected CompletableFuture<ExecutionResult> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
@@ -186,17 +183,16 @@ protected CompletableFuture<ExecutionResult> resolveField(ExecutionContext execu
186183
*
187184
* @param executionContext contains the top level execution parameters
188185
* @param parameters contains the parameters holding the fields to be executed and source object
189-
*
190186
* @return a promise to a {@link FieldValueInfo}
191-
*
192187
* @throws NonNullableFieldWasNullException in the {@link FieldValueInfo#getFieldValue()} future if a non null field resolves to a null value
193188
*/
194189
protected CompletableFuture<FieldValueInfo> resolveFieldWithInfo(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
195190
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext, parameters, parameters.getField().getSingleField());
191+
Supplier<ExecutionStepInfo> executionStepInfo = FpKit.memoize(() -> createExecutionStepInfo(executionContext, parameters, fieldDef, null));
196192

197193
Instrumentation instrumentation = executionContext.getInstrumentation();
198194
InstrumentationContext<ExecutionResult> fieldCtx = instrumentation.beginField(
199-
new InstrumentationFieldParameters(executionContext, fieldDef, createExecutionStepInfo(executionContext, parameters, fieldDef, null))
195+
new InstrumentationFieldParameters(executionContext, executionStepInfo)
200196
);
201197

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

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

252245

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

373364
Instrumentation instrumentation = executionContext.getInstrumentation();
374-
InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, executionStepInfo, fetchedValue);
365+
InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, () -> executionStepInfo, fetchedValue);
375366
InstrumentationContext<ExecutionResult> ctxCompleteField = instrumentation.beginFieldComplete(
376367
instrumentationParams
377368
);
378369

379-
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
380-
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());
381-
382370
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, executionStepInfo);
383371

384372
ExecutionStrategyParameters newParameters = parameters.transform(builder ->
385373
builder.executionStepInfo(executionStepInfo)
386-
.arguments(argumentValues)
387374
.source(fetchedValue.getFetchedValue())
388375
.localContext(fetchedValue.getLocalContext())
389376
.nonNullFieldValidator(nonNullableFieldValidator)
@@ -413,9 +400,7 @@ protected FieldValueInfo completeField(ExecutionContext executionContext, Execut
413400
*
414401
* @param executionContext contains the top level execution parameters
415402
* @param parameters contains the parameters holding the fields to be executed and source object
416-
*
417403
* @return a {@link FieldValueInfo}
418-
*
419404
* @throws NonNullableFieldWasNullException if a non null field resolves to a null value
420405
*/
421406
protected FieldValueInfo completeValue(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
@@ -476,7 +461,6 @@ private CompletableFuture<ExecutionResult> completeValueForNull(ExecutionStrateg
476461
* @param executionContext contains the top level execution parameters
477462
* @param parameters contains the parameters holding the fields to be executed and source object
478463
* @param result the result to complete, raw result
479-
*
480464
* @return a {@link FieldValueInfo}
481465
*/
482466
protected FieldValueInfo completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object result) {
@@ -499,17 +483,14 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
499483
* @param executionContext contains the top level execution parameters
500484
* @param parameters contains the parameters holding the fields to be executed and source object
501485
* @param iterableValues the values to complete, can't be null
502-
*
503486
* @return a {@link FieldValueInfo}
504487
*/
505488
protected FieldValueInfo completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Iterable<Object> iterableValues) {
506489

507490
Collection<Object> values = FpKit.toCollection(iterableValues);
508491
ExecutionStepInfo executionStepInfo = parameters.getExecutionStepInfo();
509-
GraphQLFieldDefinition fieldDef = parameters.getExecutionStepInfo().getFieldDefinition();
510-
GraphQLObjectType fieldContainer = parameters.getExecutionStepInfo().getFieldContainer();
511492

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

515496
InstrumentationContext<ExecutionResult> completeListCtx = instrumentation.beginFieldListComplete(
@@ -575,7 +556,6 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
575556
* @param parameters contains the parameters holding the fields to be executed and source object
576557
* @param scalarType the type of the scalar
577558
* @param result the result to be coerced
578-
*
579559
* @return a promise to an {@link ExecutionResult}
580560
*/
581561
protected CompletableFuture<ExecutionResult> completeValueForScalar(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLScalarType scalarType, Object result) {
@@ -606,7 +586,6 @@ protected CompletableFuture<ExecutionResult> completeValueForScalar(ExecutionCon
606586
* @param parameters contains the parameters holding the fields to be executed and source object
607587
* @param enumType the type of the enum
608588
* @param result the result to be coerced
609-
*
610589
* @return a promise to an {@link ExecutionResult}
611590
*/
612591
protected CompletableFuture<ExecutionResult> completeValueForEnum(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLEnumType enumType, Object result) {
@@ -631,7 +610,6 @@ protected CompletableFuture<ExecutionResult> completeValueForEnum(ExecutionConte
631610
* @param parameters contains the parameters holding the fields to be executed and source object
632611
* @param resolvedObjectType the resolved object type
633612
* @param result the result to be coerced
634-
*
635613
* @return a promise to an {@link ExecutionResult}
636614
*/
637615
protected CompletableFuture<ExecutionResult> completeValueForObject(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLObjectType resolvedObjectType, Object result) {
@@ -676,9 +654,7 @@ private Object handleCoercionProblem(ExecutionContext context, ExecutionStrategy
676654
* Converts an object that is known to should be an Iterable into one
677655
*
678656
* @param result the result object
679-
*
680657
* @return an Iterable from that object
681-
*
682658
* @throws java.lang.ClassCastException if its not an Iterable
683659
*/
684660
@SuppressWarnings("unchecked")
@@ -687,7 +663,7 @@ protected Iterable<Object> toIterable(Object result) {
687663
}
688664

689665
protected GraphQLObjectType resolveType(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLType fieldType) {
690-
return resolvedType.resolveType(executionContext, parameters.getField(), parameters.getSource(), parameters.getArguments(), fieldType);
666+
return resolvedType.resolveType(executionContext, parameters.getField(), parameters.getSource(), parameters.getExecutionStepInfo().getArguments(), fieldType);
691667
}
692668

693669

@@ -714,7 +690,6 @@ private void handleTypeMismatchProblem(ExecutionContext context, ExecutionStrate
714690
* @param executionContext contains the top level execution parameters
715691
* @param parameters contains the parameters holding the fields to be executed and source object
716692
* @param field the field to find the definition of
717-
*
718693
* @return a {@link GraphQLFieldDefinition}
719694
*/
720695
protected GraphQLFieldDefinition getFieldDef(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Field field) {
@@ -728,7 +703,6 @@ protected GraphQLFieldDefinition getFieldDef(ExecutionContext executionContext,
728703
* @param schema the schema in play
729704
* @param parentType the parent type of the field
730705
* @param field the field to find the definition of
731-
*
732706
* @return a {@link GraphQLFieldDefinition}
733707
*/
734708
protected GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLObjectType parentType, Field field) {
@@ -746,7 +720,6 @@ protected GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLObject
746720
*
747721
* @param e this indicates that a null value was returned for a non null field, which needs to cause the parent field
748722
* to become null OR continue on as an exception
749-
*
750723
* @throws NonNullableFieldWasNullException if a non null field resolves to a null value
751724
*/
752725
protected void assertNonNullFieldPrecondition(NonNullableFieldWasNullException e) throws NonNullableFieldWasNullException {
@@ -794,29 +767,36 @@ protected ExecutionResult handleNonNullException(ExecutionContext executionConte
794767
* @param parameters contains the parameters holding the fields to be executed and source object
795768
* @param fieldDefinition the field definition to build type info for
796769
* @param fieldContainer the field container
797-
*
798770
* @return a new type info
799771
*/
800772
protected ExecutionStepInfo createExecutionStepInfo(ExecutionContext executionContext,
801773
ExecutionStrategyParameters parameters,
802774
GraphQLFieldDefinition fieldDefinition,
803775
GraphQLObjectType fieldContainer) {
804-
GraphQLOutputType fieldType = fieldDefinition.getType();
805776
MergedField field = parameters.getField();
806-
List<Argument> fieldArgs = field.getArguments();
807-
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
808-
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), fieldArgs, executionContext.getVariables());
777+
ExecutionStepInfo parentStepInfo = parameters.getExecutionStepInfo();
778+
GraphQLOutputType fieldType = fieldDefinition.getType();
779+
List<GraphQLArgument> fieldArgDefs = fieldDefinition.getArguments();
780+
Map<String, Object> argumentValues = Collections.emptyMap();
781+
//
782+
// no need to create args at all if there are none on the field def
783+
//
784+
if (!fieldArgDefs.isEmpty()) {
785+
List<Argument> fieldArgs = field.getArguments();
786+
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
787+
argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldArgDefs, fieldArgs, executionContext.getVariables());
788+
}
789+
809790

810791
return newExecutionStepInfo()
811792
.type(fieldType)
812793
.fieldDefinition(fieldDefinition)
813794
.fieldContainer(fieldContainer)
814795
.field(field)
815796
.path(parameters.getPath())
816-
.parentInfo(parameters.getExecutionStepInfo())
797+
.parentInfo(parentStepInfo)
817798
.arguments(argumentValues)
818799
.build();
819-
820800
}
821801

822802

‎src/main/java/graphql/execution/ExecutionStrategyParameters.java‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class ExecutionStrategyParameters {
1616
private final ExecutionStepInfo executionStepInfo;
1717
private final Object source;
1818
private final Object localContext;
19-
private final Map<String, Object> arguments;
2019
private final MergedSelectionSet fields;
2120
private final NonNullableFieldValidator nonNullableFieldValidator;
2221
private final ResultPath path;
@@ -29,7 +28,6 @@ private ExecutionStrategyParameters(ExecutionStepInfo executionStepInfo,
2928
Object source,
3029
Object localContext,
3130
MergedSelectionSet fields,
32-
Map<String, Object> arguments,
3331
NonNullableFieldValidator nonNullableFieldValidator,
3432
ResultPath path,
3533
MergedField currentField,
@@ -41,7 +39,6 @@ private ExecutionStrategyParameters(ExecutionStepInfo executionStepInfo,
4139
this.localContext = localContext;
4240
this.fields = assertNotNull(fields, () -> "fields is null");
4341
this.source = source;
44-
this.arguments = arguments;
4542
this.nonNullableFieldValidator = nonNullableFieldValidator;
4643
this.path = path;
4744
this.currentField = currentField;
@@ -62,10 +59,6 @@ public MergedSelectionSet getFields() {
6259
return fields;
6360
}
6461

65-
public Map<String, Object> getArguments() {
66-
return arguments;
67-
}
68-
6962
public NonNullableFieldValidator getNonNullFieldValidator() {
7063
return nonNullableFieldValidator;
7164
}
@@ -124,7 +117,6 @@ public static class Builder {
124117
Object source;
125118
Object localContext;
126119
MergedSelectionSet fields;
127-
Map<String, Object> arguments;
128120
NonNullableFieldValidator nonNullableFieldValidator;
129121
ResultPath path = ResultPath.rootPath();
130122
MergedField currentField;
@@ -146,7 +138,6 @@ private Builder(ExecutionStrategyParameters oldParameters) {
146138
this.source = oldParameters.source;
147139
this.localContext = oldParameters.localContext;
148140
this.fields = oldParameters.fields;
149-
this.arguments = oldParameters.arguments;
150141
this.nonNullableFieldValidator = oldParameters.nonNullableFieldValidator;
151142
this.currentField = oldParameters.currentField;
152143
this.path = oldParameters.path;
@@ -185,11 +176,6 @@ public Builder localContext(Object localContext) {
185176
return this;
186177
}
187178

188-
public Builder arguments(Map<String, Object> arguments) {
189-
this.arguments = arguments;
190-
return this;
191-
}
192-
193179
public Builder nonNullFieldValidator(NonNullableFieldValidator nonNullableFieldValidator) {
194180
this.nonNullableFieldValidator = Assert.assertNotNull(nonNullableFieldValidator, () -> "requires a NonNullValidator");
195181
return this;
@@ -217,7 +203,7 @@ public Builder parent(ExecutionStrategyParameters parent) {
217203

218204

219205
public ExecutionStrategyParameters build() {
220-
return new ExecutionStrategyParameters(executionStepInfo, source, localContext, fields, arguments, nonNullableFieldValidator, path, currentField, listSize, currentListIndex, parent);
206+
return new ExecutionStrategyParameters(executionStepInfo, source, localContext, fields, nonNullableFieldValidator, path, currentField, listSize, currentListIndex, parent);
221207
}
222208
}
223209
}

0 commit comments

Comments
 (0)