-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
QueryTraverserTest.groovy
1910 lines (1685 loc) · 66.8 KB
/
QueryTraverserTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package graphql.analysis
import graphql.AssertException
import graphql.TestUtil
import graphql.execution.CoercedVariables
import graphql.language.ArrayValue
import graphql.language.Document
import graphql.language.Field
import graphql.language.FragmentDefinition
import graphql.language.FragmentSpread
import graphql.language.InlineFragment
import graphql.language.IntValue
import graphql.language.NodeUtil
import graphql.language.ObjectValue
import graphql.language.OperationDefinition
import graphql.language.StringValue
import graphql.parser.Parser
import graphql.schema.GraphQLArgument
import graphql.schema.GraphQLInputObjectField
import graphql.schema.GraphQLInterfaceType
import graphql.schema.GraphQLNonNull
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLSchema
import graphql.schema.GraphQLUnionType
import graphql.util.TraversalControl
import spock.lang.Specification
import spock.lang.Unroll
import static graphql.schema.GraphQLList.list
import static graphql.schema.GraphQLNonNull.nonNull
import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring
import static graphql.util.TraverserContext.Phase.ENTER
import static graphql.util.TraverserContext.Phase.LEAVE
import static java.util.Collections.emptyMap
class QueryTraverserTest extends Specification {
Document createQuery(String query) {
Parser parser = new Parser()
parser.parseDocument(query)
}
QueryTraverser createQueryTraversal(Document document, GraphQLSchema schema, Map variables = [:]) {
QueryTraverser queryTraversal = QueryTraverser.newQueryTraverser()
.schema(schema)
.document(document)
.variables(variables)
.build()
return queryTraversal
}
QueryVisitor mockQueryVisitor() {
def mock = Mock(QueryVisitor)
mock.visitFieldWithControl(_) >> { params ->
mock.visitField(params)
TraversalControl.CONTINUE
}
mock.visitArgument(_) >> { params ->
TraversalControl.CONTINUE
}
mock.visitArgumentValue(_) >> { params ->
TraversalControl.CONTINUE
}
return mock
}
def "test preOrder order for visitField"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{foo { subFoo} bar }
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal.visitPreOrder(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" &&
it.selectionSetContainer == null
})
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.parentType.name == "Foo" &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo" &&
it.selectionSetContainer == it.parentEnvironment.field
})
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
}
def "test postOrder order for visitField"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{foo { subFoo} bar }
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal.visitPostOrder(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.parentType.name == "Foo" &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo"
})
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" })
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
}
def "test for visitArgs and visitArgsValue"() {
given:
def schema = TestUtil.schema("""
type Query{
foo (complexArg : Complex, simpleArg : String) : Foo
bar: String
}
type Foo {
subFoo: String
}
input Complex {
name : String
moreComplex : [MoreComplex]
}
input MoreComplex {
height : Int
weight : Int
}
""")
def visitor = mockQueryVisitor()
def query = createQuery('''{
foo( complexArg : { name : "Ted", moreComplex : [{height : 100, weight : 200}] } , simpleArg : "Hi" ) {
subFoo
}
bar
}''')
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal.visitPreOrder(visitor)
then:
1 * visitor.visitArgument({ QueryVisitorFieldArgumentEnvironment env ->
env.argument.name == "complexArg" && env.graphQLArgument.type.name == "Complex"
}) >> TraversalControl.CONTINUE
1 * visitor.visitArgument({ QueryVisitorFieldArgumentEnvironment env ->
env.argument.name == "simpleArg" && env.graphQLArgument.type.name == "String"
}) >> TraversalControl.CONTINUE
1 * visitor.visitArgumentValue({ QueryVisitorFieldArgumentValueEnvironment env ->
env.graphQLArgument.name == "complexArg" &&
env.argumentInputValue.name == "complexArg" &&
env.argumentInputValue.value instanceof ObjectValue
}) >> TraversalControl.CONTINUE
1 * visitor.visitArgumentValue({ QueryVisitorFieldArgumentValueEnvironment env ->
env.graphQLArgument.name == "complexArg" &&
env.argumentInputValue.name == "name" &&
env.argumentInputValue.value instanceof StringValue
}) >> TraversalControl.CONTINUE
1 * visitor.visitArgumentValue({ QueryVisitorFieldArgumentValueEnvironment env ->
env.graphQLArgument.name == "complexArg" &&
env.argumentInputValue.name == "moreComplex" &&
env.argumentInputValue.value instanceof ArrayValue
}) >> TraversalControl.CONTINUE
1 * visitor.visitArgumentValue({ QueryVisitorFieldArgumentValueEnvironment env ->
env.graphQLArgument.name == "complexArg" &&
env.argumentInputValue.parent.name == "moreComplex" &&
env.argumentInputValue.name == "weight" &&
env.argumentInputValue.value instanceof IntValue
}) >> TraversalControl.CONTINUE
1 * visitor.visitArgumentValue({ QueryVisitorFieldArgumentValueEnvironment env ->
env.graphQLArgument.name == "complexArg" &&
env.argumentInputValue.parent.name == "moreComplex" &&
env.argumentInputValue.name == "height" &&
env.argumentInputValue.value instanceof IntValue &&
env.argumentInputValue.inputValueDefinition instanceof GraphQLInputObjectField
}) >> TraversalControl.CONTINUE
1 * visitor.visitArgumentValue({ QueryVisitorFieldArgumentValueEnvironment env ->
env.graphQLArgument.name == "simpleArg" &&
env.argumentInputValue.name == "simpleArg" &&
env.argumentInputValue.value instanceof StringValue &&
env.argumentInputValue.inputValueDefinition instanceof GraphQLArgument
}) >> TraversalControl.CONTINUE
when:
queryTraversal.visitPostOrder(visitor)
then:
1 * visitor.visitArgument({ QueryVisitorFieldArgumentEnvironment env ->
env.argument.name == "complexArg" && env.graphQLArgument.type.name == "Complex"
}) >> TraversalControl.CONTINUE
1 * visitor.visitArgument({ QueryVisitorFieldArgumentEnvironment env ->
env.argument.name == "simpleArg" && env.graphQLArgument.type.name == "String"
}) >> TraversalControl.CONTINUE
}
def "test preOrder order for inline fragments"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
... on Query {
... on Query {
foo {subFoo}
}
... on Query {
foo {subFoo}
}
}
}
""")
def inlineFragmentRoot = query.children[0].children[0].children[0]
assert inlineFragmentRoot instanceof InlineFragment
def inlineFragmentLeft = inlineFragmentRoot.selectionSet.children[0]
assert inlineFragmentLeft instanceof InlineFragment
def inlineFragmentRight = inlineFragmentRoot.selectionSet.children[1]
assert inlineFragmentRight instanceof InlineFragment
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal.visitPreOrder(visitor)
then:
1 * visitor.visitInlineFragment({ QueryVisitorInlineFragmentEnvironmentImpl env -> env.inlineFragment == inlineFragmentRoot })
then:
1 * visitor.visitInlineFragment({ QueryVisitorInlineFragmentEnvironmentImpl env -> env.inlineFragment == inlineFragmentLeft })
then:
1 * visitor.visitInlineFragment({ QueryVisitorInlineFragmentEnvironmentImpl env -> env.inlineFragment == inlineFragmentRight })
}
def "test postOrder order for inline fragments"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
... on Query @root {
... on Query @left {
foo {subFoo}
}
... on Query @right {
foo {subFoo}
}
}
}
""")
def inlineFragmentRoot = query.children[0].children[0].children[0]
assert inlineFragmentRoot instanceof InlineFragment
def inlineFragmentLeft = inlineFragmentRoot.selectionSet.children[0]
assert inlineFragmentLeft instanceof InlineFragment
def inlineFragmentRight = inlineFragmentRoot.selectionSet.children[1]
assert inlineFragmentRight instanceof InlineFragment
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal.visitPostOrder(visitor)
then:
1 * visitor.visitInlineFragment({ QueryVisitorInlineFragmentEnvironmentImpl env -> env.inlineFragment == inlineFragmentLeft })
then:
1 * visitor.visitInlineFragment({ QueryVisitorInlineFragmentEnvironmentImpl env -> env.inlineFragment == inlineFragmentRight })
then:
1 * visitor.visitInlineFragment({ QueryVisitorInlineFragmentEnvironmentImpl env -> env.inlineFragment == inlineFragmentRoot })
}
def "test preOrder order for fragment spreads"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
...F1
}
fragment F1 on Query {
...F2
...F3
}
fragment F2 on Query {
bar
}
fragment F3 on Query {
bar
}
""")
def fragmentF1 = query.definitions[1]
assert fragmentF1 instanceof FragmentDefinition
def fragmentF2 = query.definitions[2]
assert fragmentF2 instanceof FragmentDefinition
def fragmentF3 = query.definitions[3]
assert fragmentF3 instanceof FragmentDefinition
def fragmentSpreadRoot = query.definitions[0].children[0].children[0]
assert fragmentSpreadRoot instanceof FragmentSpread
def fragmentSpreadLeft = fragmentF1.selectionSet.children[0]
assert fragmentSpreadLeft instanceof FragmentSpread
def fragmentSpreadRight = fragmentF1.selectionSet.children[1]
assert fragmentSpreadRight instanceof FragmentSpread
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal.visitPreOrder(visitor)
then:
1 * visitor.visitFragmentSpread({ QueryVisitorFragmentSpreadEnvironmentImpl env -> env.fragmentSpread == fragmentSpreadRoot && env.fragmentDefinition == fragmentF1 })
then:
1 * visitor.visitFragmentSpread({ QueryVisitorFragmentSpreadEnvironmentImpl env -> env.fragmentSpread == fragmentSpreadLeft && env.fragmentDefinition == fragmentF2 })
then:
1 * visitor.visitFragmentSpread({ QueryVisitorFragmentSpreadEnvironmentImpl env -> env.fragmentSpread == fragmentSpreadRight && env.fragmentDefinition == fragmentF3 })
}
def "test postOrder order for fragment spreads"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
...F1
}
fragment F1 on Query {
...F2
...F3
}
fragment F2 on Query {
bar
}
fragment F3 on Query {
bar
}
""")
def fragmentF1 = query.definitions[1]
assert fragmentF1 instanceof FragmentDefinition
def fragmentF2 = query.definitions[2]
assert fragmentF2 instanceof FragmentDefinition
def fragmentF3 = query.definitions[3]
assert fragmentF3 instanceof FragmentDefinition
def fragmentSpreadRoot = query.definitions[0].children[0].children[0]
assert fragmentSpreadRoot instanceof FragmentSpread
def fragmentSpreadLeft = fragmentF1.selectionSet.children[0]
assert fragmentSpreadLeft instanceof FragmentSpread
def fragmentSpreadRight = fragmentF1.selectionSet.children[1]
assert fragmentSpreadRight instanceof FragmentSpread
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal.visitPostOrder(visitor)
then:
1 * visitor.visitFragmentSpread({ QueryVisitorFragmentSpreadEnvironmentImpl env -> env.fragmentSpread == fragmentSpreadLeft && env.fragmentDefinition == fragmentF2 })
then:
1 * visitor.visitFragmentSpread({ QueryVisitorFragmentSpreadEnvironmentImpl env -> env.fragmentSpread == fragmentSpreadRight && env.fragmentDefinition == fragmentF3 })
then:
1 * visitor.visitFragmentSpread({ QueryVisitorFragmentSpreadEnvironmentImpl env -> env.fragmentSpread == fragmentSpreadRoot && env.fragmentDefinition == fragmentF1 })
}
def "test preOrder and postOrder order for fragment definitions and raw variables"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
...F1
}
fragment F1 on Query {
foo {
subFoo
}
}
""")
def fragments = NodeUtil.getFragmentsByName(query)
QueryTraverser queryTraversal = QueryTraverser.newQueryTraverser()
.schema(schema)
.root(fragments["F1"])
.rootParentType(schema.getQueryType())
.fragmentsByName(fragments)
.variables([:])
.build()
when:
queryTraversal.visitPreOrder(visitor)
then:
1 * visitor.visitFragmentDefinition({ QueryVisitorFragmentDefinitionEnvironment env -> env.fragmentDefinition == fragments["F1"] })
when:
queryTraversal.visitPostOrder(visitor)
then:
1 * visitor.visitFragmentDefinition({ QueryVisitorFragmentDefinitionEnvironment env -> env.fragmentDefinition == fragments["F1"] })
}
def "test preOrder and postOrder order for fragment definitions and coerced variables"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
...F1
}
fragment F1 on Query {
foo {
subFoo
}
}
""")
def fragments = NodeUtil.getFragmentsByName(query)
QueryTraverser queryTraversal = QueryTraverser.newQueryTraverser()
.schema(schema)
.root(fragments["F1"])
.rootParentType(schema.getQueryType())
.fragmentsByName(fragments)
.coercedVariables(CoercedVariables.emptyVariables())
.build()
when:
queryTraversal.visitPreOrder(visitor)
then:
1 * visitor.visitFragmentDefinition({ QueryVisitorFragmentDefinitionEnvironment env -> env.fragmentDefinition == fragments["F1"] })
when:
queryTraversal.visitPostOrder(visitor)
then:
1 * visitor.visitFragmentDefinition({ QueryVisitorFragmentDefinitionEnvironment env -> env.fragmentDefinition == fragments["F1"] })
}
def "works for mutations()"() {
given:
def schema = TestUtil.schema("""
type Query {
a: String
}
type Mutation{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
schema {mutation: Mutation, query: Query}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
mutation M{bar foo { subFoo} }
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Mutation" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Mutation" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.parentType.name == "Foo" &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo"
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
def "works for subscriptions()"() {
given:
def schema = TestUtil.schema("""
type Query {
a: String
}
type Subscription{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
schema {subscription: Subscription, query: Query}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
subscription S{bar foo { subFoo} }
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Subscription" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Subscription" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.parentType.name == "Foo" &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo"
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "field with arguments: (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo(arg1: String, arg2: Boolean): String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
query myQuery(\$myVar: String){foo(arg1: \$myVar, arg2: true)}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema, ['myVar': 'hello'])
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "foo" &&
it.arguments == ['arg1': 'hello', 'arg2': true]
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "traverse a query when a default variable is a list: (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo(arg1: [String]): String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
query myQuery(\$myVar: [String] = ["hello default"]) {foo(arg1: \$myVar)}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema, ['myVar': 'hello'])
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "foo" &&
it.arguments == ['arg1': ['hello']]
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "traverse a query when a default variable is a list and query does not specify variables: (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo(arg1: [String]): String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
query myQuery(\$myVar: [String] = ["hello default"]) {foo(arg1: \$myVar)}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema, [:])
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "foo" &&
it.arguments == ['arg1': ['hello default']]
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "simple query (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{bar foo { subFoo} }
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.parentType.name == "Foo" &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo"
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "query with non null and lists (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo!
foo2: [Foo]
foo3: [Foo!]
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{bar foo { subFoo} foo2 { subFoo} foo3 { subFoo}}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "foo" && it.fieldDefinition.type.wrappedType.name == "Foo" && it.parentType.name == "Query" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.fieldsContainer.name == "Foo" &&
(it.parentType instanceof GraphQLNonNull) &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.wrappedType.name == "Foo"
})
2 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "subFoo" })
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "query with inline fragment (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
bar
... on Query {
foo
{ subFoo
}
}
}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
def inlineFragment = query.children[0].children[0].children[1]
assert inlineFragment instanceof InlineFragment
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" && it.selectionSetContainer == inlineFragment })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.parentType.name == "Foo" &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo"
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "query with inline fragment without condition (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
bar
... {
foo
{ subFoo
}
}
}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.parentType.name == "Foo" &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo"
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "query with fragment (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
bar
...Test
}
fragment Test on Query {
foo
{ subFoo
}
}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
def fragmentDefinition = query.children[1]
assert fragmentDefinition instanceof FragmentDefinition
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" && it.selectionSetContainer == fragmentDefinition })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it ->
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
it.parentType.name == "Foo" &&
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo"
})
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "query with skipped fields (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
{
bar
...Test @skip(if: true)
}
fragment Test on Query {
foo
{ subFoo
}
}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema)
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
0 * visitor.visitField(*_)
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "query with skipped fields and variables (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo
bar: String
}
type Foo {
subFoo: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
query MyQuery(\$variableFoo: Boolean) {
bar
...Test @skip(if: \$variableFoo)
}
fragment Test on Query {
foo
{ subFoo
}
}
""")
QueryTraverser queryTraversal = createQueryTraversal(query, schema, [variableFoo: true])
when:
queryTraversal."$visitFn"(visitor)
then:
1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
0 * visitor.visitField(*_)
where:
order | visitFn
'postOrder' | 'visitPostOrder'
'preOrder' | 'visitPreOrder'
}
@Unroll
def "nested fragments (#order)"() {
given:
def schema = TestUtil.schema("""
type Query{
foo: Foo1
bar: String
}
type Foo1 {
string: String
subFoo: Foo2
}
type Foo2 {
otherString: String
}
""")
def visitor = mockQueryVisitor()
def query = createQuery("""
query MyQuery(\$variableFoo: Boolean) {
bar
...Test @include(if: \$variableFoo)
}
fragment Test on Query {
bar
foo {