Skip to content

Commit 17e7ccd

Browse files
committed
This adds support for QueryAppliedDirective on operations and documents - made it a supplier for performance
1 parent 8f049fe commit 17e7ccd

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSche
111111
ResponseMapFactory responseMapFactory = GraphQL.unusualConfiguration(graphQLContext)
112112
.responseMapFactory().getOr(ResponseMapFactory.DEFAULT);
113113

114-
Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> operationDirectives = operationDirectivesResolver
115-
.resolveDirectives(document, graphQLSchema, coercedVariables, graphQLContext, locale);
114+
Supplier<Map<OperationDefinition, ImmutableList<QueryAppliedDirective>>> operationDirectives = FpKit.interThreadMemoize(() ->
115+
operationDirectivesResolver.resolveDirectives(document, graphQLSchema, coercedVariables, graphQLContext, locale));
116116

117117
ExecutionContext executionContext = newExecutionContextBuilder()
118118
.instrumentation(instrumentation)

src/main/java/graphql/execution/ExecutionContext.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.function.Consumer;
3737
import java.util.function.Supplier;
3838

39+
import static graphql.normalized.ExecutableNormalizedOperationFactory.*;
40+
3941
@SuppressWarnings("TypeParameterUnusedInFormals")
4042
@PublicApi
4143
public class ExecutionContext {
@@ -75,8 +77,8 @@ public class ExecutionContext {
7577
private final ResultNodesInfo resultNodesInfo = new ResultNodesInfo();
7678
private final EngineRunningState engineRunningState;
7779

78-
private final Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> allOperationsDirectives;
79-
private final Map<String, ImmutableList<QueryAppliedDirective>> operationDirectives;
80+
private final Supplier<Map<OperationDefinition, ImmutableList<QueryAppliedDirective>>> allOperationsDirectives;
81+
private final Supplier<Map<String, ImmutableList<QueryAppliedDirective>>> operationDirectives;
8082
private final Profiler profiler;
8183

8284
ExecutionContext(ExecutionContextBuilder builder) {
@@ -103,13 +105,27 @@ public class ExecutionContext {
103105
this.localContext = builder.localContext;
104106
this.executionInput = builder.executionInput;
105107
this.dataLoaderDispatcherStrategy = builder.dataLoaderDispatcherStrategy;
106-
this.queryTree = FpKit.interThreadMemoize(() -> ExecutableNormalizedOperationFactory.createExecutableNormalizedOperation(graphQLSchema, operationDefinition, fragmentsByName, coercedVariables));
107108
this.propagateErrorsOnNonNullContractFailure = builder.propagateErrorsOnNonNullContractFailure;
108109
this.engineRunningState = builder.engineRunningState;
109110
this.profiler = builder.profiler;
110-
this.allOperationsDirectives = builder.opDirectivesMap;
111-
List<QueryAppliedDirective> list = allOperationsDirectives.get(getOperationDefinition());
112-
this.operationDirectives = OperationDirectivesResolver.toAppliedDirectivesByName(list);
111+
// lazy loading for performance
112+
this.queryTree = mkExecutableNormalizedOperation();
113+
this.allOperationsDirectives = builder.allOperationsDirectives;
114+
this.operationDirectives = mkOpDirectives(builder.allOperationsDirectives);
115+
}
116+
117+
private Supplier<ExecutableNormalizedOperation> mkExecutableNormalizedOperation() {
118+
return FpKit.interThreadMemoize(() -> {
119+
Options options = Options.defaultOptions().graphQLContext(graphQLContext).locale(locale);
120+
return createExecutableNormalizedOperation(graphQLSchema, operationDefinition, fragmentsByName, coercedVariables, options);
121+
});
122+
}
123+
124+
private Supplier<Map<String, ImmutableList<QueryAppliedDirective>>> mkOpDirectives(Supplier<Map<OperationDefinition, ImmutableList<QueryAppliedDirective>>> allOperationsDirectives) {
125+
return FpKit.interThreadMemoize(() -> {
126+
List<QueryAppliedDirective> list = allOperationsDirectives.get().get(operationDefinition);
127+
return OperationDirectivesResolver.toAppliedDirectivesByName(list);
128+
});
113129
}
114130

115131
public ExecutionId getExecutionId() {
@@ -148,15 +164,15 @@ public OperationDefinition getOperationDefinition() {
148164
* @return the map of {@link QueryAppliedDirective}s by name that were on this executing operation
149165
*/
150166
public Map<String, ImmutableList<QueryAppliedDirective>> getOperationDirectives() {
151-
return operationDirectives;
167+
return operationDirectives.get();
152168
}
153169

154170
/**
155171
* @return the map of all the {@link QueryAppliedDirective}s that were on the {@link Document} including
156172
* {@link OperationDefinition}s that are not currently executing.
157173
*/
158174
public Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> getAllOperationDirectives() {
159-
return allOperationsDirectives;
175+
return allOperationsDirectives.get();
160176
}
161177

162178
public CoercedVariables getCoercedVariables() {

src/main/java/graphql/execution/ExecutionContextBuilder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.jspecify.annotations.Nullable;
2222

2323
import java.util.Collections;
24-
import java.util.List;
2524
import java.util.Locale;
2625
import java.util.Map;
2726
import java.util.function.Supplier;
@@ -58,7 +57,7 @@ public class ExecutionContextBuilder {
5857
EngineRunningState engineRunningState;
5958
ResponseMapFactory responseMapFactory = ResponseMapFactory.DEFAULT;
6059
Profiler profiler;
61-
Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> opDirectivesMap = Collections.emptyMap();
60+
Supplier<Map<OperationDefinition, ImmutableList<QueryAppliedDirective>>> allOperationsDirectives = Collections::emptyMap;
6261

6362
/**
6463
* @return a new builder of {@link graphql.execution.ExecutionContext}s
@@ -261,8 +260,8 @@ public ExecutionContextBuilder profiler(Profiler profiler) {
261260
return this;
262261
}
263262

264-
public ExecutionContextBuilder operationDirectives(Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> opDirectivesMap) {
265-
this.opDirectivesMap = opDirectivesMap;
263+
public ExecutionContextBuilder operationDirectives(Supplier<Map<OperationDefinition, ImmutableList<QueryAppliedDirective>>> allOperationsDirectives) {
264+
this.allOperationsDirectives = allOperationsDirectives;
266265
return this;
267266
}
268267

0 commit comments

Comments
 (0)