forked from nautilus/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan_test.go
1794 lines (1555 loc) · 48.1 KB
/
plan_test.go
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 gateway
import (
"fmt"
"testing"
"github.com/nautilus/graphql"
"github.com/stretchr/testify/assert"
"github.com/vektah/gqlparser/v2/ast"
)
func TestPlanQuery_singleRootField(t *testing.T) {
t.Parallel()
// the location for the schema
location := "url1"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "foo", location)
schema, _ := graphql.LoadSchema(`
type Query {
foo: Boolean
}
`)
// compute the plan for a query that just hits one service
plans, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: "{ foo }",
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when building schema: %s", err.Error())
return
}
// the first selection is the only one we care about
root := plans[0].RootStep.Then[0]
// there should only be one selection
if len(root.SelectionSet) != 1 {
t.Error("encountered the wrong number of selections under root step")
return
}
rootField := graphql.SelectedFields(root.SelectionSet)[0]
// make sure that the first step is pointed at the right place
queryer := root.Queryer.(*graphql.SingleRequestQueryer)
assert.Equal(t, location, queryer.URL())
// we need to be asking for Query.foo
assert.Equal(t, rootField.Name, "foo")
// there should be anything selected underneath it
assert.Len(t, rootField.SelectionSet, 0)
}
func TestPlanQuery_includeFragmentsSameLocation(t *testing.T) {
t.Parallel()
// the location for the schema
location := "url1"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "foo", location)
schema, _ := graphql.LoadSchema(`
type Query {
foo: Boolean
}
`)
// compute the plan for a query that just hits one service
plans, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: `
query MyQuery {
...Foo
}
fragment Foo on Query {
foo
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when planning query: %s", err.Error())
return
}
if len(plans[0].RootStep.Then) != 1 {
t.Error("Could not find the step with fragment spread")
return
}
// the first selection is the only one we care about
root := plans[0].RootStep.Then[0]
// there should only be one selection
if len(root.SelectionSet) != 1 {
t.Errorf("encountered the wrong number of selections under root step: %v", len(root.SelectionSet))
return
}
// there should be a single selection that is a spread of the fragment Foo
fragment, ok := root.SelectionSet[0].(*ast.FragmentSpread)
if !ok {
t.Error("Root selection was not a fragment spread", root.SelectionSet[0])
return
}
// make sure that the fragment has the right name
assert.Equal(t, "Foo", fragment.Name)
// we need to make sure that the fragment definition matches expectation
fragmentDef := root.QueryDocument.Fragments.ForName("Foo")
if fragmentDef == nil {
t.Error("Could not find fragment definition for Foo")
return
}
// there should only be one selection in the fragment
if len(fragmentDef.SelectionSet) != 1 {
t.Errorf("Encountered the incorrect number of fields under fragment definition")
return
}
// we should have selected foo
assert.Equal(t, "foo", graphql.SelectedFields(fragmentDef.SelectionSet)[0].Name)
}
// Tests that location selection for Fields within Fragment Spreads are correctly
// prioritized, to avoid unnecessary federation steps.
func TestPlanQuery_includeFragmentsBoundaryTypes(t *testing.T) {
t.Parallel()
// the locations for the schema
location1 := "url1"
location2 := "url2"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "foo", location1)
locations.RegisterURL("BoundaryType", "a", location2)
locations.RegisterURL("BoundaryType", "a", location1)
locations.RegisterURL("BoundaryType", "b", location2)
locations.RegisterURL("BoundaryType", "b", location1)
locations.RegisterURL("boundaryA", "fieldA", location2)
locations.RegisterURL("boundaryA", "fieldA", location1)
locations.RegisterURL("boundaryB", "fieldB", location2)
locations.RegisterURL("boundaryB", "fieldB", location1)
schema, _ := graphql.LoadSchema(`
type Query {
foo: BoundaryType!
}
type BoundaryType {
a: boundaryA!
b: boundaryB!
}
type boundaryA {
fieldA: String!
}
type boundaryB {
fieldB: String!
}
`)
plans, err := (&MinQueriesPlanner{}).Plan(
&PlanningContext{
Query: `
query {
foo {
...Foo
}
}
fragment Foo on BoundaryType {
a {
... aFragment
}
b {
... bFragment
}
}
fragment aFragment on boundaryA {
fieldA
}
fragment bFragment on boundaryB {
fieldB
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
},
)
if err != nil {
t.Errorf("encountered error when planning query: %s", err.Error())
return
}
assert.Equal(t, len(plans), 1)
assert.Equal(t, len(plans[0].RootStep.Then), 1, "Expected only one child step, got: %d", len(plans[0].RootStep.Then))
assert.Equal(t, len(plans[0].RootStep.Then[0].Then), 0, "Expected no children steps, got: %d", len(plans[0].RootStep.Then[0].Then))
}
func TestPlanQuery_includeFragmentsDifferentLocation(t *testing.T) {
t.Parallel()
// the locations for the schema
location1 := "url1"
location2 := "url2"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "foo", location1)
locations.RegisterURL(typeNameQuery, "bar", location2)
schema, _ := graphql.LoadSchema(`
type Query {
foo: Boolean
bar: Boolean
}
`)
// compute the plan for a query that just hits one service
plans, err := (&MinQueriesPlanner{}).Plan(
&PlanningContext{
Query: `
query MyQuery {
...Foo
}
fragment Foo on Query {
foo
bar
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when planning query: %s", err.Error())
return
}
if len(plans[0].RootStep.Then) != 2 {
t.Errorf("Encountered incorrect number of steps after root step. Expected 2, found %v", len(plans[0].RootStep.Then))
return
}
// get the step for location 1
location1Step := plans[0].RootStep.Then[0]
// make sure that the step has only one selection (the fragment)
if len(location1Step.SelectionSet) != 1 {
t.Errorf("Encountered incorrect number of selections under location 1 step. Expected 1, found %v", len(location1Step.SelectionSet))
return
}
assert.Equal(t, &ast.FragmentSpread{Name: "Foo"}, location1Step.SelectionSet[0])
// get the step for location 2
location2Step := plans[0].RootStep.Then[1]
// make sure that the step has only one selection (the fragment)
if len(location2Step.SelectionSet) != 1 {
t.Errorf("Encountered incorrect number of selections under location 2 step. Expected 1, found %v", len(location2Step.SelectionSet))
return
}
assert.Equal(t, &ast.FragmentSpread{Name: "Foo"}, location2Step.SelectionSet[0])
// we also should have a definition for the fragment that only includes the fields to location 1
location1Defn := location1Step.FragmentDefinitions[0]
location2Defn := location2Step.FragmentDefinitions[0]
encounteredFields := Set{}
for _, definition := range (ast.FragmentDefinitionList{location1Defn, location2Defn}) {
assert.Equal(t, typeNameQuery, definition.TypeCondition)
assert.Equal(t, "Foo", definition.Name)
if len(definition.SelectionSet) != 1 {
t.Errorf("Encountered incorrect number of selections under fragment definition for location 1. Expected 1 found %v", len(location1Defn.SelectionSet))
return
}
// add the field we encountered to the set
encounteredFields.Add(graphql.SelectedFields(definition.SelectionSet)[0].Name)
}
// make sure we saw both the step for "foo" and the step for "bar"
if !encounteredFields.Has("foo") && !encounteredFields.Has("bar") {
t.Error("Did not encounter both foo and bar steps")
return
}
}
func TestPlanQuery_includeInlineFragments(t *testing.T) {
t.Parallel()
// the locations for the schema
location1 := "url1"
location2 := "url2"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "foo", location1)
locations.RegisterURL(typeNameQuery, "bar", location2)
schema, _ := graphql.LoadSchema(`
type Query {
foo: Boolean
bar: Boolean
}
`)
// compute the plan for a query that just hits one service
plans, err := (&MinQueriesPlanner{}).Plan(
&PlanningContext{
Query: `
query MyQuery {
... on Query {
foo
bar
}
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when planning query: %s", err.Error())
return
}
if len(plans[0].RootStep.Then) != 2 {
t.Errorf("Encountered incorrect number of steps after root step. Expected 2, found %v", len(plans[0].RootStep.Then))
return
}
// get the step for location 1
location1Step := plans[0].RootStep.Then[0]
assert.Equal(t, []string{}, location1Step.InsertionPoint)
// make sure that the step has only one selection (the fragment)
if len(location1Step.SelectionSet) != 1 {
t.Errorf("Encountered incorrect number of selections under location 1 step. Expected 1, found %v", len(location1Step.SelectionSet))
return
}
// get the step for location 2
location2Step := plans[0].RootStep.Then[1]
assert.Equal(t, []string{}, location2Step.InsertionPoint)
// make sure that the step has only one selection (the fragment)
if len(location2Step.SelectionSet) != 1 {
t.Errorf("Encountered incorrect number of selections under location 2 step. Expected 1, found %v", len(location2Step.SelectionSet))
return
}
// we also should have a definition for the fragment that only includes the fields to location 1
location1Defn := location1Step.SelectionSet[0]
location2Defn := location2Step.SelectionSet[0]
encounteredFields := Set{}
for _, definition := range (ast.SelectionSet{location1Defn, location2Defn}) {
fragment, ok := definition.(*ast.InlineFragment)
if !ok {
t.Error("Did not encounter an inline fragment")
return
}
assert.Equal(t, typeNameQuery, fragment.TypeCondition)
if len(fragment.SelectionSet) != 1 {
t.Errorf("Encountered incorrect number of selections under fragment definition. Expected 1 found %v", len(fragment.SelectionSet))
return
}
// add the field we encountered to the set
encounteredFields.Add(graphql.SelectedFields(fragment.SelectionSet)[0].Name)
}
// make sure we saw both the step for "foo" and the step for "bar"
if !encounteredFields.Has("foo") && !encounteredFields.Has("bar") {
t.Error("Did not encounter both foo and bar steps")
return
}
}
func TestPlanQuery_nestedInlineFragmentsSameLocation(t *testing.T) {
t.Parallel()
// the locations for the schema
loc1 := "url1"
loc2 := "url2"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "foo", loc1)
locations.RegisterURL(typeNameQuery, "bar", loc2)
schema, _ := graphql.LoadSchema(`
type Query {
foo: Boolean
bar: Boolean
}
`)
plans, err := (&MinQueriesPlanner{}).Plan(
&PlanningContext{
Query: `
query MyQuery {
... on Query {
... on Query {
foo
}
bar
}
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when planning query: %s", err.Error())
return
}
// grab the 2 sibling steps
steps := plans[0].RootStep.Then
if !assert.Len(t, steps, 2) {
return
}
var loc1Step *QueryPlanStep
var loc2Step *QueryPlanStep
// find the steps
for _, step := range steps {
// look at the queryer to figure out where the request is going
if queryer, ok := step.Queryer.(*graphql.SingleRequestQueryer); ok {
if queryer.URL() == loc1 {
loc1Step = step
} else if queryer.URL() == loc2 {
loc2Step = step
}
} else {
t.Error("Encountered non-network queryer")
return
}
}
// the step that's going to location 1 should be equivalent to
// query MyQuery {
// ... on Query {
// ... on Query {
// foo
// }
// }
// }
// that first slection should be an inline fragment
assert.NotNil(t, loc1Step)
if !assert.Len(t, loc1Step.SelectionSet, 1) {
return
}
loc1Selection, ok := loc1Step.SelectionSet[0].(*ast.InlineFragment)
if !assert.True(t, ok) {
return
}
// there should be one selection in that inline fragment
if !assert.Len(t, loc1Selection.SelectionSet, 1) {
return
}
loc1SubSelection, ok := loc1Selection.SelectionSet[0].(*ast.InlineFragment)
if !assert.True(t, ok, "first sub-selection in location 1 selection is not an inline fragment: \n%v", graphql.FormatSelectionSet(loc1Selection.SelectionSet)) {
return
}
// there should be one field
if !assert.Len(t, loc1SubSelection.SelectionSet, 1) {
return
}
loc1Field, ok := loc1SubSelection.SelectionSet[0].(*ast.Field)
if !assert.True(t, ok) {
return
}
// it should be for the field "foo"
assert.Equal(t, "foo", loc1Field.Name)
// the step that's going to location 2 should be equivalent to
// query MyQuery {
// ... on Query {
// bar
// }
// }
if !assert.Len(t, loc2Step.SelectionSet, 1) {
return
}
loc2Selection, ok := loc2Step.SelectionSet[0].(*ast.InlineFragment)
if !assert.True(t, ok) {
return
}
// it should have one selection that's a field
if !assert.Len(t, loc2Selection.SelectionSet, 1) {
return
}
loc2Field, ok := loc2Selection.SelectionSet[0].(*ast.Field)
if !assert.True(t, ok) {
return
}
assert.Equal(t, "bar", loc2Field.Name)
}
func TestPlanQuery_singleRootObject(t *testing.T) {
t.Parallel()
// the location for the schema
location := "url1"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "allUsers", location)
locations.RegisterURL("User", "firstName", location)
locations.RegisterURL("User", "friends", location)
schema, _ := graphql.LoadSchema(`
type User {
firstName: String!
friends: [User!]!
}
type Query {
allUsers: [User!]!
}
`)
// compute the plan for a query that just hits one service
selections, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: `
{
allUsers {
firstName
friends {
firstName
friends {
firstName
}
}
}
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when building schema: %s", err.Error())
return
}
// the first selection is the only one we care about
rootStep := selections[0].RootStep.Then[0]
// there should only be one selection
if len(rootStep.SelectionSet) != 1 {
t.Error("encountered the wrong number of selections under root step")
return
}
rootField := graphql.SelectedFields(rootStep.SelectionSet)[0]
// make sure that the first step is pointed at the right place
queryer := rootStep.Queryer.(*graphql.SingleRequestQueryer)
assert.Equal(t, location, queryer.URL())
// we need to be asking for allUsers
assert.Equal(t, rootField.Name, "allUsers")
// grab the field from the top level selection
field, ok := rootField.SelectionSet[0].(*ast.Field)
if !ok {
t.Error("Did not get a field out of the allUsers selection")
return
}
// and from all users we need to ask for their firstName
assert.Equal(t, "firstName", field.Name)
assert.Equal(t, "String!", field.Definition.Type.Dump())
// we also should have asked for the friends object
friendsField, ok := rootField.SelectionSet[1].(*ast.Field)
if !ok {
t.Error("Did not get a friends field out of the allUsers selection")
}
// and from all users we need to ask for their firstName
assert.Equal(t, "friends", friendsField.Name)
// look at the selection we've made of friends
firstNameField, ok := friendsField.SelectionSet[0].(*ast.Field)
if !ok {
t.Error("Did not get a field out of the allUsers selection")
}
assert.Equal(t, "firstName", firstNameField.Name)
// there should be a second field for friends
friendsInnerField, ok := friendsField.SelectionSet[1].(*ast.Field)
if !ok {
t.Error("Did not get an inner friends out of the allUsers selection")
}
assert.Equal(t, "friends", friendsInnerField.Name)
// and a field below it for their firstName
firstNameInnerField, ok := friendsInnerField.SelectionSet[0].(*ast.Field)
if !ok {
t.Error("Did not get an inner firstName out of the allUsers selection")
}
assert.Equal(t, "firstName", firstNameInnerField.Name)
}
func TestPlanQuery_subGraphs(t *testing.T) {
t.Parallel()
schema, _ := graphql.LoadSchema(`
type User {
firstName: String!
catPhotos: [CatPhoto!]!
}
type CatPhoto {
URL: String!
owner: User!
}
type Query {
allUsers: [User!]!
}
`)
// the location of the user service
userLocation := "user-location"
// the location of the cat service
catLocation := "cat-location"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "allUsers", userLocation)
locations.RegisterURL("User", "firstName", userLocation)
locations.RegisterURL("User", "catPhotos", catLocation)
locations.RegisterURL("CatPhoto", "URL", catLocation)
locations.RegisterURL("CatPhoto", "owner", userLocation)
plans, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: `
{
allUsers {
firstName
catPhotos {
URL
owner {
firstName
}
}
}
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when building schema: %s", err.Error())
return
}
// there are 3 steps of a single plan that we care about
// the first step is grabbing allUsers and their firstName from the user service
// the second step is grabbing User catPhotos from the cat service
// the third step is grabb CatPhoto.owner.firstName from the user service from the user service
// the first step should have all users
firstStep := plans[0].RootStep.Then[0]
// make sure we are grabbing values off of Query since its the root
assert.Equal(t, typeNameQuery, firstStep.ParentType)
// make sure there's a selection set
if len(firstStep.SelectionSet) != 1 {
t.Error("first step did not have a selection set")
return
}
firstField := graphql.SelectedFields(firstStep.SelectionSet)[0]
// it is resolved against the user service
queryer := firstStep.Queryer.(*graphql.SingleRequestQueryer)
assert.Equal(t, userLocation, queryer.URL())
// make sure it is for allUsers
assert.Equal(t, "allUsers", firstField.Name)
// all users should have one selected value since `catPhotos` is from another service
// there will also be an `id` added so that the query can be stitched together
if len(firstField.SelectionSet) != 2 {
for _, selection := range graphql.SelectedFields(firstField.SelectionSet) {
fmt.Println(selection.Name)
}
t.Error("Encountered incorrext number of fields on allUsers selection set")
return
}
// grab the field from the top level selection
field, ok := firstField.SelectionSet[0].(*ast.Field)
if !ok {
t.Error("Did not get a field out of the allUsers selection")
return
}
// and from all users we need to ask for their firstName
assert.Equal(t, "firstName", field.Name)
assert.Equal(t, "String!", field.Definition.Type.Dump())
// the second step should ask for the cat photo fields
if len(firstStep.Then) != 1 {
t.Errorf("Encountered the wrong number of steps after the first one %v", len(firstStep.Then))
return
}
secondStep := firstStep.Then[0]
// make sure we will insert the step in the right place
assert.Equal(t, []string{"allUsers"}, secondStep.InsertionPoint)
// make sure we are grabbing values off of User since we asked for User.catPhotos
assert.Equal(t, "User", secondStep.ParentType)
// we should be going to the catePhoto servie
queryer = secondStep.Queryer.(*graphql.SingleRequestQueryer)
assert.Equal(t, catLocation, queryer.URL())
// we should only want one field selected
if len(secondStep.SelectionSet) != 1 {
t.Errorf("Did not have the right number of subfields of User.catPhotos: %v", len(secondStep.SelectionSet))
return
}
// make sure we selected the catPhotos field
selectedSecondField := graphql.SelectedFields(secondStep.SelectionSet)[0]
assert.Equal(t, "catPhotos", selectedSecondField.Name)
// we should have also asked for one field underneath
secondSubSelection := graphql.SelectedFields(selectedSecondField.SelectionSet)
if len(secondSubSelection) != 2 {
t.Error("Encountered the incorrect number of fields selected under User.catPhotos")
}
secondSubSelectionField := secondSubSelection[0]
assert.Equal(t, "URL", secondSubSelectionField.Name)
// the third step should ask for the User.firstName
if len(secondStep.Then) != 1 {
t.Errorf("Encountered the wrong number of steps after the second one %v", len(secondStep.Then))
return
}
thirdStep := secondStep.Then[0]
// make sure we will insert the step in the right place
assert.Equal(t, []string{"allUsers", "catPhotos"}, thirdStep.InsertionPoint)
// make sure we are grabbing values off of User since we asked for User.catPhotos
assert.Equal(t, "CatPhoto", thirdStep.ParentType)
// we should be going to the catePhoto service
queryer = thirdStep.Queryer.(*graphql.SingleRequestQueryer)
assert.Equal(t, userLocation, queryer.URL())
// make sure we will insert the step in the right place
assert.Equal(t, []string{"allUsers", "catPhotos"}, thirdStep.InsertionPoint)
// we should only want one field selected
if len(thirdStep.SelectionSet) != 1 {
t.Errorf("Did not have the right number of subfields of User.catPhotos: %v", len(thirdStep.SelectionSet))
return
}
// make sure we selected the catPhotos field
selectedThirdField := graphql.SelectedFields(thirdStep.SelectionSet)[0]
assert.Equal(t, "owner", selectedThirdField.Name)
// we should have also asked for one field underneath
thirdSubSelection := graphql.SelectedFields(selectedThirdField.SelectionSet)
if len(thirdSubSelection) != 1 {
t.Error("Encountered the incorrect number of fields selected under User.catPhotos")
}
thirdSubSelectionField := thirdSubSelection[0]
assert.Equal(t, "firstName", thirdSubSelectionField.Name)
}
func TestPlanQuery_preferParentLocation(t *testing.T) {
t.Parallel()
schema, _ := graphql.LoadSchema(`
type User {
id: ID!
}
type Query {
allUsers: [User!]!
}
`)
// the location of the user service
userLocation := "user-location"
// the location of the cat service
catLocation := "cat-location"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "allUsers", userLocation)
// add the
locations.RegisterURL("User", "id", catLocation)
locations.RegisterURL("User", "id", userLocation)
plans, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: `
{
allUsers {
id
}
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when building schema: %s", err.Error())
return
}
// there should only be 1 step to this query
// the first step should have all users
firstStep := plans[0].RootStep.Then[0]
// make sure we are grabbing values off of Query since its the root
assert.Equal(t, typeNameQuery, firstStep.ParentType)
// make sure there's a selection set
if len(firstStep.Then) != 0 {
t.Errorf("There shouldn't be any dependent step on this one. Found %v.", len(firstStep.Then))
return
}
}
func TestPlanQuery_scrubFields(t *testing.T) {
t.Parallel()
schema, _ := graphql.LoadSchema(`
type User {
id: ID!
firstName: String!
favoriteCatSpecies: String!
catPhotos: [CatPhoto!]!
}
type CatPhoto {
URL: String!
owner: User!
}
type Query {
allUsers: [User!]!
}
`)
// the location of the user service
userLocation := "user-location"
// the location of the cat service
catLocation := "cat-location"
// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL(typeNameQuery, "allUsers", userLocation)
locations.RegisterURL("CatPhoto", "owner", userLocation)
locations.RegisterURL("User", "firstName", userLocation)
locations.RegisterURL("User", "favoriteCatSpecies", catLocation)
locations.RegisterURL("User", "catPhotos", catLocation)
locations.RegisterURL("User", "id", catLocation, userLocation)
locations.RegisterURL("CatPhoto", "URL", catLocation)
t.Run("Multiple Step Scrubbing", func(t *testing.T) {
t.Parallel()
plans, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: `
{
allUsers {
catPhotos {
owner {
firstName
}
}
}
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
if err != nil {
t.Error(err.Error())
return
}
// each transition between step requires an id field. None of them were requested so we should have two
// places where we want to scrub it
assert.Equal(t, map[string][][]string{
"id": {
{"allUsers"},
{"allUsers", "catPhotos"},
},
}, plans[0].FieldsToScrub)
})
t.Run("Single Step no Scrubbing", func(t *testing.T) {
t.Parallel()
plans, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: `
{
allUsers {
firstName
}
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
if err != nil {
t.Error(err.Error())
return
}
// each transition between step requires an id field. None of them were requested so we should have two
// places where we want to scrub it
assert.Equal(t, map[string][][]string{
"id": {},
}, plans[0].FieldsToScrub)
})
t.Run("Existing id", func(t *testing.T) {
t.Parallel()
plans, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: `
{
allUsers {
id
catPhotos {
owner {
firstName
}
}
}
}
`,
Schema: schema,
Locations: locations,
Gateway: &Gateway{logger: &DefaultLogger{}},
})
if err != nil {
t.Error(err.Error())
return
}
// each transition between step requires an id field. None of them were requested so we should have two
// places where we want to scrub it
assert.Equal(t, map[string][][]string{
"id": {
{"allUsers", "catPhotos"},
},
}, plans[0].FieldsToScrub)
})
}
func TestPlanQuery_groupSiblings(t *testing.T) {
t.Parallel()
schema, _ := graphql.LoadSchema(`
type User {
favoriteCatSpecies: String!
catPhotos: [CatPhoto!]!
}