Skip to content

Commit 641bea4

Browse files
committed
Tweaked code and fixed test
1 parent 3e23fea commit 641bea4

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import graphql.schema.GraphQLSchema;
1717
import graphql.util.FpKit;
1818
import graphql.util.LockKit;
19+
import org.jetbrains.annotations.Nullable;
1920

2021
import java.util.ArrayList;
2122
import java.util.LinkedHashMap;
@@ -37,6 +38,7 @@ public class QueryDirectivesImpl implements QueryDirectives {
3738
private final MergedField mergedField;
3839
private final GraphQLSchema schema;
3940
private final Map<String, Object> coercedVariables;
41+
@Nullable
4042
private final Map<String, NormalizedInputValue> normalizedVariableValues;
4143
private final GraphQLContext graphQLContext;
4244
private final Locale locale;
@@ -68,22 +70,26 @@ private void computeValuesLazily() {
6870
BiMap<QueryAppliedDirective, GraphQLDirective> gqlDirectiveCounterPartsInverse = gqlDirectiveCounterParts.inverse();
6971
mergedField.getFields().forEach(field -> {
7072
List<Directive> directives = field.getDirectives();
71-
ImmutableList<GraphQLDirective> resolvedDirectives = ImmutableList.copyOf(FpKit.flatList(
72-
directivesResolver
73-
.resolveDirectives(directives, schema, coercedVariables, graphQLContext, locale)
74-
.values()
75-
));
76-
for (int i = 0; i < directives.size(); i++) {
77-
Directive directive = directives.get(i);
73+
Map<String, Directive> astDirectivesByName = FpKit.getByName(directives, Directive::getName);
74+
Map<String, List<GraphQLDirective>> directivesMap = directivesResolver
75+
.resolveDirectives(directives, schema, coercedVariables, graphQLContext, locale);
76+
ImmutableList<GraphQLDirective> resolvedDirectives = ImmutableList.copyOf(
77+
FpKit.flatList(directivesMap.values()));
78+
79+
// build a counterpart map of GraphQLDirective to AST directive
80+
for (int i = 0; i < resolvedDirectives.size(); i++) {
7881
GraphQLDirective graphQLDirective = resolvedDirectives.get(i);
79-
directiveCounterParts.put(graphQLDirective, directive);
82+
Directive astDirective = astDirectivesByName.get(graphQLDirective.getName());
83+
if (astDirective != null) {
84+
directiveCounterParts.put(graphQLDirective, astDirective);
85+
}
8086
}
8187

8288
ImmutableList.Builder<QueryAppliedDirective> appliedDirectiveBuilder = ImmutableList.builder();
8389
for (GraphQLDirective resolvedDirective : resolvedDirectives) {
8490
QueryAppliedDirective appliedDirective = toAppliedDirective(resolvedDirective);
85-
gqlDirectiveCounterParts.put(resolvedDirective, appliedDirective);
8691
appliedDirectiveBuilder.add(appliedDirective);
92+
gqlDirectiveCounterParts.put(resolvedDirective, appliedDirective);
8793
}
8894
byField.put(field, resolvedDirectives);
8995
// at some point we will only use applied
@@ -106,10 +112,12 @@ private void computeValuesLazily() {
106112
byNameApplied.values().forEach(directiveList -> {
107113
for (QueryAppliedDirective queryAppliedDirective : directiveList) {
108114
GraphQLDirective graphQLDirective = gqlDirectiveCounterPartsInverse.get(queryAppliedDirective);
115+
// we need this counterpart because the ValuesResolver needs the runtime and AST element
109116
Directive directive = directiveCounterParts.get(graphQLDirective);
110-
111-
Map<String, NormalizedInputValue> normalizedArgumentValues = ValuesResolver.getNormalizedArgumentValues(graphQLDirective.getArguments(), directive.getArguments(), this.normalizedVariableValues);
112-
normalizedValuesByAppliedDirective.put(queryAppliedDirective, normalizedArgumentValues);
117+
if (directive != null) {
118+
Map<String, NormalizedInputValue> normalizedArgumentValues = ValuesResolver.getNormalizedArgumentValues(graphQLDirective.getArguments(), directive.getArguments(), this.normalizedVariableValues);
119+
normalizedValuesByAppliedDirective.put(queryAppliedDirective, normalizedArgumentValues);
120+
}
113121
}
114122
});
115123
}

src/test/groovy/graphql/schema/CoercingTest.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql.schema
22

33
import graphql.ExecutionInput
4+
import graphql.GraphQLContext
45
import graphql.TestUtil
56
import graphql.analysis.MaxQueryDepthInstrumentation
67
import graphql.language.ArrayValue
@@ -13,6 +14,7 @@ import graphql.language.StringValue
1314
import graphql.language.VariableReference
1415
import graphql.schema.idl.RuntimeWiring
1516
import graphql.schema.idl.TypeRuntimeWiring
17+
import org.jetbrains.annotations.NotNull
1618
import spock.lang.Specification
1719

1820
import java.time.ZonedDateTime
@@ -244,6 +246,11 @@ class CoercingTest extends Specification {
244246
Object parseLiteral(Object input) throws CoercingParseLiteralException {
245247
return input
246248
}
249+
250+
@Override
251+
StringValue valueToLiteral(@NotNull Object input, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) {
252+
return new StringValue(input.toString())
253+
}
247254
})
248255
.build()
249256

0 commit comments

Comments
 (0)