66import graphql .analysis .QueryVisitorInlineFragmentEnvironment ;
77import graphql .analysis .QueryVisitorStub ;
88import graphql .language .Argument ;
9- import graphql .language .AstTransformer ;
109import graphql .language .Document ;
1110import graphql .language .Field ;
1211import graphql .language .FragmentDefinition ;
1312import graphql .language .FragmentSpread ;
1413import graphql .language .InlineFragment ;
1514import graphql .language .Node ;
16- import graphql .language .NodeVisitorStub ;
1715import graphql .language .OperationDefinition ;
1816import graphql .language .Value ;
1917import graphql .language .VariableReference ;
2018import graphql .schema .GraphQLObjectType ;
2119import graphql .schema .GraphQLSchema ;
22- import graphql .util .TraversalControl ;
23- import graphql .util .TraverserContext ;
2420import lombok .Getter ;
2521
2622import java .util .ArrayList ;
27- import java .util .Collection ;
2823import java .util .List ;
2924import java .util .Map ;
3025import java .util .Set ;
3631 */
3732public class VariableDefinitionFilter {
3833
39- private static AstTransformer astTransformer = new AstTransformer ();
40-
4134 /**
4235 * Traverses a GraphQL Node and returns all VariableReference names used in all nodes in the graph.
4336 *
@@ -50,17 +43,17 @@ public class VariableDefinitionFilter {
5043 * reference indicator prefix '$' will be <b>excluded</b> in the result.
5144 */
5245 public Set <String > getVariableReferencesFromNode (GraphQLSchema graphQLSchema , GraphQLObjectType rootType ,
53- Map <String , FragmentDefinition > fragmentsByName , Map <String , Object > variables , Node <?> rootNode ) {
46+ Map <String , FragmentDefinition > fragmentsByName , Map <String , Object > variables , Node <?> rootNode ) {
5447 final VariableReferenceVisitor variableReferenceVisitor = new VariableReferenceVisitor ();
5548
5649 //need to utilize a better pattern for creating mockable QueryTraverser/QueryTransformer
5750 QueryTraverser queryTraverser = QueryTraverser .newQueryTraverser ()
58- .schema (graphQLSchema )
59- .rootParentType (rootType ) //need to support also for subscription
60- .fragmentsByName (fragmentsByName )
61- .variables (variables )
62- .root (rootNode )
63- .build ();
51+ .schema (graphQLSchema )
52+ .rootParentType (rootType ) //need to support also for subscription
53+ .fragmentsByName (fragmentsByName )
54+ .variables (variables )
55+ .root (rootNode )
56+ .build ();
6457
6558 queryTraverser .visitPreOrder (variableReferenceVisitor );
6659
@@ -75,28 +68,16 @@ public Set<String> getVariableReferencesFromNode(GraphQLSchema graphQLSchema, Gr
7568
7669 Set <VariableReference > additionalReferences = operationDirectiveVariableReferences (operationDefinitions );
7770
78- Stream <VariableReference > variableReferenceStream ;
79- if ((variableReferenceVisitor .getVariableReferences ().size () + additionalReferences .size ()) != variables .size ()) {
80- NodeTraverser nodeTraverser = new NodeTraverser ();
81- astTransformer .transform (rootNode , nodeTraverser );
82-
83- variableReferenceStream = Stream .of (variableReferenceVisitor .getVariableReferences (),
84- additionalReferences ,
85- nodeTraverser .getVariableReferenceExtractor ().getVariableReferences ())
86- .flatMap (Collection ::stream );
87- } else {
88- variableReferenceStream = Stream .concat (variableReferenceVisitor .getVariableReferences ().stream (), additionalReferences .stream ());
89- }
90- return variableReferenceStream .map (VariableReference ::getName ).collect (Collectors .toSet ());
91-
71+ return Stream .concat (variableReferenceVisitor .getVariableReferences ().stream (), additionalReferences .stream ())
72+ .map (VariableReference ::getName ).collect (Collectors .toSet ());
9273 }
9374
9475 private Set <VariableReference > operationDirectiveVariableReferences (List <OperationDefinition > operationDefinitions ) {
9576 final List <Value > values = operationDefinitions .stream ()
96- .flatMap (operationDefinition -> operationDefinition .getDirectives ().stream ())
97- .flatMap (directive -> directive .getArguments ().stream ())
98- .map (Argument ::getValue )
99- .collect (Collectors .toList ());
77+ .flatMap (operationDefinition -> operationDefinition .getDirectives ().stream ())
78+ .flatMap (directive -> directive .getArguments ().stream ())
79+ .map (Argument ::getValue )
80+ .collect (Collectors .toList ());
10081
10182 VariableReferenceExtractor extractor = new VariableReferenceExtractor ();
10283 extractor .captureVariableReferences (values );
@@ -138,7 +119,7 @@ public void visitField(final QueryVisitorFieldEnvironment env) {
138119 }
139120
140121 final Stream <Argument > directiveArgumentStream = field .getDirectives ().stream ()
141- .flatMap (directive -> directive .getArguments ().stream ());
122+ .flatMap (directive -> directive .getArguments ().stream ());
142123
143124 final Stream <Argument > fieldArgumentStream = field .getArguments ().stream ();
144125
@@ -154,7 +135,7 @@ public void visitInlineFragment(final QueryVisitorInlineFragmentEnvironment env)
154135 }
155136
156137 Stream <Argument > arguments = env .getInlineFragment ().getDirectives ().stream ()
157- .flatMap (directive -> directive .getArguments ().stream ());
138+ .flatMap (directive -> directive .getArguments ().stream ());
158139
159140 captureVariableReferences (arguments );
160141 }
@@ -169,33 +150,18 @@ public void visitFragmentSpread(final QueryVisitorFragmentSpreadEnvironment env)
169150 }
170151
171152 final Stream <Argument > allArguments = Stream .concat (
172- fragmentDefinition .getDirectives ().stream (),
173- fragmentSpread .getDirectives ().stream ()
153+ fragmentDefinition .getDirectives ().stream (),
154+ fragmentSpread .getDirectives ().stream ()
174155 ).flatMap (directive -> directive .getArguments ().stream ());
175156
176157 captureVariableReferences (allArguments );
177158 }
178159
179160 private void captureVariableReferences (Stream <Argument > arguments ) {
180161 final List <Value > values = arguments .map (Argument ::getValue )
181- .collect (Collectors .toList ());
162+ .collect (Collectors .toList ());
182163
183164 variableReferenceExtractor .captureVariableReferences (values );
184165 }
185166 }
186-
187- static class NodeTraverser extends NodeVisitorStub {
188-
189- @ Getter
190- private final VariableReferenceExtractor variableReferenceExtractor = new VariableReferenceExtractor ();
191-
192- public TraversalControl visitArgument (Argument node , TraverserContext <Node > context ) {
193- return this .visitNode (node , context );
194- }
195-
196- public TraversalControl visitVariableReference (VariableReference node , TraverserContext <Node > context ) {
197- variableReferenceExtractor .captureVariableReference (node );
198- return this .visitValue (node , context );
199- }
200- }
201167}
0 commit comments