Skip to content

Commit 2e7c4fc

Browse files
committed
This adds support for QueryAppliedDirective on operations and documents - use Immutable signature
1 parent 1951ed6 commit 2e7c4fc

File tree

8 files changed

+37
-38
lines changed

8 files changed

+37
-38
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql.execution;
22

33

4+
import com.google.common.collect.ImmutableList;
45
import graphql.Directives;
56
import graphql.EngineRunningState;
67
import graphql.ExecutionInput;
@@ -110,7 +111,7 @@ public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSche
110111
ResponseMapFactory responseMapFactory = GraphQL.unusualConfiguration(graphQLContext)
111112
.responseMapFactory().getOr(ResponseMapFactory.DEFAULT);
112113

113-
Map<OperationDefinition, List<QueryAppliedDirective>> opDirectivesMap = operationDirectivesResolver.resolveDirectives(document, graphQLSchema, coercedVariables, graphQLContext, locale);
114+
Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> opDirectivesMap = operationDirectivesResolver.resolveDirectives(document, graphQLSchema, coercedVariables, graphQLContext, locale);
114115

115116
ExecutionContext executionContext = newExecutionContextBuilder()
116117
.instrumentation(instrumentation)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class ExecutionContext {
7575
private final ResultNodesInfo resultNodesInfo = new ResultNodesInfo();
7676
private final EngineRunningState engineRunningState;
7777

78-
private final Map<OperationDefinition, List<QueryAppliedDirective>> opDirectivesMap;
78+
private final Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> opDirectivesMap;
7979
private final Profiler profiler;
8080

8181
ExecutionContext(ExecutionContextBuilder builder) {
@@ -144,7 +144,7 @@ public OperationDefinition getOperationDefinition() {
144144
/**
145145
* @return the map of {@link QueryAppliedDirective}s by name that were on this executing operation
146146
*/
147-
public Map<String, List<QueryAppliedDirective>> getOperationDirectives() {
147+
public Map<String, ImmutableList<QueryAppliedDirective>> getOperationDirectives() {
148148
List<QueryAppliedDirective> list = opDirectivesMap.get(getOperationDefinition());
149149
return OperationDirectivesResolver.toAppliedDirectivesByName(list);
150150
}
@@ -153,7 +153,7 @@ public Map<String, List<QueryAppliedDirective>> getOperationDirectives() {
153153
* @return the map of all the {@link QueryAppliedDirective}s that were on the {@link Document} including
154154
* {@link OperationDefinition}s that are not currently executing.
155155
*/
156-
public Map<OperationDefinition, List<QueryAppliedDirective>> getAllOperationDirectives() {
156+
public Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> getAllOperationDirectives() {
157157
return opDirectivesMap;
158158
}
159159

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class ExecutionContextBuilder {
5858
EngineRunningState engineRunningState;
5959
ResponseMapFactory responseMapFactory = ResponseMapFactory.DEFAULT;
6060
Profiler profiler;
61-
Map<OperationDefinition, List<QueryAppliedDirective>> opDirectivesMap = Collections.emptyMap();
61+
Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> opDirectivesMap = Collections.emptyMap();
6262

6363
/**
6464
* @return a new builder of {@link graphql.execution.ExecutionContext}s
@@ -261,7 +261,7 @@ public ExecutionContextBuilder profiler(Profiler profiler) {
261261
return this;
262262
}
263263

264-
public ExecutionContextBuilder operationDirectives(Map<OperationDefinition, List<QueryAppliedDirective>> opDirectivesMap) {
264+
public ExecutionContextBuilder operationDirectives(Map<OperationDefinition, ImmutableList<QueryAppliedDirective>> opDirectivesMap) {
265265
this.opDirectivesMap = opDirectivesMap;
266266
return this;
267267
}

src/main/java/graphql/execution/directives/DirectivesResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.BiMap;
44
import com.google.common.collect.HashBiMap;
55
import com.google.common.collect.ImmutableBiMap;
6+
import com.google.common.collect.ImmutableList;
67
import graphql.GraphQLContext;
78
import graphql.Internal;
89
import graphql.collect.ImmutableKit;
@@ -63,7 +64,7 @@ private void buildArguments(GraphQLDirective.Builder directiveBuilder,
6364
});
6465
}
6566

66-
public List<QueryAppliedDirective> toAppliedDirectives(List<Directive> directives, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
67+
public ImmutableList<QueryAppliedDirective> toAppliedDirectives(List<Directive> directives, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
6768
BiMap<GraphQLDirective, Directive> directivesMap = resolveDirectives(directives, schema, variables, graphQLContext, locale);
6869
return ImmutableKit.map(directivesMap.keySet(), this::toAppliedDirective);
6970
}
Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package graphql.execution.directives;
22

3+
import com.google.common.collect.ImmutableList;
34
import com.google.common.collect.ImmutableMap;
45
import graphql.GraphQLContext;
56
import graphql.Internal;
67
import graphql.execution.CoercedVariables;
78
import graphql.language.Document;
89
import graphql.language.OperationDefinition;
910
import graphql.schema.GraphQLSchema;
11+
import graphql.util.FpKit;
1012
import org.jspecify.annotations.NullMarked;
1113

12-
import java.util.ArrayList;
13-
import java.util.LinkedHashMap;
1414
import java.util.List;
1515
import java.util.Locale;
1616
import java.util.Map;
@@ -21,16 +21,15 @@ public class OperationDirectivesResolver {
2121

2222
private final DirectivesResolver directivesResolver = new DirectivesResolver();
2323

24-
public Map<OperationDefinition, List<QueryAppliedDirective>> resolveDirectives(Document document, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
25-
Map<OperationDefinition, List<QueryAppliedDirective>> map = new LinkedHashMap<>();
26-
List<OperationDefinition> operations = document.getDefinitionsOfType(OperationDefinition.class);
27-
for (OperationDefinition operationDefinition : operations) {
28-
map.put(operationDefinition, resolveDirectives(operationDefinition, schema, variables, graphQLContext, locale));
24+
public ImmutableMap<OperationDefinition, ImmutableList<QueryAppliedDirective>> resolveDirectives(Document document, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
25+
ImmutableMap.Builder<OperationDefinition, ImmutableList<QueryAppliedDirective>> builder = ImmutableMap.builder();
26+
for (OperationDefinition operationDefinition : document.getDefinitionsOfType(OperationDefinition.class)) {
27+
builder.put(operationDefinition, resolveDirectives(operationDefinition, schema, variables, graphQLContext, locale));
2928
}
30-
return ImmutableMap.copyOf(map);
29+
return builder.build();
3130
}
3231

33-
public List<QueryAppliedDirective> resolveDirectives(OperationDefinition operationDefinition, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
32+
public ImmutableList<QueryAppliedDirective> resolveDirectives(OperationDefinition operationDefinition, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
3433
return directivesResolver.toAppliedDirectives(
3534
operationDefinition.getDirectives(),
3635
schema,
@@ -40,18 +39,14 @@ public List<QueryAppliedDirective> resolveDirectives(OperationDefinition operati
4039
);
4140
}
4241

43-
public Map<String, List<QueryAppliedDirective>> resolveDirectivesByName(OperationDefinition operationDefinition, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
42+
public ImmutableMap<String, ImmutableList<QueryAppliedDirective>> resolveDirectivesByName(OperationDefinition operationDefinition, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
4443
List<QueryAppliedDirective> list = resolveDirectives(operationDefinition, schema, variables, graphQLContext, locale);
4544
return toAppliedDirectivesByName(list);
4645
}
4746

48-
public static Map<String, List<QueryAppliedDirective>> toAppliedDirectivesByName(List<QueryAppliedDirective> queryAppliedDirectives) {
49-
Map<String, List<QueryAppliedDirective>> map = new LinkedHashMap<>();
50-
for (QueryAppliedDirective queryAppliedDirective : queryAppliedDirectives) {
51-
List<QueryAppliedDirective> list = map.computeIfAbsent(queryAppliedDirective.getName(), k -> new ArrayList<>());
52-
list.add(queryAppliedDirective);
53-
}
54-
return ImmutableMap.copyOf(map);
47+
public static ImmutableMap<String, ImmutableList<QueryAppliedDirective>> toAppliedDirectivesByName(List<QueryAppliedDirective> queryAppliedDirectives) {
48+
Map<String, ImmutableList<QueryAppliedDirective>> immutableListMap = FpKit.groupingBy(queryAppliedDirectives, QueryAppliedDirective::getName);
49+
return ImmutableMap.copyOf(immutableListMap);
5550
}
5651

5752
}

src/main/java/graphql/normalized/ExecutableNormalizedOperation.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.normalized;
22

3+
import com.google.common.collect.ImmutableList;
34
import com.google.common.collect.ImmutableListMultimap;
45
import graphql.Assert;
56
import graphql.PublicApi;
@@ -26,7 +27,7 @@
2627
@PublicApi
2728
public class ExecutableNormalizedOperation {
2829
private final OperationDefinition.Operation operation;
29-
private final Map<String, List<QueryAppliedDirective>> operationDirectives;
30+
private final Map<String, ImmutableList<QueryAppliedDirective>> operationDirectives;
3031
private final String operationName;
3132
private final List<ExecutableNormalizedField> topLevelFields;
3233
private final ImmutableListMultimap<Field, ExecutableNormalizedField> fieldToNormalizedField;
@@ -39,7 +40,7 @@ public class ExecutableNormalizedOperation {
3940
public ExecutableNormalizedOperation(
4041
OperationDefinition.Operation operation,
4142
String operationName,
42-
Map<String, List<QueryAppliedDirective>> operationDirectives,
43+
Map<String, ImmutableList<QueryAppliedDirective>> operationDirectives,
4344
List<ExecutableNormalizedField> topLevelFields,
4445
ImmutableListMultimap<Field, ExecutableNormalizedField> fieldToNormalizedField,
4546
Map<ExecutableNormalizedField, MergedField> normalizedFieldToMergedField,
@@ -83,7 +84,7 @@ public String getOperationName() {
8384
*
8485
* @return the directives that are on this operation itself.
8586
*/
86-
public Map<String, List<QueryAppliedDirective>> getOperationDirectives() {
87+
public Map<String, ImmutableList<QueryAppliedDirective>> getOperationDirectives() {
8788
return operationDirectives;
8889
}
8990

src/main/java/graphql/normalized/ExecutableNormalizedOperationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ private ExecutableNormalizedOperation createNormalizedQueryImpl() {
491491
ENFMerger.merge(possibleMerger.parent, childrenWithSameResultKey, graphQLSchema, options.deferSupport);
492492
}
493493

494-
Map<String, List<QueryAppliedDirective>> operationDirectives = directivesResolver.resolveDirectivesByName(operationDefinition,
494+
Map<String, ImmutableList<QueryAppliedDirective>> operationDirectives = directivesResolver.resolveDirectivesByName(operationDefinition,
495495
graphQLSchema,
496496
coercedVariableValues,
497497
options.graphQLContext,

src/test/groovy/graphql/execution/directives/OperationDirectivesResolverTest.groovy

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.execution.directives
22

3+
import com.google.common.collect.ImmutableList
34
import graphql.ExecutionResult
45
import graphql.GraphQL
56
import graphql.GraphQLContext
@@ -57,7 +58,7 @@ class OperationDirectivesResolverTest extends Specification {
5758
""")
5859

5960
when:
60-
Map<OperationDefinition, List<QueryAppliedDirective>> resolveDirectives = new OperationDirectivesResolver()
61+
def resolveDirectives = new OperationDirectivesResolver()
6162
.resolveDirectives(document, schema, CoercedVariables.emptyVariables(), GraphQLContext.getDefault(), Locale.getDefault())
6263

6364
def data = resolveDirectives.collectEntries { operation, directives ->
@@ -82,7 +83,7 @@ class OperationDirectivesResolverTest extends Specification {
8283
def operationDefinition = extractOp(document)
8384

8485
when:
85-
Map<String, List<QueryAppliedDirective>> resolveDirectives = new OperationDirectivesResolver()
86+
def resolveDirectives = new OperationDirectivesResolver()
8687
.resolveDirectivesByName(operationDefinition, schema, CoercedVariables.emptyVariables(), GraphQLContext.getDefault(), Locale.getDefault())
8788

8889
then:
@@ -102,7 +103,7 @@ class OperationDirectivesResolverTest extends Specification {
102103
def operationDefinition = extractOp(document)
103104

104105
when:
105-
Map<String, List<QueryAppliedDirective>> resolveDirectives = new OperationDirectivesResolver()
106+
def resolveDirectives = new OperationDirectivesResolver()
106107
.resolveDirectivesByName(operationDefinition, schema, CoercedVariables.emptyVariables(), GraphQLContext.getDefault(), Locale.getDefault())
107108

108109
then:
@@ -116,11 +117,11 @@ class OperationDirectivesResolverTest extends Specification {
116117

117118

118119
private static boolean timeoutAsserts(QueryAppliedDirective directive, Integer value) {
119-
directive.name == "timeout"
120-
directive.arguments.size() == 1
121-
directive.arguments[0].name == "ms"
122-
(directive.arguments[0].type as GraphQLScalarType).name == "Int"
123-
directive.arguments[0].value == value
120+
assert directive.name == "timeout"
121+
assert directive.arguments.size() == 1
122+
assert directive.arguments[0].name == "ms"
123+
assert (directive.arguments[0].type as GraphQLScalarType).name == "Int"
124+
assert directive.arguments[0].value == value
124125
true
125126
}
126127

@@ -145,7 +146,7 @@ class OperationDirectivesResolverTest extends Specification {
145146
""")
146147

147148
then:
148-
Map<String, List<QueryAppliedDirective>> resolveDirectives = executionContext.getOperationDirectives()
149+
def resolveDirectives = executionContext.getOperationDirectives()
149150

150151
commonIntegrationAsserts(resolveDirectives)
151152

@@ -158,7 +159,7 @@ class OperationDirectivesResolverTest extends Specification {
158159

159160
}
160161

161-
private static boolean commonIntegrationAsserts(Map<String, List<QueryAppliedDirective>> resolveDirectives) {
162+
private static boolean commonIntegrationAsserts(Map<String, ImmutableList<QueryAppliedDirective>> resolveDirectives) {
162163
assert resolveDirectives.size() == 4
163164
def directives = resolveDirectives["timeout"]
164165
assert directives.size() == 1

0 commit comments

Comments
 (0)