6
6
import graphql .analysis .QueryVisitorInlineFragmentEnvironment ;
7
7
import graphql .analysis .QueryVisitorStub ;
8
8
import graphql .language .Argument ;
9
- import graphql .language .AstTransformer ;
10
9
import graphql .language .Document ;
11
10
import graphql .language .Field ;
12
11
import graphql .language .FragmentDefinition ;
13
12
import graphql .language .FragmentSpread ;
14
13
import graphql .language .InlineFragment ;
15
14
import graphql .language .Node ;
16
- import graphql .language .NodeVisitorStub ;
17
15
import graphql .language .OperationDefinition ;
18
16
import graphql .language .Value ;
19
17
import graphql .language .VariableReference ;
20
18
import graphql .schema .GraphQLObjectType ;
21
19
import graphql .schema .GraphQLSchema ;
22
- import graphql .util .TraversalControl ;
23
- import graphql .util .TraverserContext ;
24
20
import lombok .Getter ;
25
21
26
22
import java .util .ArrayList ;
27
- import java .util .Collection ;
28
23
import java .util .List ;
29
24
import java .util .Map ;
30
25
import java .util .Set ;
36
31
*/
37
32
public class VariableDefinitionFilter {
38
33
39
- private static AstTransformer astTransformer = new AstTransformer ();
40
-
41
34
/**
42
35
* Traverses a GraphQL Node and returns all VariableReference names used in all nodes in the graph.
43
36
*
@@ -50,17 +43,17 @@ public class VariableDefinitionFilter {
50
43
* reference indicator prefix '$' will be <b>excluded</b> in the result.
51
44
*/
52
45
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 ) {
54
47
final VariableReferenceVisitor variableReferenceVisitor = new VariableReferenceVisitor ();
55
48
56
49
//need to utilize a better pattern for creating mockable QueryTraverser/QueryTransformer
57
50
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 ();
64
57
65
58
queryTraverser .visitPreOrder (variableReferenceVisitor );
66
59
@@ -75,28 +68,16 @@ public Set<String> getVariableReferencesFromNode(GraphQLSchema graphQLSchema, Gr
75
68
76
69
Set <VariableReference > additionalReferences = operationDirectiveVariableReferences (operationDefinitions );
77
70
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 ());
92
73
}
93
74
94
75
private Set <VariableReference > operationDirectiveVariableReferences (List <OperationDefinition > operationDefinitions ) {
95
76
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 ());
100
81
101
82
VariableReferenceExtractor extractor = new VariableReferenceExtractor ();
102
83
extractor .captureVariableReferences (values );
@@ -138,7 +119,7 @@ public void visitField(final QueryVisitorFieldEnvironment env) {
138
119
}
139
120
140
121
final Stream <Argument > directiveArgumentStream = field .getDirectives ().stream ()
141
- .flatMap (directive -> directive .getArguments ().stream ());
122
+ .flatMap (directive -> directive .getArguments ().stream ());
142
123
143
124
final Stream <Argument > fieldArgumentStream = field .getArguments ().stream ();
144
125
@@ -154,7 +135,7 @@ public void visitInlineFragment(final QueryVisitorInlineFragmentEnvironment env)
154
135
}
155
136
156
137
Stream <Argument > arguments = env .getInlineFragment ().getDirectives ().stream ()
157
- .flatMap (directive -> directive .getArguments ().stream ());
138
+ .flatMap (directive -> directive .getArguments ().stream ());
158
139
159
140
captureVariableReferences (arguments );
160
141
}
@@ -169,33 +150,18 @@ public void visitFragmentSpread(final QueryVisitorFragmentSpreadEnvironment env)
169
150
}
170
151
171
152
final Stream <Argument > allArguments = Stream .concat (
172
- fragmentDefinition .getDirectives ().stream (),
173
- fragmentSpread .getDirectives ().stream ()
153
+ fragmentDefinition .getDirectives ().stream (),
154
+ fragmentSpread .getDirectives ().stream ()
174
155
).flatMap (directive -> directive .getArguments ().stream ());
175
156
176
157
captureVariableReferences (allArguments );
177
158
}
178
159
179
160
private void captureVariableReferences (Stream <Argument > arguments ) {
180
161
final List <Value > values = arguments .map (Argument ::getValue )
181
- .collect (Collectors .toList ());
162
+ .collect (Collectors .toList ());
182
163
183
164
variableReferenceExtractor .captureVariableReferences (values );
184
165
}
185
166
}
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
- }
201
167
}
0 commit comments