@@ -54,6 +54,34 @@ class AuthDownstreamQueryRedactorVisitorSpec extends Specification {
54
54
}
55
55
"""
56
56
57
+ def skipQuery = """ query skipQuery(\$ shouldSkip: Boolean) {
58
+ a {
59
+ b1 @skip(if: \$ shouldSkip) {
60
+ c1 {
61
+ s1
62
+ }
63
+ }
64
+ b2 {
65
+ i1
66
+ }
67
+ }
68
+ }
69
+ """
70
+
71
+ def includesQuery = """ query includesQuery(\$ shouldInclude: Boolean) {
72
+ a {
73
+ b1 {
74
+ c1 {
75
+ s1
76
+ }
77
+ }
78
+ b2 @include(if: \$ shouldInclude) {
79
+ i1
80
+ }
81
+ }
82
+ }
83
+ """
84
+
57
85
static final Object TEST_AUTH_DATA = " TestAuthDataCanBeAnyObject"
58
86
59
87
Field mockField = Mock ()
@@ -134,6 +162,171 @@ class AuthDownstreamQueryRedactorVisitorSpec extends Specification {
134
162
argumentValueResolver. resolve(_, _, _) >> Collections . emptyMap()
135
163
}
136
164
165
+ def " skip query with skip directive true removes selection set" () {
166
+ given :
167
+ Document document = new Parser (). parseDocument(skipQuery)
168
+ OperationDefinition operationDefinition = document. getDefinitionsOfType(OperationDefinition . class). get(0 )
169
+ Field rootField = SelectionSetUtil . getFieldByPath(Arrays . asList(" a" ), operationDefinition. getSelectionSet())
170
+ GraphQLFieldsContainer rootFieldParentType = (GraphQLFieldsContainer ) testGraphQLSchema. getType(" Query" )
171
+
172
+ Map<String , Object > queryVariables = new HashMap<> ()
173
+ queryVariables. put(" shouldSkip" , true )
174
+
175
+ AuthDownstreamQueryModifier specUnderTest = AuthDownstreamQueryModifier . builder()
176
+ .rootParentType((GraphQLFieldsContainer ) rootFieldParentType)
177
+ .fieldAuthorization(mockFieldAuthorization)
178
+ .graphQLContext(mockGraphQLContext)
179
+ .queryVariables(queryVariables)
180
+ .graphQLSchema(testGraphQLSchema)
181
+ .selectionCollector(new SelectionCollector (fragmentsByName))
182
+ .serviceMetadata(mockServiceMetadata)
183
+ .authData(TEST_AUTH_DATA )
184
+ .build()
185
+
186
+ when :
187
+ Field transformedField = (Field ) astTransformer. transform(rootField, specUnderTest)
188
+
189
+ then :
190
+ transformedField. getName() == " a"
191
+ Object [] selectionSet = transformedField. getSelectionSet()
192
+ .getSelections()
193
+ .asList()
194
+ selectionSet. size() == 1
195
+ ((Field )selectionSet. first()). getName() == (" b2" )
196
+
197
+ 1 * mockFieldAuthorization. authorize(queryA) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
198
+ 1 * mockFieldAuthorization. authorize(aB2) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
199
+ 1 * mockFieldAuthorization. authorize(b2i1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
200
+ mockRenamedMetadata. getOriginalFieldNamesByRenamedName() >> Collections . emptyMap()
201
+ mockServiceMetadata. getRenamedMetadata() >> mockRenamedMetadata
202
+ }
203
+
204
+ def " skip query with skip directive false keeps selection set" () {
205
+ given :
206
+ Document document = new Parser (). parseDocument(skipQuery)
207
+ OperationDefinition operationDefinition = document. getDefinitionsOfType(OperationDefinition . class). get(0 )
208
+ Field rootField = SelectionSetUtil . getFieldByPath(Arrays . asList(" a" ), operationDefinition. getSelectionSet())
209
+ GraphQLFieldsContainer rootFieldParentType = (GraphQLFieldsContainer ) testGraphQLSchema. getType(" Query" )
210
+
211
+ Map<String , Object > queryVariables = new HashMap<> ()
212
+ queryVariables. put(" shouldSkip" , false )
213
+
214
+ AuthDownstreamQueryModifier specUnderTest = AuthDownstreamQueryModifier . builder()
215
+ .rootParentType((GraphQLFieldsContainer ) rootFieldParentType)
216
+ .fieldAuthorization(mockFieldAuthorization)
217
+ .graphQLContext(mockGraphQLContext)
218
+ .queryVariables(queryVariables)
219
+ .graphQLSchema(testGraphQLSchema)
220
+ .selectionCollector(new SelectionCollector (fragmentsByName))
221
+ .serviceMetadata(mockServiceMetadata)
222
+ .authData(TEST_AUTH_DATA )
223
+ .build()
224
+
225
+ when :
226
+ Field transformedField = (Field ) astTransformer. transform(rootField, specUnderTest)
227
+
228
+ then :
229
+ transformedField. getName() == " a"
230
+ Object [] selectionSet = transformedField. getSelectionSet()
231
+ .getSelections()
232
+ .asList()
233
+ selectionSet. size() == 2
234
+ ((Field )selectionSet[0 ]). getName() == " b1"
235
+ ((Field )selectionSet[1 ]). getName() == " b2"
236
+
237
+ 1 * mockFieldAuthorization. authorize(queryA) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
238
+ 1 * mockFieldAuthorization. authorize(aB1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
239
+ 1 * mockFieldAuthorization. authorize(b1C1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
240
+ 1 * mockFieldAuthorization. authorize(c1S1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
241
+ 1 * mockFieldAuthorization. authorize(aB2) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
242
+ 1 * mockFieldAuthorization. authorize(b2i1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
243
+ mockRenamedMetadata. getOriginalFieldNamesByRenamedName() >> Collections . emptyMap()
244
+ mockServiceMetadata. getRenamedMetadata() >> mockRenamedMetadata
245
+ }
246
+
247
+ def " includes query with include directive true keeps selection set" () {
248
+ given :
249
+ Document document = new Parser (). parseDocument(includesQuery)
250
+ OperationDefinition operationDefinition = document. getDefinitionsOfType(OperationDefinition . class). get(0 )
251
+ Field rootField = SelectionSetUtil . getFieldByPath(Arrays . asList(" a" ), operationDefinition. getSelectionSet())
252
+ GraphQLFieldsContainer rootFieldParentType = (GraphQLFieldsContainer ) testGraphQLSchema. getType(" Query" )
253
+
254
+ Map<String , Object > queryVariables = new HashMap<> ()
255
+ queryVariables. put(" shouldInclude" , true )
256
+
257
+ AuthDownstreamQueryModifier specUnderTest = AuthDownstreamQueryModifier . builder()
258
+ .rootParentType((GraphQLFieldsContainer ) rootFieldParentType)
259
+ .fieldAuthorization(mockFieldAuthorization)
260
+ .graphQLContext(mockGraphQLContext)
261
+ .queryVariables(queryVariables)
262
+ .graphQLSchema(testGraphQLSchema)
263
+ .selectionCollector(new SelectionCollector (fragmentsByName))
264
+ .serviceMetadata(mockServiceMetadata)
265
+ .authData(TEST_AUTH_DATA )
266
+ .build()
267
+
268
+ when :
269
+ Field transformedField = (Field ) astTransformer. transform(rootField, specUnderTest)
270
+
271
+ then :
272
+ transformedField. getName() == " a"
273
+ Object [] selectionSet = transformedField. getSelectionSet()
274
+ .getSelections()
275
+ .asList()
276
+ selectionSet. size() == 2
277
+ ((Field )selectionSet[0 ]). getName() == " b1"
278
+ ((Field )selectionSet[1 ]). getName() == " b2"
279
+
280
+ 1 * mockFieldAuthorization. authorize(queryA) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
281
+ 1 * mockFieldAuthorization. authorize(aB1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
282
+ 1 * mockFieldAuthorization. authorize(b1C1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
283
+ 1 * mockFieldAuthorization. authorize(c1S1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
284
+ 1 * mockFieldAuthorization. authorize(aB2) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
285
+ 1 * mockFieldAuthorization. authorize(b2i1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
286
+ mockRenamedMetadata. getOriginalFieldNamesByRenamedName() >> Collections . emptyMap()
287
+ mockServiceMetadata. getRenamedMetadata() >> mockRenamedMetadata
288
+ }
289
+
290
+ def " includes query with include directive false removes selection set" () {
291
+ given :
292
+ Document document = new Parser (). parseDocument(includesQuery)
293
+ OperationDefinition operationDefinition = document. getDefinitionsOfType(OperationDefinition . class). get(0 )
294
+ Field rootField = SelectionSetUtil . getFieldByPath(Arrays . asList(" a" ), operationDefinition. getSelectionSet())
295
+ GraphQLFieldsContainer rootFieldParentType = (GraphQLFieldsContainer ) testGraphQLSchema. getType(" Query" )
296
+
297
+ Map<String , Object > queryVariables = new HashMap<> ()
298
+ queryVariables. put(" shouldInclude" , false )
299
+
300
+ AuthDownstreamQueryModifier specUnderTest = AuthDownstreamQueryModifier . builder()
301
+ .rootParentType((GraphQLFieldsContainer ) rootFieldParentType)
302
+ .fieldAuthorization(mockFieldAuthorization)
303
+ .graphQLContext(mockGraphQLContext)
304
+ .queryVariables(queryVariables)
305
+ .graphQLSchema(testGraphQLSchema)
306
+ .selectionCollector(new SelectionCollector (fragmentsByName))
307
+ .serviceMetadata(mockServiceMetadata)
308
+ .authData(TEST_AUTH_DATA )
309
+ .build()
310
+
311
+ when :
312
+ Field transformedField = (Field ) astTransformer. transform(rootField, specUnderTest)
313
+
314
+ then :
315
+ transformedField. getName() == " a"
316
+ Object [] selectionSet = transformedField. getSelectionSet()
317
+ .getSelections()
318
+ .asList()
319
+ selectionSet. size() == 1
320
+ ((Field )selectionSet[0 ]). getName() == " b1"
321
+
322
+ 1 * mockFieldAuthorization. authorize(queryA) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
323
+ 1 * mockFieldAuthorization. authorize(aB1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
324
+ 1 * mockFieldAuthorization. authorize(b1C1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
325
+ 1 * mockFieldAuthorization. authorize(c1S1) >> FieldAuthorizationResult . ALLOWED_FIELD_AUTH_RESULT
326
+ mockRenamedMetadata. getOriginalFieldNamesByRenamedName() >> Collections . emptyMap()
327
+ mockServiceMetadata. getRenamedMetadata() >> mockRenamedMetadata
328
+ }
329
+
137
330
def " redact query, results to empty selection set" () {
138
331
given :
139
332
0 commit comments