Skip to content

Cherry pick PR 3929 reduce nonnullablefieldvalidator allocations #3974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
2 changes: 1 addition & 1 deletion src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe

ResultPath path = ResultPath.rootPath();
ExecutionStepInfo executionStepInfo = newExecutionStepInfo().type(operationRootType).path(path).build();
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, executionStepInfo);
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext);

ExecutionStrategyParameters parameters = newParameters()
.executionStepInfo(executionStepInfo)
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,7 @@ private FieldValueInfo completeField(GraphQLFieldDefinition fieldDef, ExecutionC
instrumentationParams, executionContext.getInstrumentationState()
));

NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, executionStepInfo);

ExecutionStrategyParameters newParameters = parameters.transform(executionStepInfo,
nonNullableFieldValidator,
fetchedValue.getLocalContext(),
fetchedValue.getFetchedValue());

Expand Down Expand Up @@ -781,13 +778,12 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,

ExecutionStepInfo stepInfoForListElement = executionStepInfoFactory.newExecutionStepInfoForListElement(executionStepInfo, indexedPath);

NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, stepInfoForListElement);

FetchedValue value = unboxPossibleDataFetcherResult(executionContext, parameters, item);

ExecutionStrategyParameters newParameters = parameters.transform(stepInfoForListElement,
nonNullableFieldValidator, indexedPath,
value.getLocalContext(), value.getFetchedValue());
indexedPath,
value.getLocalContext(),
value.getFetchedValue());

fieldValueInfos.add(completeValue(executionContext, newParameters));
index++;
Expand Down Expand Up @@ -926,10 +922,8 @@ protected Object completeValueForObject(ExecutionContext executionContext, Execu
);

ExecutionStepInfo newExecutionStepInfo = executionStepInfo.changeTypeWithPreservedNonNull(resolvedObjectType);
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, newExecutionStepInfo);

ExecutionStrategyParameters newParameters = parameters.transform(newExecutionStepInfo,
nonNullableFieldValidator,
subFields,
result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private ExecutionStrategyParameters(ExecutionStepInfo executionStepInfo,
this.localContext = localContext;
this.fields = assertNotNull(fields, () -> "fields is null");
this.source = source;
this.nonNullableFieldValidator = nonNullableFieldValidator;
this.nonNullableFieldValidator = assertNotNull(nonNullableFieldValidator, () -> "requires a NonNullValidator");;
this.path = path;
this.currentField = currentField;
this.parent = parent;
Expand Down Expand Up @@ -132,7 +132,6 @@ ExecutionStrategyParameters transform(MergedField currentField,

@Internal
ExecutionStrategyParameters transform(ExecutionStepInfo executionStepInfo,
NonNullableFieldValidator nonNullableFieldValidator,
MergedSelectionSet fields,
Object source) {
return new ExecutionStrategyParameters(executionStepInfo,
Expand All @@ -148,7 +147,6 @@ ExecutionStrategyParameters transform(ExecutionStepInfo executionStepInfo,

@Internal
ExecutionStrategyParameters transform(ExecutionStepInfo executionStepInfo,
NonNullableFieldValidator nonNullableFieldValidator,
ResultPath path,
Object localContext,
Object source) {
Expand All @@ -165,7 +163,6 @@ ExecutionStrategyParameters transform(ExecutionStepInfo executionStepInfo,

@Internal
ExecutionStrategyParameters transform(ExecutionStepInfo executionStepInfo,
NonNullableFieldValidator nonNullableFieldValidator,
Object localContext,
Object source) {
return new ExecutionStrategyParameters(executionStepInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
public class NonNullableFieldValidator {

private final ExecutionContext executionContext;
private final ExecutionStepInfo executionStepInfo;

public NonNullableFieldValidator(ExecutionContext executionContext, ExecutionStepInfo executionStepInfo) {
public NonNullableFieldValidator(ExecutionContext executionContext) {
this.executionContext = executionContext;
this.executionStepInfo = executionStepInfo;
}

/**
* Called to check that a value is non null if the type requires it to be non null
* Called to check that a value is non-null if the type requires it to be non null
*
* @param parameters the execution strategy parameters
* @param result the result to check
Expand All @@ -34,6 +32,7 @@ public NonNullableFieldValidator(ExecutionContext executionContext, ExecutionSte
*/
public <T> T validate(ExecutionStrategyParameters parameters, T result) throws NonNullableFieldWasNullException {
if (result == null) {
ExecutionStepInfo executionStepInfo = parameters.getExecutionStepInfo();
if (executionStepInfo.isNonNullType()) {
// see https://spec.graphql.org/October2021/#sec-Errors-and-Non-Nullability
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private boolean keepOrdered(GraphQLContext graphQLContext) {
*/

private CompletableFuture<Publisher<Object>> createSourceEventStream(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
ExecutionStrategyParameters newParameters = firstFieldOfSubscriptionSelection(parameters);
ExecutionStrategyParameters newParameters = firstFieldOfSubscriptionSelection(executionContext,parameters);

CompletableFuture<FetchedValue> fieldFetched = Async.toCompletableFuture(fetchField(executionContext, newParameters));
return fieldFetched.thenApply(fetchedValue -> {
Expand Down Expand Up @@ -139,7 +139,7 @@ private CompletableFuture<ExecutionResult> executeSubscriptionEvent(ExecutionCon
.root(eventPayload)
.resetErrors()
);
ExecutionStrategyParameters newParameters = firstFieldOfSubscriptionSelection(parameters);
ExecutionStrategyParameters newParameters = firstFieldOfSubscriptionSelection(newExecutionContext, parameters);
ExecutionStepInfo subscribedFieldStepInfo = createSubscribedFieldStepInfo(executionContext, newParameters);

InstrumentationFieldParameters i13nFieldParameters = new InstrumentationFieldParameters(executionContext, () -> subscribedFieldStepInfo);
Expand Down Expand Up @@ -179,12 +179,14 @@ private String getRootFieldName(ExecutionStrategyParameters parameters) {
return rootField.getResultKey();
}

private ExecutionStrategyParameters firstFieldOfSubscriptionSelection(ExecutionStrategyParameters parameters) {
private ExecutionStrategyParameters firstFieldOfSubscriptionSelection(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
MergedSelectionSet fields = parameters.getFields();
MergedField firstField = fields.getSubField(fields.getKeys().get(0));

ResultPath fieldPath = parameters.getPath().segment(mkNameForPath(firstField.getSingleField()));
return parameters.transform(firstField,fieldPath);
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext);
return parameters.transform(builder -> builder
.field(firstField).path(fieldPath).nonNullFieldValidator(nonNullableFieldValidator));
}

private ExecutionStepInfo createSubscribedFieldStepInfo(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ abstract class AsyncExecutionStrategyTest extends Specification {
.newParameters()
.executionStepInfo(typeInfo)
.fields(mergedSelectionSet(['hello': mergedField([Field.newField('hello').build()]), 'hello2': mergedField([Field.newField('hello2').build()])]))
.nonNullFieldValidator(new NonNullableFieldValidator(executionContext))
.build()

AsyncExecutionStrategy asyncExecutionStrategy = new AsyncExecutionStrategy()
Expand Down Expand Up @@ -160,6 +161,7 @@ abstract class AsyncExecutionStrategyTest extends Specification {
.newParameters()
.executionStepInfo(typeInfo)
.fields(mergedSelectionSet(['hello': mergedField([Field.newField('hello').build()]), 'hello2': mergedField([Field.newField('hello2').build()])]))
.nonNullFieldValidator(new NonNullableFieldValidator(executionContext))
.build()

AsyncExecutionStrategy asyncExecutionStrategy = new AsyncExecutionStrategy()
Expand Down Expand Up @@ -205,6 +207,7 @@ abstract class AsyncExecutionStrategyTest extends Specification {
.newParameters()
.executionStepInfo(typeInfo)
.fields(mergedSelectionSet(['hello': mergedField([Field.newField('hello').build()]), 'hello2': mergedField([Field.newField('hello2').build()])]))
.nonNullFieldValidator(new NonNullableFieldValidator(executionContext))
.build()

AsyncExecutionStrategy asyncExecutionStrategy = new AsyncExecutionStrategy()
Expand Down Expand Up @@ -249,6 +252,7 @@ abstract class AsyncExecutionStrategyTest extends Specification {
.newParameters()
.executionStepInfo(typeInfo)
.fields(mergedSelectionSet(['hello': mergedField([Field.newField('hello').build()]), 'hello2': mergedField([Field.newField('hello2').build()])]))
.nonNullFieldValidator(new NonNullableFieldValidator(executionContext))
.build()

AsyncExecutionStrategy asyncExecutionStrategy = new AsyncExecutionStrategy()
Expand Down Expand Up @@ -312,6 +316,7 @@ abstract class AsyncExecutionStrategyTest extends Specification {
.newParameters()
.executionStepInfo(typeInfo)
.fields(mergedSelectionSet(['hello': mergedField([new Field('hello')]), 'hello2': mergedField([new Field('hello2')])]))
.nonNullFieldValidator(new NonNullableFieldValidator(executionContext))
.build()

AsyncExecutionStrategy asyncExecutionStrategy = new AsyncExecutionStrategy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class AsyncSerialExecutionStrategyTest extends Specification {
.newParameters()
.executionStepInfo(typeInfo)
.fields(mergedSelectionSet(['hello': mergedField(new Field('hello')), 'hello2': mergedField(new Field('hello2')), 'hello3': mergedField(new Field('hello3'))]))
.nonNullFieldValidator(new NonNullableFieldValidator(executionContext))
.build()

AsyncSerialExecutionStrategy strategy = new AsyncSerialExecutionStrategy()
Expand Down Expand Up @@ -163,6 +164,7 @@ class AsyncSerialExecutionStrategyTest extends Specification {
.newParameters()
.executionStepInfo(typeInfo)
.fields(mergedSelectionSet(['hello': mergedField(new Field('hello')), 'hello2': mergedField(new Field('hello2')), 'hello3': mergedField(new Field('hello3'))]))
.nonNullFieldValidator(new NonNullableFieldValidator(executionContext))
.build()

AsyncSerialExecutionStrategy strategy = new AsyncSerialExecutionStrategy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class ExecutionStrategyParametersTest extends Specification {

def "ExecutionParameters can be transformed"() {
given:
def executionContext = Mock(ExecutionContext)
def parameters = newParameters()
.executionStepInfo(newExecutionStepInfo().type(GraphQLString))
.source(new Object())
.localContext("localContext")
.nonNullFieldValidator(new NonNullableFieldValidator(executionContext))
.fields(mergedSelectionSet("a": []))
.build()

Expand Down
Loading
Loading