apollo-federation 2.13.1

Apollo Federation
Documentation
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
use super::ServiceDefinition;
use super::assert_composition_errors;
use super::compose_as_fed2_subgraphs;

mod requires_tests {
    use super::*;

    #[test]
    fn fails_if_it_cannot_satisfy_a_requires() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type Query {
                    a: A
                }

                type A @key(fields: "id") {
                    id: ID!
                    x: Int
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type A @key(fields: "id") {
                    id: ID! @external
                    x: Int @external
                    y: Int @requires(fields: "x")
                    z: Int @requires(fields: "x")
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[
                (
                    "SATISFIABILITY_ERROR",
                    r#"
                    The following supergraph API query:
                    {
                      a {
                        y
                      }
                    }
                    cannot be satisfied by the subgraphs because:
                    - from subgraph "A": cannot find field "A.y".
                    - from subgraph "B": cannot satisfy @requires conditions on field "A.y" (please ensure that this is not due to key field "id" being accidentally marked @external).
                    "#,
                ),
                (
                    "SATISFIABILITY_ERROR",
                    r#"
                    The following supergraph API query:
                    {
                      a {
                        z
                      }
                    }
                    cannot be satisfied by the subgraphs because:
                    - from subgraph "A": cannot find field "A.z".
                    - from subgraph "B": cannot satisfy @requires conditions on field "A.z" (please ensure that this is not due to key field "id" being accidentally marked @external).
                    "#,
                ),
            ],
        );
    }

    #[test]
    fn fails_if_no_usable_post_requires_keys() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type T1 @key(fields: "id") {
                    id: Int!
                    f1: String
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type Query {
                    getT1s: [T1]
                }

                type T1 {
                    id: Int! @shareable
                    f1: String @external
                    f2: T2! @requires(fields: "f1")
                }

                type T2 {
                    a: String
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[(
                "SATISFIABILITY_ERROR",
                r#"
                The following supergraph API query:
                {
                  getT1s {
                    f2 {
                      ...
                    }
                  }
                }
                cannot be satisfied by the subgraphs because:
                - from subgraph "B": @requires condition on field "T1.f2" can be satisfied but missing usable key on "T1" in subgraph "B" to resume query.
                - from subgraph "A": cannot find field "T1.f2".
                "#,
            )],
        );
    }
}

mod non_resolvable_keys_tests {
    use super::*;

    #[test]
    fn fails_if_key_is_declared_non_resolvable_but_would_be_needed() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type T @key(fields: "id", resolvable: false) {
                    id: ID!
                    f: String
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type Query {
                    getTs: [T]
                }

                type T @key(fields: "id") {
                    id: ID!
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[(
                "SATISFIABILITY_ERROR",
                r#"
                The following supergraph API query:
                {
                  getTs {
                    f
                  }
                }
                cannot be satisfied by the subgraphs because:
                - from subgraph "B":
                  - cannot find field "T.f".
                  - cannot move to subgraph "A", which has field "T.f", because none of the @key defined on type "T" in subgraph "A" are resolvable (they are all declared with their "resolvable" argument set to false).
                "#,
            )],
        );
    }
}

mod interface_object_tests {
    use super::*;

    #[ignore = "Error message is missing message part about interface object usage"]
    #[test]
    fn fails_on_interface_object_usage_with_missing_key_on_interface() {
        let subgraph_a = ServiceDefinition {
            name: "subgraphA",
            type_defs: r#"
                interface I {
                    id: ID!
                    x: Int
                }

                type A implements I @key(fields: "id") {
                    id: ID!
                    x: Int
                }

                type B implements I @key(fields: "id") {
                    id: ID!
                    x: Int
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "subgraphB",
            type_defs: r#"
                type Query {
                    iFromB: I
                }

                type I @interfaceObject @key(fields: "id") {
                    id: ID!
                    y: Int
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[
                (
                    "SATISFIABILITY_ERROR",
                    r#"
                    The following supergraph API query:
                    {
                      iFromB {
                        ... on A {
                          ...
                        }
                      }
                    }
                    cannot be satisfied by the subgraphs because:
                    - from subgraph "subgraphB": no subgraph can be reached to resolve the implementation type of @interfaceObject type "I".
                    "#,
                ),
                (
                    "SATISFIABILITY_ERROR",
                    r#"
                    The following supergraph API query:
                    {
                      iFromB {
                        ... on B {
                          ...
                        }
                      }
                    }
                    cannot be satisfied by the subgraphs because:
                    - from subgraph "subgraphB": no subgraph can be reached to resolve the implementation type of @interfaceObject type "I".
                    "#,
                ),
            ],
        );
    }

    #[test]
    fn fails_on_interface_object_with_some_unreachable_implementation() {
        let subgraph_a = ServiceDefinition {
            name: "subgraphA",
            type_defs: r#"
                interface I @key(fields: "id") {
                    id: ID!
                    x: Int
                }

                type A implements I @key(fields: "id") {
                    id: ID!
                    x: Int
                }

                type B implements I @key(fields: "id") {
                    id: ID!
                    x: Int
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "subgraphB",
            type_defs: r#"
                type Query {
                    iFromB: I
                }

                type I @interfaceObject @key(fields: "id") {
                    id: ID!
                    y: Int
                }
            "#,
        };

        let subgraph_c = ServiceDefinition {
            name: "subgraphC",
            type_defs: r#"
                type A {
                    z: Int
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b, subgraph_c]);
        assert_composition_errors(
            &result,
            &[(
                "SATISFIABILITY_ERROR",
                r#"
                The following supergraph API query:
                {
                  iFromB {
                    ... on A {
                      z
                    }
                  }
                }
                cannot be satisfied by the subgraphs because:
                - from subgraph "subgraphB":
                  - cannot find implementation type "A" (supergraph interface "I" is declared with @interfaceObject in "subgraphB").
                  - cannot move to subgraph "subgraphC", which has field "A.z", because interface "I" is not defined in this subgraph (to jump to "subgraphC", it would need to both define interface "I" and have a @key on it).
                - from subgraph "subgraphA":
                  - cannot find field "A.z".
                  - cannot move to subgraph "subgraphC", which has field "A.z", because type "A" has no @key defined in subgraph "subgraphC".
                "#,
            )],
        );
    }
}

// when shared field has non-intersecting runtime types in different subgraphs
mod shared_field_runtime_types_tests {
    use super::*;

    #[test]
    fn errors_for_interfaces() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type Query {
                    a: A @shareable
                }

                interface A {
                    x: Int
                }

                type I1 implements A {
                    x: Int
                    i1: Int
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type Query {
                    a: A @shareable
                }

                interface A {
                    x: Int
                }

                type I2 implements A {
                    x: Int
                    i2: Int
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[(
                "SHAREABLE_HAS_MISMATCHED_RUNTIME_TYPES",
                r#"
                For the following supergraph API query:
                {
                  a {
                    ...
                  }
                }
                Shared field "Query.a" return type "A" has a non-intersecting set of possible runtime types across subgraphs. Runtime types in subgraphs are:
                 - in subgraph "A", type "I1";
                 - in subgraph "B", type "I2".
                This is not allowed as shared fields must resolve the same way in all subgraphs, and that implies at least some common runtime types between the subgraphs.
                "#,
            )],
        );
    }

    #[test]
    fn errors_for_unions() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type Query {
                    e: E! @shareable
                }

                type E @key(fields: "id") {
                    id: ID!
                    s: U! @shareable
                }

                union U = A | B

                type A {
                    a: Int
                }

                type B {
                    b: Int
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type E @key(fields: "id") {
                    id: ID!
                    s: U! @shareable
                }

                union U = C | D

                type C {
                    c: Int
                }

                type D {
                    d: Int
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[(
                "SHAREABLE_HAS_MISMATCHED_RUNTIME_TYPES",
                r#"
                For the following supergraph API query:
                {
                  e {
                    s {
                      ...
                    }
                  }
                }
                Shared field "E.s" return type "U!" has a non-intersecting set of possible runtime types across subgraphs. Runtime types in subgraphs are:
                 - in subgraph "A", types "A" and "B";
                 - in subgraph "B", types "C" and "D".
                This is not allowed as shared fields must resolve the same way in all subgraphs, and that implies at least some common runtime types between the subgraphs.
                "#,
            )],
        );
    }
}

mod shareable_mutation_fields_tests {
    use super::*;

    #[test]
    fn errors_when_queries_may_require_multiple_calls_to_mutation_field() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type Query {
                    dummy: Int
                }

                type Mutation {
                    f: F @shareable
                }

                type F {
                    x: Int
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type Mutation {
                    f: F @shareable
                }

                type F {
                    y: Int
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[(
                "SATISFIABILITY_ERROR",
                r#"
                Supergraph API queries using the mutation field "Mutation.f" at top-level must be satisfiable without needing to call that field from multiple subgraphs, but every subgraph with that field encounters satisfiability errors. Please fix these satisfiability errors for (at least) one of the following subgraphs with the mutation field:
                - When calling "Mutation.f" at top-level from subgraph "A":
                  The following supergraph API query:
                  mutation {
                    f {
                      y
                    }
                  }
                  cannot be satisfied by the subgraphs because:
                  - from subgraph "A":
                    - cannot find field "F.y".
                    - cannot move to subgraph "B", which has field "F.y", because type "F" has no @key defined in subgraph "B".
                - When calling "Mutation.f" at top-level from subgraph "B":
                  The following supergraph API query:
                  mutation {
                    f {
                      x
                    }
                  }
                  cannot be satisfied by the subgraphs because:
                  - from subgraph "B":
                    - cannot find field "F.x".
                    - cannot move to subgraph "A", which has field "F.x", because type "F" has no @key defined in subgraph "A".
                "#,
            )],
        );
    }

    #[test]
    fn errors_normally_for_mutation_fields_that_are_not_actually_shared() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type Query {
                    dummy: Int
                }

                type Mutation {
                    f: F @shareable
                }

                type F @key(fields: "id") {
                    id: ID!
                    x: Int
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type F @key(fields: "id", resolvable: false) {
                    id: ID!
                    y: Int
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[(
                "SATISFIABILITY_ERROR",
                r#"
                The following supergraph API query:
                mutation {
                  f {
                    y
                  }
                }
                cannot be satisfied by the subgraphs because:
                - from subgraph "A":
                  - cannot find field "F.y".
                  - cannot move to subgraph "B", which has field "F.y", because none of the @key defined on type "F" in subgraph "B" are resolvable (they are all declared with their "resolvable" argument set to false).
                "#,
            )],
        );
    }

    #[test]
    fn succeeds_when_queries_do_not_require_multiple_calls_to_mutation_field() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type Query {
                    dummy: Int
                }

                type Mutation {
                    f: F @shareable
                }

                type F @key(fields: "id") {
                    id: ID!
                    x: Int
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type Mutation {
                    f: F @shareable
                }

                type F @key(fields: "id", resolvable: false) {
                    id: ID!
                    y: Int
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert!(
            result.is_ok(),
            "Expected successful composition, but got errors: {:?}",
            result.err()
        );
    }
}

mod other_validation_errors_tests {
    use super::*;

    #[ignore = "until merge implementation completed"]
    #[test]
    fn errors_when_max_validation_subgraph_paths_is_exceeded() {
        let subgraph_a = ServiceDefinition {
            name: "A",
            type_defs: r#"
                type Query {
                    a: A
                }

                type A @key(fields: "id") {
                    id: ID!
                    b: B
                    c: C
                    d: D
                }

                type B @key(fields: "id") {
                    id: ID!
                    a: A @shareable
                    b: Int @shareable
                    c: C @shareable
                    d: D @shareable
                }

                type C @key(fields: "id") {
                    id: ID!
                    a: A @shareable
                    b: B @shareable
                    c: Int @shareable
                    d: D @shareable
                }

                type D @key(fields: "id") {
                    id: ID!
                    a: A @shareable
                    b: B @shareable
                    c: C @shareable
                    d: Int @shareable
                }
            "#,
        };

        let subgraph_b = ServiceDefinition {
            name: "B",
            type_defs: r#"
                type B @key(fields: "id") {
                    id: ID!
                    b: Int @shareable
                    c: C @shareable
                    d: D @shareable
                }

                type C @key(fields: "id") {
                    id: ID!
                    b: B @shareable
                    c: Int @shareable
                    d: D @shareable
                }

                type D @key(fields: "id") {
                    id: ID!
                    b: B @shareable
                    c: C @shareable
                    d: Int @shareable
                }
            "#,
        };

        let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
        assert_composition_errors(
            &result,
            &[(
                "SATISFIABILITY_ERROR",
                "Maximum number of validation subgraph paths exceeded: 12",
            )],
        );
    }
}