apollo-federation 2.14.0

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
use test_log::test;

use super::ServiceDefinition;
use super::assert_composition_errors;
use super::compose_as_fed2_subgraphs;

// =============================================================================
// MERGE VALIDATIONS - Tests for validation during the merge phase
// =============================================================================

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

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type A {
          b: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "INVALID_GRAPHQL",
            r#"[subgraphA] Error: cannot find type `A` in this document
   ╭─[ subgraphA:3:14 ]
   │
 3 │           a: A
   │              ┬  
   │              ╰── not found in this scope
───╯
"#,
        )],
    );
}

#[test]
fn merge_validations_errors_when_subgraph_has_introspection_reserved_name() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          __someQuery: Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type Query {
          aValidOne: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "INVALID_GRAPHQL",
            r#"[subgraphA] Error: a field cannot be named `__someQuery` as names starting with two underscores are reserved
   ╭─[ subgraphA:3:11 ]
   │
 3 │           __someQuery: Int
   │           ─────┬─────  
   │                ╰─────── Pick a different name here
───╯
"#,
        )],
    );
}

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

        directive @tag on ENUM_VALUE
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type A {
          b: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "DIRECTIVE_DEFINITION_INVALID",
            r#"[subgraphA] Invalid definition for directive "@tag": missing required argument "name""#,
        )],
    );
}

#[test]
fn merge_validations_reject_subgraph_named_underscore() {
    let subgraph_a = ServiceDefinition {
        name: "_",
        type_defs: r#"
        type Query {
          a: String
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type A {
          b: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "INVALID_SUBGRAPH_NAME",
            "[_] Invalid name _ for a subgraph: this name is reserved",
        )],
    );
}

#[test]
fn merge_validations_reject_if_no_subgraphs_have_query() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type A {
          a: Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type B {
          b: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "QUERY_ROOT_MISSING",
            "No queries found in any subgraph: a supergraph must have a query root type.",
        )],
    );
}

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

        type A {
          a: Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        interface A {
          b: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "TYPE_KIND_MISMATCH",
            r#"Type "A" has mismatched kind: it is defined as Object Type in subgraph "subgraphA" but Interface Type in subgraph "subgraphB""#,
        )],
    );
}

#[test]
fn merge_validations_errors_if_external_field_not_defined_elsewhere() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          q: String
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        interface I {
          f: Int
        }

        type A implements I @key(fields: "k") {
          k: ID!
          f: Int @external
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "EXTERNAL_MISSING_ON_BASE",
            r#"Field "A.f" is marked @external on all the subgraphs in which it is listed (subgraph "subgraphB")."#,
        )],
    );
}

#[test]
fn merge_validations_errors_if_mandatory_argument_not_in_all_subgraphs() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          q(a: Int!): String @shareable
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type Query {
          q: String @shareable
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "REQUIRED_ARGUMENT_MISSING_IN_SOME_SUBGRAPH",
            r#"Argument "Query.q(a:)" is required in some subgraphs but does not appear in all subgraphs: it is required in subgraph "subgraphA" but does not appear in subgraph "subgraphB""#,
        )],
    );
}

#[test]
fn merge_validations_errors_if_subgraph_required_without_args_but_mandatory_in_supergraph() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          t: T
        }

        type T @key(fields: "id") {
          id: ID!
          x(arg: Int): Int @external
          y: Int @requires(fields: "x")
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type T @key(fields: "id") {
          id: ID!
          x(arg: Int!): Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "REQUIRES_INVALID_FIELDS",
            r#"[subgraphA] On field "T.y", for @requires(fields: "x"): no value provided for argument "arg" of field "T.x" but a value is mandatory as "arg" is required in subgraph "subgraphB""#,
        )],
    );
}

#[test]
fn merge_validations_errors_if_subgraph_required_with_arg_not_in_supergraph() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          t: T
        }

        type T @key(fields: "id") {
          id: ID!
          x(arg: Int): Int @external
          y: Int @requires(fields: "x(arg: 42)")
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type T @key(fields: "id") {
          id: ID!
          x: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "REQUIRES_INVALID_FIELDS",
            r#"[subgraphA] On field "T.y", for @requires(fields: "x(arg: 42)"): cannot provide a value for argument "arg" of field "T.x" as argument "arg" is not defined in subgraph "subgraphB""#,
        )],
    );
}

// =============================================================================
// POST-MERGE VALIDATIONS - Tests for validation after the merge phase
// =============================================================================

#[test]
fn post_merge_errors_if_type_does_not_implement_interface_post_merge() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          I: [I!]
        }

        interface I {
          a: Int
        }

        type A implements I {
          a: Int
          b: Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        interface I {
          b: Int
        }

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

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "INTERFACE_FIELD_NO_IMPLEM",
            r#"Interface field "I.a" is declared in subgraph "subgraphA" but type "B", which implements "I" only in subgraph "subgraphB" does not have field "a"."#,
        )],
    );
}

#[test]
fn post_merge_errors_if_type_does_not_implement_interface_on_interface() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          I: [I!]
        }

        interface I {
          a: Int
        }

        interface J implements I {
          a: Int
          b: Int
        }

        type A implements I & J {
          a: Int
          b: Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        interface J {
          b: Int
        }

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

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "INTERFACE_FIELD_NO_IMPLEM",
            r#"Interface field "J.a" is declared in subgraph "subgraphA" but type "B", which implements "J" only in subgraph "subgraphB" does not have field "a"."#,
        )],
    );
}

#[test]
fn requires_fields_validated_in_supergraph_schema() {
    // The @requires fields arguments should be validated against the supergraph schema, not the
    // subgraph schema. Thus, even if subgraph B's @requires fields argument value is invalid in the
    // subgraph schema, it's still valid in supergraph after merging.

    // In subgraph A, type B implements interface I.
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          t: T
        }

        interface I {
          x: String
        }

        type B implements I @key(fields: "x") {
          x: String @shareable
          y: String
        }

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

    // In subgraph B, type B does NOT implement interface I,
    // but @requires uses an inline fragment `... on B` within an I-typed field.
    // This should be valid because B implements I in the supergraph.
    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        interface I {
          x: String
        }

        type B @key(fields: "x") {
          x: String @shareable
          y: String @external
        }

        type T @key(fields: "id") {
          id: ID!
          i: I @external
          computed: String @requires(fields: "i { x ... on B { y } }")
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    result.expect(
        "Expected composition to succeed when type implements interface in another subgraph",
    );
}

// =============================================================================
// MISC VALIDATIONS - Standalone validation tests
// =============================================================================

#[test]
fn misc_not_broken_by_similar_field_argument_signatures() {
    // This test validates the case from https://github.com/apollographql/federation/issues/1100 is fixed.
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          t: T
        }

        type T @shareable {
          a(x: String): Int
          b(x: Int): Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type T @shareable {
          a(x: String): Int
          b(x: Int): Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    let _supergraph = result.expect("Expected composition to succeed");
}

// =============================================================================
// SATISFIABILITY VALIDATIONS - Tests for satisfiability validation
// =============================================================================

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

        type A @shareable {
          x: Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        type A @shareable {
          x: Int
          y: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    // This test specifically checks that the error code is SATISFIABILITY_ERROR
    // The exact error message is tested elsewhere
    let errors = result.expect_err("Expected composition to fail due to satisfiability");
    let error_codes: Vec<String> = errors
        .iter()
        .map(|e| e.code().definition().code().to_string())
        .collect();
    assert!(
        error_codes
            .iter()
            .any(|msg| msg.contains("SATISFIABILITY_ERROR")),
        "Expected SATISFIABILITY_ERROR but got: {:?}",
        error_codes
    );
}

#[test]
fn satisfiability_validation_handles_indirectly_reachable_keys() {
    // This test ensures that a regression introduced by https://github.com/apollographql/federation/pull/1653
    // is properly fixed. All we want to check is that validation succeeds on this example.
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          t: T
        }

        type T @key(fields: "k1") {
          k1: Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        # Note: the ordering of the key happens to matter for this to be a proper reproduction of the
        # issue #1653 created.
        type T @key(fields: "k2") @key(fields: "k1") {
          k1: Int
          k2: Int
        }
        "#,
    };

    let subgraph_c = ServiceDefinition {
        name: "subgraphC",
        type_defs: r#"
        type T @key(fields: "k2") {
          k2: Int
          v: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b, subgraph_c]);
    let _supergraph = result.expect("Expected composition to succeed - satisfiability should pass");
}