Skip to content

Commit baa2452

Browse files
Intern field names (#3489)
* Not quite finished - but has directive filtering * Directive definitions now have a predicate and fixed up tests * This removes the CompletableFuture signature from InstrumentationContext * This removes the CompletableFuture signature from InstrumentationContext - javadoc * Don't build a builder object and then turn it into the actual object * Don't build a builder object and then turn it into the actual object - package level * Intern field names --------- Co-authored-by: Brad Baker <[email protected]> Co-authored-by: Brad Baker <[email protected]>
1 parent 53408fd commit baa2452

27 files changed

+265
-225
lines changed

src/main/java/graphql/GraphQL.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,17 +420,16 @@ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionI
420420
InstrumentationExecutionParameters inputInstrumentationParameters = new InstrumentationExecutionParameters(executionInputWithId, this.graphQLSchema, instrumentationState);
421421
ExecutionInput instrumentedExecutionInput = instrumentation.instrumentExecutionInput(executionInputWithId, inputInstrumentationParameters, instrumentationState);
422422

423-
CompletableFuture<ExecutionResult> beginExecutionCF = new CompletableFuture<>();
424423
InstrumentationExecutionParameters instrumentationParameters = new InstrumentationExecutionParameters(instrumentedExecutionInput, this.graphQLSchema, instrumentationState);
425424
InstrumentationContext<ExecutionResult> executionInstrumentation = nonNullCtx(instrumentation.beginExecution(instrumentationParameters, instrumentationState));
426-
executionInstrumentation.onDispatched(beginExecutionCF);
425+
executionInstrumentation.onDispatched();
427426

428427
GraphQLSchema graphQLSchema = instrumentation.instrumentSchema(this.graphQLSchema, instrumentationParameters, instrumentationState);
429428

430429
CompletableFuture<ExecutionResult> executionResult = parseValidateAndExecute(instrumentedExecutionInput, graphQLSchema, instrumentationState);
431430
//
432431
// finish up instrumentation
433-
executionResult = executionResult.whenComplete(completeInstrumentationCtxCF(executionInstrumentation, beginExecutionCF));
432+
executionResult = executionResult.whenComplete(completeInstrumentationCtxCF(executionInstrumentation));
434433
//
435434
// allow instrumentation to tweak the result
436435
executionResult = executionResult.thenCompose(result -> instrumentation.instrumentExecutionResult(result, instrumentationParameters, instrumentationState));
@@ -507,15 +506,13 @@ private PreparsedDocumentEntry parseAndValidate(AtomicReference<ExecutionInput>
507506
private ParseAndValidateResult parse(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
508507
InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState);
509508
InstrumentationContext<Document> parseInstrumentationCtx = nonNullCtx(instrumentation.beginParse(parameters, instrumentationState));
510-
CompletableFuture<Document> documentCF = new CompletableFuture<>();
511-
parseInstrumentationCtx.onDispatched(documentCF);
509+
parseInstrumentationCtx.onDispatched();
512510

513511
ParseAndValidateResult parseResult = ParseAndValidate.parse(executionInput);
514512
if (parseResult.isFailure()) {
515513
parseInstrumentationCtx.onCompleted(null, parseResult.getSyntaxException());
516514
return parseResult;
517515
} else {
518-
documentCF.complete(parseResult.getDocument());
519516
parseInstrumentationCtx.onCompleted(parseResult.getDocument(), null);
520517

521518
DocumentAndVariables documentAndVariables = parseResult.getDocumentAndVariables();
@@ -527,15 +524,13 @@ private ParseAndValidateResult parse(ExecutionInput executionInput, GraphQLSchem
527524

528525
private List<ValidationError> validate(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
529526
InstrumentationContext<List<ValidationError>> validationCtx = nonNullCtx(instrumentation.beginValidation(new InstrumentationValidationParameters(executionInput, document, graphQLSchema, instrumentationState), instrumentationState));
530-
CompletableFuture<List<ValidationError>> cf = new CompletableFuture<>();
531-
validationCtx.onDispatched(cf);
527+
validationCtx.onDispatched();
532528

533529
Predicate<Class<?>> validationRulePredicate = executionInput.getGraphQLContext().getOrDefault(ParseAndValidate.INTERNAL_VALIDATION_PREDICATE_HINT, r -> true);
534530
Locale locale = executionInput.getLocale() != null ? executionInput.getLocale() : Locale.getDefault();
535531
List<ValidationError> validationErrors = ParseAndValidate.validate(graphQLSchema, document, validationRulePredicate, locale);
536532

537533
validationCtx.onCompleted(validationErrors, null);
538-
cf.complete(validationErrors);
539534
return validationErrors;
540535
}
541536

src/main/java/graphql/execution/AsyncExecutionStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
5050
Async.CombinedBuilder<FieldValueInfo> futures = getAsyncFieldValueInfo(executionContext, parameters, deferredExecutionSupport);
5151

5252
CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
53-
executionStrategyCtx.onDispatched(overallResult);
53+
executionStrategyCtx.onDispatched();
5454

5555
futures.await().whenComplete((completeValueInfos, throwable) -> {
5656
List<String> fieldsExecutedOnInitialResult = deferredExecutionSupport.getNonDeferredFieldNames(fieldNames);

src/main/java/graphql/execution/AsyncSerialExecutionStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
4949
});
5050

5151
CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
52-
executionStrategyCtx.onDispatched(overallResult);
52+
executionStrategyCtx.onDispatched();
5353

5454
resultsFuture.whenComplete(handleResults(executionContext, fieldNames, overallResult));
5555
overallResult.whenComplete(executionStrategyCtx::onCompleted);

src/main/java/graphql/execution/Execution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
134134
ExecutionResult executionResult = new ExecutionResultImpl(Collections.singletonList((GraphQLError) rte));
135135
CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture(executionResult);
136136

137-
executeOperationCtx.onDispatched(resultCompletableFuture);
137+
executeOperationCtx.onDispatched();
138138
executeOperationCtx.onCompleted(executionResult, rte);
139139
return resultCompletableFuture;
140140
}
@@ -189,7 +189,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
189189
}
190190

191191
// note this happens NOW - not when the result completes
192-
executeOperationCtx.onDispatched(result);
192+
executeOperationCtx.onDispatched();
193193

194194
// fill out extensions if we have them
195195
result = result.thenApply(er -> mergeExtensionsBuilderIfPresent(er, graphQLContext));

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

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ protected CompletableFuture<Map<String, Object>> executeObject(ExecutionContext
211211
Async.CombinedBuilder<FieldValueInfo> resolvedFieldFutures = getAsyncFieldValueInfo(executionContext, parameters, deferredExecutionSupport);
212212

213213
CompletableFuture<Map<String, Object>> overallResult = new CompletableFuture<>();
214-
resolveObjectCtx.onDispatched(overallResult);
214+
resolveObjectCtx.onDispatched();
215215

216216
resolvedFieldFutures.await().whenComplete((completeValueInfos, throwable) -> {
217217
List<String> fieldsExecutedOnInitialResult = deferredExecutionSupport.getNonDeferredFieldNames(fieldNames);
@@ -354,7 +354,7 @@ protected CompletableFuture<FieldValueInfo> resolveFieldWithInfo(ExecutionContex
354354

355355
CompletableFuture<Object> fieldValueFuture = result.thenCompose(FieldValueInfo::getFieldValueFuture);
356356

357-
fieldCtx.onDispatched(fieldValueFuture);
357+
fieldCtx.onDispatched();
358358
fieldValueFuture.whenComplete(fieldCtx::onCompleted);
359359
return result;
360360
}
@@ -425,7 +425,7 @@ protected CompletableFuture<FetchedValue> fetchField(ExecutionContext executionC
425425
dataFetcher = executionContext.getDataLoaderDispatcherStrategy().modifyDataFetcher(dataFetcher);
426426
CompletableFuture<Object> fetchedValue = invokeDataFetcher(executionContext, parameters, fieldDef, dataFetchingEnvironment, dataFetcher);
427427
executionContext.getDataLoaderDispatcherStrategy().fieldFetched(executionContext, parameters, dataFetcher, fetchedValue);
428-
fetchCtx.onDispatched(fetchedValue);
428+
fetchCtx.onDispatched();
429429
return fetchedValue
430430
.handle((result, exception) -> {
431431
fetchCtx.onCompleted(result, exception);
@@ -475,18 +475,11 @@ protected FetchedValue unboxPossibleDataFetcherResult(ExecutionContext execution
475475
// if the field returns nothing then they get the context of their parent field
476476
localContext = parameters.getLocalContext();
477477
}
478-
return FetchedValue.newFetchedValue()
479-
.fetchedValue(executionContext.getValueUnboxer().unbox(dataFetcherResult.getData()))
480-
.rawFetchedValue(dataFetcherResult.getData())
481-
.errors(dataFetcherResult.getErrors())
482-
.localContext(localContext)
483-
.build();
478+
Object unBoxedValue = executionContext.getValueUnboxer().unbox(dataFetcherResult.getData());
479+
return new FetchedValue(unBoxedValue, dataFetcherResult.getErrors(), localContext);
484480
} else {
485-
return FetchedValue.newFetchedValue()
486-
.fetchedValue(executionContext.getValueUnboxer().unbox(result))
487-
.rawFetchedValue(result)
488-
.localContext(parameters.getLocalContext())
489-
.build();
481+
Object unBoxedValue = executionContext.getValueUnboxer().unbox(result);
482+
return new FetchedValue(unBoxedValue, ImmutableList.of(), parameters.getLocalContext());
490483
}
491484
}
492485

@@ -575,7 +568,7 @@ protected FieldValueInfo completeField(ExecutionContext executionContext, Execut
575568
FieldValueInfo fieldValueInfo = completeValue(executionContext, newParameters);
576569

577570
CompletableFuture<Object> executionResultFuture = fieldValueInfo.getFieldValueFuture();
578-
ctxCompleteField.onDispatched(executionResultFuture);
571+
ctxCompleteField.onDispatched();
579572
executionResultFuture.whenComplete(ctxCompleteField::onCompleted);
580573
return fieldValueInfo;
581574
}
@@ -608,10 +601,10 @@ protected FieldValueInfo completeValue(ExecutionContext executionContext, Execut
608601
return completeValueForList(executionContext, parameters, result);
609602
} else if (isScalar(fieldType)) {
610603
fieldValue = completeValueForScalar(executionContext, parameters, (GraphQLScalarType) fieldType, result);
611-
return FieldValueInfo.newFieldValueInfo(SCALAR).fieldValue(fieldValue).build();
604+
return new FieldValueInfo(SCALAR, fieldValue);
612605
} else if (isEnum(fieldType)) {
613606
fieldValue = completeValueForEnum(executionContext, parameters, (GraphQLEnumType) fieldType, result);
614-
return FieldValueInfo.newFieldValueInfo(ENUM).fieldValue(fieldValue).build();
607+
return new FieldValueInfo(ENUM, fieldValue);
615608
}
616609

617610
// when we are here, we have a complex type: Interface, Union or Object
@@ -628,7 +621,7 @@ protected FieldValueInfo completeValue(ExecutionContext executionContext, Execut
628621
// complete field as null, validating it is nullable
629622
return getFieldValueInfoForNull(parameters);
630623
}
631-
return FieldValueInfo.newFieldValueInfo(OBJECT).fieldValue(fieldValue).build();
624+
return new FieldValueInfo(OBJECT, fieldValue);
632625
}
633626

634627
private void handleUnresolvedTypeProblem(ExecutionContext context, ExecutionStrategyParameters parameters, UnresolvedTypeException e) {
@@ -649,7 +642,7 @@ private void handleUnresolvedTypeProblem(ExecutionContext context, ExecutionStra
649642
*/
650643
private FieldValueInfo getFieldValueInfoForNull(ExecutionStrategyParameters parameters) {
651644
CompletableFuture<Object> fieldValue = completeValueForNull(parameters);
652-
return FieldValueInfo.newFieldValueInfo(NULL).fieldValue(fieldValue).build();
645+
return new FieldValueInfo(NULL, fieldValue);
653646
}
654647

655648
protected CompletableFuture<Object> completeValueForNull(ExecutionStrategyParameters parameters) {
@@ -674,10 +667,10 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
674667
try {
675668
resultIterable = parameters.getNonNullFieldValidator().validate(parameters.getPath(), resultIterable);
676669
} catch (NonNullableFieldWasNullException e) {
677-
return FieldValueInfo.newFieldValueInfo(LIST).fieldValue(exceptionallyCompletedFuture(e)).build();
670+
return new FieldValueInfo(LIST, exceptionallyCompletedFuture(e));
678671
}
679672
if (resultIterable == null) {
680-
return FieldValueInfo.newFieldValueInfo(LIST).fieldValue(completedFuture(null)).build();
673+
return new FieldValueInfo(LIST, completedFuture(null));
681674
}
682675
return completeValueForList(executionContext, parameters, resultIterable);
683676
}
@@ -729,7 +722,7 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
729722
CompletableFuture<List<Object>> resultsFuture = Async.each(fieldValueInfos, FieldValueInfo::getFieldValueFuture);
730723

731724
CompletableFuture<Object> overallResult = new CompletableFuture<>();
732-
completeListCtx.onDispatched(overallResult);
725+
completeListCtx.onDispatched();
733726
overallResult.whenComplete(completeListCtx::onCompleted);
734727

735728
resultsFuture.whenComplete((results, exception) -> {
@@ -742,10 +735,7 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
742735
overallResult.complete(completedResults);
743736
});
744737

745-
return FieldValueInfo.newFieldValueInfo(LIST)
746-
.fieldValue(overallResult)
747-
.fieldValueInfos(fieldValueInfos)
748-
.build();
738+
return new FieldValueInfo(LIST, overallResult, fieldValueInfos);
749739
}
750740

751741
protected <T> void handleValueException(CompletableFuture<T> overallResult, Throwable e, ExecutionContext executionContext) {

src/main/java/graphql/execution/FetchedValue.java

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters;
77

88
import java.util.List;
9-
import java.util.function.Consumer;
109

1110
/**
1211
* Note: This is returned by {@link InstrumentationFieldCompleteParameters#getFetchedValue()}
@@ -15,14 +14,12 @@
1514
@PublicApi
1615
public class FetchedValue {
1716
private final Object fetchedValue;
18-
private final Object rawFetchedValue;
1917
private final Object localContext;
2018
private final ImmutableList<GraphQLError> errors;
2119

22-
private FetchedValue(Object fetchedValue, Object rawFetchedValue, ImmutableList<GraphQLError> errors, Object localContext) {
20+
FetchedValue(Object fetchedValue, List<GraphQLError> errors, Object localContext) {
2321
this.fetchedValue = fetchedValue;
24-
this.rawFetchedValue = rawFetchedValue;
25-
this.errors = errors;
22+
this.errors = ImmutableList.copyOf(errors);
2623
this.localContext = localContext;
2724
}
2825

@@ -33,10 +30,6 @@ public Object getFetchedValue() {
3330
return fetchedValue;
3431
}
3532

36-
public Object getRawFetchedValue() {
37-
return rawFetchedValue;
38-
}
39-
4033
public List<GraphQLError> getErrors() {
4134
return errors;
4235
}
@@ -45,64 +38,13 @@ public Object getLocalContext() {
4538
return localContext;
4639
}
4740

48-
public FetchedValue transform(Consumer<Builder> builderConsumer) {
49-
Builder builder = newFetchedValue(this);
50-
builderConsumer.accept(builder);
51-
return builder.build();
52-
}
53-
5441
@Override
5542
public String toString() {
5643
return "FetchedValue{" +
5744
"fetchedValue=" + fetchedValue +
58-
", rawFetchedValue=" + rawFetchedValue +
5945
", localContext=" + localContext +
6046
", errors=" + errors +
6147
'}';
6248
}
6349

64-
public static Builder newFetchedValue() {
65-
return new Builder();
66-
}
67-
68-
public static Builder newFetchedValue(FetchedValue otherValue) {
69-
return new Builder()
70-
.fetchedValue(otherValue.getFetchedValue())
71-
.rawFetchedValue(otherValue.getRawFetchedValue())
72-
.errors(otherValue.getErrors())
73-
.localContext(otherValue.getLocalContext())
74-
;
75-
}
76-
77-
public static class Builder {
78-
79-
private Object fetchedValue;
80-
private Object rawFetchedValue;
81-
private Object localContext;
82-
private ImmutableList<GraphQLError> errors = ImmutableList.of();
83-
84-
public Builder fetchedValue(Object fetchedValue) {
85-
this.fetchedValue = fetchedValue;
86-
return this;
87-
}
88-
89-
public Builder rawFetchedValue(Object rawFetchedValue) {
90-
this.rawFetchedValue = rawFetchedValue;
91-
return this;
92-
}
93-
94-
public Builder localContext(Object localContext) {
95-
this.localContext = localContext;
96-
return this;
97-
}
98-
99-
public Builder errors(List<GraphQLError> errors) {
100-
this.errors = ImmutableList.copyOf(errors);
101-
return this;
102-
}
103-
104-
public FetchedValue build() {
105-
return new FetchedValue(fetchedValue, rawFetchedValue, errors, localContext);
106-
}
107-
}
10850
}
Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package graphql.execution;
22

3+
import com.google.common.collect.ImmutableList;
34
import graphql.ExecutionResult;
45
import graphql.ExecutionResultImpl;
56
import graphql.PublicApi;
67

7-
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.concurrent.CompletableFuture;
1010

@@ -26,7 +26,11 @@ public enum CompleteValueType {
2626
private final CompletableFuture<Object> fieldValue;
2727
private final List<FieldValueInfo> fieldValueInfos;
2828

29-
private FieldValueInfo(CompleteValueType completeValueType, CompletableFuture<Object> fieldValue, List<FieldValueInfo> fieldValueInfos) {
29+
FieldValueInfo(CompleteValueType completeValueType, CompletableFuture<Object> fieldValue) {
30+
this(completeValueType, fieldValue, ImmutableList.of());
31+
}
32+
33+
FieldValueInfo(CompleteValueType completeValueType, CompletableFuture<Object> fieldValue, List<FieldValueInfo> fieldValueInfos) {
3034
assertNotNull(fieldValueInfos, () -> "fieldValueInfos can't be null");
3135
this.completeValueType = completeValueType;
3236
this.fieldValue = fieldValue;
@@ -37,10 +41,11 @@ public CompleteValueType getCompleteValueType() {
3741
return completeValueType;
3842
}
3943

40-
@Deprecated(since="2023-09-11" )
44+
@Deprecated(since = "2023-09-11")
4145
public CompletableFuture<ExecutionResult> getFieldValue() {
4246
return fieldValue.thenApply(fv -> ExecutionResultImpl.newExecutionResult().data(fv).build());
4347
}
48+
4449
public CompletableFuture<Object> getFieldValueFuture() {
4550
return fieldValue;
4651
}
@@ -49,9 +54,6 @@ public List<FieldValueInfo> getFieldValueInfos() {
4954
return fieldValueInfos;
5055
}
5156

52-
public static Builder newFieldValueInfo(CompleteValueType completeValueType) {
53-
return new Builder(completeValueType);
54-
}
5557

5658
@Override
5759
public String toString() {
@@ -62,34 +64,4 @@ public String toString() {
6264
'}';
6365
}
6466

65-
@SuppressWarnings("unused")
66-
public static class Builder {
67-
private CompleteValueType completeValueType;
68-
private CompletableFuture<Object> fieldValueFuture;
69-
private List<FieldValueInfo> listInfos = new ArrayList<>();
70-
71-
public Builder(CompleteValueType completeValueType) {
72-
this.completeValueType = completeValueType;
73-
}
74-
75-
public Builder completeValueType(CompleteValueType completeValueType) {
76-
this.completeValueType = completeValueType;
77-
return this;
78-
}
79-
80-
public Builder fieldValue(CompletableFuture<Object> executionResultFuture) {
81-
this.fieldValueFuture = executionResultFuture;
82-
return this;
83-
}
84-
85-
public Builder fieldValueInfos(List<FieldValueInfo> listInfos) {
86-
assertNotNull(listInfos, () -> "fieldValueInfos can't be null");
87-
this.listInfos = listInfos;
88-
return this;
89-
}
90-
91-
public FieldValueInfo build() {
92-
return new FieldValueInfo(completeValueType, fieldValueFuture, listInfos);
93-
}
94-
}
9567
}

0 commit comments

Comments
 (0)