1616import graphql .schema .GraphQLSchema ;
1717import graphql .util .FpKit ;
1818import graphql .util .LockKit ;
19+ import org .jetbrains .annotations .Nullable ;
1920
2021import java .util .ArrayList ;
2122import 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 }
0 commit comments