-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathhints.test.ts
1295 lines (1115 loc) · 33.6 KB
/
hints.test.ts
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
import { asFed2SubgraphDocument, buildSubgraph, Subgraphs } from '@apollo/federation-internals';
import { DocumentNode } from 'graphql';
import gql from 'graphql-tag';
import {
HintCodeDefinition,
HINTS,
} from '../hints';
import { MergeResult, mergeSubgraphs } from '../merging';
import { composeAsFed2Subgraphs } from './testHelper';
import { formatExpectedToMatchReceived } from './matchers/toMatchString';
import { composeServices } from '../compose';
function mergeDocuments(...documents: DocumentNode[]): MergeResult {
const subgraphs = new Subgraphs();
let i = 1;
for (const doc of documents) {
const name = `Subgraph${i++}`;
try {
subgraphs.add(buildSubgraph(name, `https://${name}`, asFed2SubgraphDocument(doc)));
} catch (e) {
throw new Error(e.toString());
}
}
return mergeSubgraphs(subgraphs);
}
declare global {
namespace jest {
interface Matchers<R> {
toRaiseHint(id: HintCodeDefinition, message: string, coordinate: string | undefined): R;
toNotRaiseHints(): R;
}
}
}
expect.extend({
toRaiseHint(mergeResult: MergeResult, expectedDefinition: HintCodeDefinition, expectedMessage: string, expectedCoordinate: string | undefined) {
if (mergeResult.errors) {
return {
message: () => `Expected subgraphs to merge but got errors: [${mergeResult.errors.map(e => e.message).join(', ')}]`,
pass: false
};
}
const hints = mergeResult.hints;
const expectedCode = expectedDefinition.code;
const matchingHints = hints.filter(h => h.definition.code === expectedCode);
if (matchingHints.length === 0) {
const details = hints.length === 0
? 'no hint was raised'
: `hints were raised with code(s): ${hints.map(h => h.definition.code).join(', ')}`;
return {
message: () => `Expected subgraphs merging to raise a ${expectedCode} hint, but ${details}`,
pass: false
};
}
for (const hint of matchingHints) {
const received = hint.message;
const receivedCoordinate = hint.coordinate
const expected = formatExpectedToMatchReceived(expectedMessage, received);
if (this.equals(expected, received) && this.equals(expectedCoordinate, receivedCoordinate)) {
return {
message: () => `Expected subgraphs merging to not raise hint ${expectedCode} with message '${expected}', but it did`,
pass: true,
}
}
}
if (matchingHints.length === 1) {
const receivedMessage = matchingHints[0].message;
const receivedCoordinate = matchingHints[0].coordinate;
const message = formatExpectedToMatchReceived(expectedMessage, receivedMessage);
const coordinate = formatExpectedToMatchReceived(expectedCoordinate || '', receivedCoordinate || '');
return {
message: () => (
this.utils.matcherHint('toRaiseHint', undefined, undefined,)
+ '\n\n'
+ `Found hint matching code ${expectedCode}, but messages or coordinates don't match:\n`
+ this.utils.printDiffOrStringify(message, receivedMessage, 'Expected', 'Received', true)
+ "\n\n"
+ this.utils.printDiffOrStringify(coordinate, receivedCoordinate, 'Expected', 'Received', true)
),
pass: false,
};
}
return {
message: () => (
this.utils.matcherHint('toRaiseHint', undefined, undefined,)
+ '\n\n'
+ `Found ${matchingHints.length} hint(s) matching code ${expectedCode}, but none had the expected message or coordinate:\n`
+ matchingHints.map((h, i) => {
const received = h.message;
const expected = formatExpectedToMatchReceived(expectedMessage, received);
return `Hint ${i}:\n`
+ this.utils.printDiffOrStringify(expected, received, 'Expected', 'Received', true)
}).join('\n\n')
+ matchingHints.map((h, i) => {
const received = h.coordinate;
const expected = formatExpectedToMatchReceived(expectedMessage, received || '');
return `Hint ${i}:\n`
+ this.utils.printDiffOrStringify(expected, received, 'Expected', 'Received', true)
}).join('\n\n')
),
pass: false,
}
},
toNotRaiseHints(mergeResult: MergeResult) {
if (mergeResult.errors) {
return {
message: () => `Expected subgraphs to merge but got errors: [${mergeResult.errors.map(e => e.message).join(', ')}]`,
pass: false
};
}
const hints = mergeResult.hints;
if (hints.length > 0) {
return {
message: () => `Expected subgraphs merging to NOT raise any hints, but got:\n - ${hints.map((h) => h.toString()).join('\n - ')}`,
pass: false
};
}
return {
message: () => "You're negating a negative method? Instead of using `toRaiseHint`? Do you want to talk about it?",
pass: true
};
}
});
test('hints on merging field with nullable and non-nullable types', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @shareable {
f: String
}
`;
const subgraph2 = gql`
type T @shareable {
f: String!
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_BUT_COMPATIBLE_FIELD_TYPE,
'Type of field "T.f" is inconsistent but compatible across subgraphs: '
+ 'will use type "String" (from subgraph "Subgraph1") in supergraph but "T.f" has subtype "String!" in subgraph "Subgraph2".',
'T.f'
);
})
test('hints on merging field with subtype types', () => {
const subgraph1 = gql`
type Query {
a: Int
}
interface I {
v: Int
}
type Impl implements I @shareable {
v: Int
}
type T @shareable {
f: I
}
`;
const subgraph2 = gql`
interface I {
v: Int
}
type Impl implements I @shareable {
v: Int
}
type T @shareable {
f: Impl
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_BUT_COMPATIBLE_FIELD_TYPE,
'Type of field "T.f" is inconsistent but compatible across subgraphs: '
+ 'will use type "I" (from subgraph "Subgraph1") in supergraph but "T.f" has subtype "Impl" in subgraph "Subgraph2".',
'T.f'
);
})
test('hints on merging argument with nullable and non-nullable types', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @shareable {
f(a: String!): String
}
`;
const subgraph2 = gql`
type T @shareable {
f(a: String): String
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_BUT_COMPATIBLE_ARGUMENT_TYPE,
'Type of argument "T.f(a:)" is inconsistent but compatible across subgraphs: '
+ 'will use type "String!" (from subgraph "Subgraph1") in supergraph but "T.f(a:)" has supertype "String" in subgraph "Subgraph2".',
'T.f(a:)'
);
})
test('hints on merging argument with default value in only some subgraph', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @shareable {
f(a: String = "foo"): String
}
`;
const subgraph2 = gql`
type T @shareable {
f(a: String): String
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_DEFAULT_VALUE_PRESENCE,
'Argument "T.f(a:)" has a default value in only some subgraphs: '
+ 'will not use a default in the supergraph (there is no default in subgraph "Subgraph2") but "T.f(a:)" has default value "foo" in subgraph "Subgraph1".',
'T.f(a:)'
);
})
test('hints on object being an entity in only some subgraph', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @key(fields: "k") {
k: Int
v1: String
}
`;
const subgraph2 = gql`
type T @shareable {
k: Int
v2: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_ENTITY,
'Type "T" is declared as an entity (has a @key applied) in some but not all defining subgraphs: '
+ 'it has no @key in subgraph "Subgraph2" but has some @key in subgraph "Subgraph1".',
'T'
);
})
test('hints on field of object value type not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @shareable {
a: Int
b: Int
}
`;
const subgraph2 = gql`
type T @shareable {
a: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_OBJECT_VALUE_TYPE_FIELD,
'Field "T.b" of non-entity object type "T" is defined in some but not all subgraphs that define "T": '
+ '"T.b" is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'T'
);
})
test('hints on field of interface value type not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
a: Int
}
interface T {
a: Int
b: Int
}
`;
const subgraph2 = gql`
interface T {
a: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_INTERFACE_VALUE_TYPE_FIELD,
'Field "T.b" of interface type "T" is defined in some but not all subgraphs that define "T": '
+ '"T.b" is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'T'
);
})
test('*No* hint on field of interface _with @key_ not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
a: Int
}
interface T @key(fields: "id") {
id: ID!
a: Int
b: Int
}
`;
const subgraph2 = gql`
type T @interfaceObject @key(fields: "id") {
id: ID!
a: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toNotRaiseHints();
})
test('hints on field of input object value type not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
a: Int
}
input T {
a: Int
b: Int
}
`;
const subgraph2 = gql`
input T {
a: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_INPUT_OBJECT_FIELD,
'Input object field "b" will not be added to "T" in the supergraph as it does not appear in all subgraphs: it is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'T.b'
);
})
test('hints on union member not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
a: Int
}
union T = A | B | C
type A @shareable {
a: Int
}
type B {
b: Int
}
type C @shareable {
b: Int
}
`;
const subgraph2 = gql`
union T = A | C
type A @shareable {
a: Int
}
type C @shareable {
b: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_UNION_MEMBER,
'Union type "T" includes member type "B" in some but not all defining subgraphs: '
+ '"B" is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'T'
);
})
test('hints on enum type not being used', () => {
const subgraph1 = gql`
type Query {
a: Int
}
enum T {
V1
V2
}
`;
const subgraph2 = gql`
enum T {
V1
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.UNUSED_ENUM_TYPE,
'Enum type "T" is defined but unused. It will be included in the supergraph with all the values appearing in any subgraph ("as if" it was only used as an output type).',
'T'
);
})
test('hints on enum value of input enum type not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
a(t: T): Int
}
enum T {
V1
V2
}
`;
const subgraph2 = gql`
enum T {
V1
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_ENUM_VALUE_FOR_INPUT_ENUM,
'Value "V2" of enum type "T" will not be part of the supergraph as it is not defined in all the subgraphs defining "T": '
+ '"V2" is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'T.V2'
);
})
test('hints on enum value of output enum type not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
t: T
}
enum T {
V1
V2
}
`;
const subgraph2 = gql`
enum T {
V1
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_ENUM_VALUE_FOR_OUTPUT_ENUM,
'Value "V2" of enum type "T" has been added to the supergraph but is only defined in a subset of the subgraphs defining "T": '
+ '"V2" is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'T.V2'
);
})
// Skipped for now because we don't merge any type system directives and so
// this cannot be properly tested.
test.skip('hints on type system directives having inconsistent repeatable', () => {
const subgraph1 = gql`
type Query {
a: Int
}
directive @tag(name: String!) repeatable on FIELD_DEFINITION
`;
const subgraph2 = gql`
directive @tag(name: String!) on FIELD_DEFINITION
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_TYPE_SYSTEM_DIRECTIVE_REPEATABLE,
'Type system directive "@tag" is marked repeatable in the supergraph but it is inconsistently marked repeatable in subgraphs: '
+ 'it is repeatable in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'@tag'
);
})
// Skipped for now because we don't merge any type system directives and so
// this cannot be properly tested.
test.skip('hints on type system directives having inconsistent locations', () => {
// Same as above, we kind of have to use tag.
const subgraph1 = gql`
type Query {
a: Int
}
directive @tag(name: String!) on FIELD_DEFINITION
`;
const subgraph2 = gql`
directive @tag(name: String!) on INTERFACE
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_TYPE_SYSTEM_DIRECTIVE_LOCATIONS,
'Type system directive "@tag" has inconsistent locations across subgraphs '
+ 'and will use locations "FIELD_DEFINITION, INTERFACE" (union of all subgraphs) in the supergraph, but has: '
+ 'location "FIELD_DEFINITION" in subgraph "Subgraph1" and location "INTERFACE" in subgraph "Subgraph2".',
'@tag'
);
})
test('hints on executable directives not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
a: Int
}
directive @t repeatable on QUERY
`;
const subgraph2 = gql`
scalar s
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_EXECUTABLE_DIRECTIVE_PRESENCE,
'Executable directive "@t" will not be part of the supergraph as it does not appear in all subgraphs: '
+ 'it is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'@t'
);
})
test('hints on executable directives having no locations intersection', () => {
const subgraph1 = gql`
type Query {
a: Int
}
directive @t on QUERY
`;
const subgraph2 = gql`
directive @t on FIELD
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.NO_EXECUTABLE_DIRECTIVE_LOCATIONS_INTERSECTION,
'Executable directive "@t" has no location that is common to all subgraphs: '
+ 'it will not appear in the supergraph as there no intersection between location "QUERY" in subgraph "Subgraph1" and location "FIELD" in subgraph "Subgraph2".',
'@t'
);
})
test('hints on executable directives having inconsistent repeatable', () => {
const subgraph1 = gql`
type Query {
a: Int
}
directive @t repeatable on QUERY
`;
const subgraph2 = gql`
directive @t on QUERY
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_EXECUTABLE_DIRECTIVE_REPEATABLE,
'Executable directive "@t" will not be marked repeatable in the supergraph as it is inconsistently marked repeatable in subgraphs: '
+ 'it is not repeatable in subgraph "Subgraph2" but is repeatable in subgraph "Subgraph1".',
'@t'
);
})
test('hints on executable directives having inconsistent locations', () => {
const subgraph1 = gql`
type Query {
a: Int
}
directive @t on QUERY | FIELD
`;
const subgraph2 = gql`
directive @t on FIELD
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_EXECUTABLE_DIRECTIVE_LOCATIONS,
'Executable directive "@t" has inconsistent locations across subgraphs '
+ 'and will use location "FIELD" (intersection of all subgraphs) in the supergraph, but has: '
+ 'location "FIELD" in subgraph "Subgraph2" and locations "FIELD, QUERY" in subgraph "Subgraph1".',
'@t'
);
})
test('hints on executable directives argument not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
a: Int
}
directive @t(a: Int) on FIELD
`;
const subgraph2 = gql`
directive @t on FIELD
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_ARGUMENT_PRESENCE,
'Optional argument "@t(a:)" will not be included in the supergraph as it does not appear in all subgraphs: '
+ 'it is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'@t(a:)'
);
})
test('hints on field argument not being in all subgraphs', () => {
const subgraph1 = gql`
type Query {
f(a: Int): Int @shareable
}
`;
const subgraph2 = gql`
type Query {
f: Int @shareable
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_ARGUMENT_PRESENCE,
'Optional argument "Query.f(a:)" will not be included in the supergraph as it does not appear in all subgraphs: '
+ 'it is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'Query.f(a:)'
);
})
test('hints on inconsistent description for schema definition', () => {
const subgraph1 = gql`
"""
Queries to the API
- a: gives you a int
"""
schema {
query: Query
}
type Query {
a: Int
}
`;
const subgraph2 = gql`
"""
Entry point for the API
"""
schema {
query: Query
}
type Query {
b: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_DESCRIPTION,
'The schema definition has inconsistent descriptions across subgraphs. The supergraph will use description (from subgraph "Subgraph1"):\n'
+ ' """\n'
+ ' Queries to the API\n'
+ ' - a: gives you a int\n'
+ ' """\n'
+ 'In subgraph "Subgraph2", the description is:\n'
+ ' """\n'
+ ' Entry point for the API\n'
+ ' """',
undefined
);
})
test('hints on inconsistent description for field', () => {
// We make sure the 2nd and 3rd subgraphs have the same description to
// ensure it's the one that gets picked.
const subgraph1 = gql`
type Query {
a: Int
}
type T @shareable {
"I don't know what I'm doing"
f: Int
}
`;
const subgraph2 = gql`
type T @shareable {
"Return a super secret integer"
f: Int
}
`;
const subgraph3 = gql`
type T @shareable {
"""
Return a super secret integer
"""
f: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2, subgraph3);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_DESCRIPTION,
'Element "T.f" has inconsistent descriptions across subgraphs. The supergraph will use description (from subgraphs "Subgraph2" and "Subgraph3"):\n'
+ ' """\n'
+ ' Return a super secret integer\n'
+ ' """\n'
+ 'In subgraph "Subgraph1", the description is:\n'
+ ' """\n'
+ ' I don\'t know what I\'m doing\n'
+ ' """',
'T.f'
);
});
describe('hint tests related to the @override directive', () => {
it('hint when from subgraph does not exist', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @key(fields: "id"){
id: Int
f: Int @override(from: "Subgraph3")
}
`;
const subgraph2 = gql`
type T @key(fields: "id"){
id: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.FROM_SUBGRAPH_DOES_NOT_EXIST,
`Source subgraph "Subgraph3" for field "T.f" on subgraph "Subgraph1" does not exist. Did you mean "Subgraph1" or "Subgraph2"?`,
'T.f'
);
});
it('hint when @override directive can be removed', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @key(fields: "id"){
id: Int
f: Int @override(from: "Subgraph2")
}
`;
const subgraph2 = gql`
type T @key(fields: "id"){
id: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.OVERRIDE_DIRECTIVE_CAN_BE_REMOVED,
`Field "T.f" on subgraph "Subgraph1" no longer exists in the from subgraph. The @override directive can be removed.`,
'T.f'
);
});
it('hint overridden field can be removed', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @key(fields: "id"){
id: Int
f: Int @override(from: "Subgraph2")
}
`;
const subgraph2 = gql`
type T @key(fields: "id"){
id: Int
f: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.OVERRIDDEN_FIELD_CAN_BE_REMOVED,
`Field "T.f" on subgraph "Subgraph2" is overridden. Consider removing it.`,
'T.f'
);
});
it('hint overridden field can be made external', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @key(fields: "id"){
id: Int @override(from: "Subgraph2")
}
`;
const subgraph2 = gql`
type T @key(fields: "id"){
id: Int
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.OVERRIDDEN_FIELD_CAN_BE_REMOVED,
`Field "T.id" on subgraph "Subgraph2" is overridden. It is still used in some federation directive(s) (@key, @requires, and/or @provides) and/or to satisfy interface constraint(s), but consider marking it @external explicitly or removing it along with its references.`,
'T.id'
);
});
it('hint when @override directive can be removed because overridden field has been marked external', () => {
const subgraph1 = gql`
type Query {
a: Int
}
type T @key(fields: "id"){
id: Int @override(from: "Subgraph2")
f: Int
}
`;
const subgraph2 = gql`
type T @key(fields: "id"){
id: Int @external
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.OVERRIDE_DIRECTIVE_CAN_BE_REMOVED,
`Field "T.id" on subgraph "Subgraph1" is not resolved anymore by the from subgraph (it is marked "@external" in "Subgraph2"). The @override directive can be removed.`,
'T.id'
);
});
});
describe('on non-repeatable directives used with incompatible arguments', () => {
it('does _not_ warn when subgraphs have the same arguments', () => {
const subgraph1 = gql`
type Query {
a: String @shareable @deprecated(reason: "because")
}
`;
const subgraph2 = gql`
type Query {
a: String @shareable @deprecated(reason: "because")
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toNotRaiseHints();
});
it('does _not_ warn when subgraphs all use the same arguments defaults', () => {
const subgraph1 = gql`
type Query {
a: String @shareable @deprecated
}
`;
const subgraph2 = gql`
type Query {
a: String @shareable @deprecated
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toNotRaiseHints();
});
it('does _not_ warn if a subgraph uses the argument default and othe pass an argument, but it is the default', () => {
const subgraph1 = gql`
type Query {
a: String @shareable @deprecated(reason: "No longer supported")
}
`;
const subgraph2 = gql`
type Query {
a: String @shareable @deprecated
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toNotRaiseHints();
});
it('warns if a subgraph use a default argument but the other use the (different) default ', () => {
const subgraph1 = gql`
type Query {
a: String @shareable @deprecated(reason: "bad")
}
`;
const subgraph2 = gql`
type Query {
a: String @shareable @deprecated
}
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_NON_REPEATABLE_DIRECTIVE_ARGUMENTS,
'Non-repeatable directive @deprecated is applied to "Query.a" in multiple subgraphs but with incompatible arguments. '
+ 'The supergraph will use arguments {reason: "bad"} (from subgraph "Subgraph1"), but found no arguments in subgraph "Subgraph2".',
'Query.a'
);
});
it('warns if subgraphs use a different argument', () => {
// Note: using @specifiedBy for variety and to illustrate that nothing we test here is specific to @deprecated.
const subgraph1 = gql`
type Query {
f: Foo
}
scalar Foo @specifiedBy(url: "http://FooSpec.com")
`;
const subgraph2 = gql`
scalar Foo @specifiedBy(url: "http://BarSpec.com")
`;
const result = mergeDocuments(subgraph1, subgraph2);
expect(result).toRaiseHint(
HINTS.INCONSISTENT_NON_REPEATABLE_DIRECTIVE_ARGUMENTS,
'Non-repeatable directive @specifiedBy is applied to "Foo" in multiple subgraphs but with incompatible arguments. '
+ 'The supergraph will use arguments {url: "http://FooSpec.com"} (from subgraph "Subgraph1"), but found arguments {url: "http://BarSpec.com"} in subgraph "Subgraph2".',
'Foo'
);
});