apollo-federation 2.15.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
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
use std::collections::HashSet;

use apollo_federation::subgraph::typestate::Subgraph;
use insta::assert_snapshot;
use test_log::test;

use super::ServiceDefinition;
use super::assert_composition_errors;
use super::compose;
use super::compose_as_fed2_subgraphs;
use super::print_sdl;

// =============================================================================
// @interfaceObject DIRECTIVE TESTS - Tests for @interfaceObject functionality
// =============================================================================

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

        interface I @key(fields: "id") {
          id: ID!
          x: Int
        }

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

        type B implements I @key(fields: "id") {
          id: ID!
          x: Int
          z: 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]);
    let supergraph = result.expect("Expected composition to succeed");
    let api_schema = supergraph
        .to_api_schema(Default::default())
        .expect("Expected API schema generation to succeed");
    assert_snapshot!(print_sdl(api_schema.schema()));
}

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

        type I @interfaceObject @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,
        &[(
            "INTERFACE_OBJECT_USAGE_ERROR",
            r#"Type "I" is declared with @interfaceObject in all the subgraphs in which it is defined (it is defined in subgraphs "subgraphA" and "subgraphB" but should be defined as an interface in at least one subgraph)"#,
        )],
    );
}

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

        interface I @key(fields: "id") {
          id: ID!
          x: Int
        }

        type A 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 Query {
          iFromC: I
        }

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

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

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

          interface I @key(fields: "id") {
            id: ID!
            x: Int
          }

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

          type B implements I @key(fields: "id") {
            id: ID!
            x: Int
            z: 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#"
          interface I {
            id: ID!
            x: Int
          }

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

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b, subgraph_c]);
    assert_composition_errors(
        &result,
        &[(
            "INTERFACE_KEY_MISSING_IMPLEMENTATION_TYPE",
            r#"[subgraphA] Interface type "I" has a resolvable key (@key(fields: "id")) in subgraph "subgraphA" but that subgraph is missing some of the supergraph implementation types of "I". Subgraph "subgraphA" should define type "C" (and have it implement "I")."#,
        )],
    );
}

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

        interface I @key(fields: "id") {
          id: ID!
          x: Int
        }

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

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

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

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

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

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "INTERFACE_OBJECT_USAGE_ERROR",
            r#"[subgraphB] Interface type "I" is defined as an @interfaceObject in subgraph "subgraphB" so that subgraph should not define any of the implementation types of "I", but it defines type "A""#,
        )],
    );
}

#[test]
fn interface_object_composes_references_to_interface_object() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          i: I @shareable
        }

        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 {
          i: I @shareable
        }

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

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

#[test]
fn interface_object_does_not_error_when_optimizing_unnecessary_loops() {
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type T @key(fields: "id") {
          id: ID!
          i: I
        }

        interface I @key(fields: "id") {
          id: ID!
          x: Int
        }

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

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

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

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

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

        type I @interfaceObject @key(fields: "id") {
          id: ID!
        }

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

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    let _supergraph = result.expect(
        "Expected composition to succeed - should not error when optimizing unnecessary loops",
    );
}

#[test]
fn interface_object_fed354_repro_failure() {
    let subgraph1 = ServiceDefinition {
        name: "Subgraph1",
        type_defs: r#"
        type Query {
          error_query: TicketField!
        }

        type User @interfaceObject @key(fields: "id") {
          id: ID!
        }

        interface TicketField {
          id: ID!
          createdBy: User
        }

        type TextTicketField implements TicketField @key(fields: "id") @shareable {
          id: ID!
          createdBy: User
        }
        "#,
    };

    let subgraph2 = ServiceDefinition {
        name: "Subgraph2",
        type_defs: r#"
        interface Ticket @key(fields: "id", resolvable: true) {
          id: ID!
        }

        interface User @key(fields: "id", resolvable: true) {
          id: ID!
          requestedTickets: [Ticket!]!
        }

        interface TicketField {
          createdBy: User
          id: ID!
        }

        type TextTicketField implements TicketField @shareable {
          createdBy: User
          id: ID!
        }

        type Customer implements User @key(fields: "id", resolvable: true) @shareable {
          id: ID!
          requestedTickets: [Ticket!]!
        }

        type Agent implements User @key(fields: "id", resolvable: true) @shareable {
          id: ID!
          requestedTickets: [Ticket!]!
        }

        type Question implements Ticket @key(fields: "id", resolvable: true) {
          fields: [TicketField!]!
          id: ID!
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph1, subgraph2]);
    let _supergraph =
        result.expect("Expected composition to succeed - this is a repro test for issue FED-354");
}

#[test]
fn interface_object_with_inaccessible_field() {
    // Regression test for interface object fields not getting @join__field directives.
    // When an interface has @interfaceObject types in some subgraphs, all fields need
    // @join__field directives to indicate which subgraphs provide them.
    //
    // Setup:
    // - subgraph_a: defines interface with id @inaccessible
    // - subgraph_b: defines interface WITHOUT id field
    // - subgraph_c: @interfaceObject with id in key
    //
    // The bug was that subgraph_a and subgraph_c weren't getting @join__field for id.

    let subgraph_a = r#"
        extend schema
            @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", "@inaccessible"])

        type Query {
            items: [Item]
        }

        interface Item {
            id: ID! @inaccessible
        }

        type Product implements Item @key(fields: "id") {
            id: ID!
            name: String
        }
    "#;

    let subgraph_b = r#"
        extend schema
            @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])

        interface Item {
            name: String
        }

        type Special implements Item @key(fields: "id") {
            id: ID!
            name: String
        }
    "#;

    let subgraph_c = r#"
        extend schema
            @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", "@interfaceObject"])

        type Item @key(fields: "id") @interfaceObject {
            id: ID!
            extra: String
        }
    "#;

    let parsed_a = Subgraph::parse("subgraph-a", "http://example.com", subgraph_a).unwrap();
    let parsed_b = Subgraph::parse("subgraph-b", "http://example.com", subgraph_b).unwrap();
    let parsed_c = Subgraph::parse("subgraph-c", "http://example.com", subgraph_c).unwrap();

    let supergraph = compose(vec![parsed_a, parsed_b, parsed_c]).unwrap();

    let item_interface = supergraph
        .schema()
        .schema()
        .types
        .get("Item")
        .unwrap()
        .as_interface()
        .unwrap();
    let id_field = item_interface.fields.get("id").unwrap();
    let id_directives: HashSet<_> = id_field.directives.iter().map(|d| d.to_string()).collect();

    assert!(
        id_directives.contains("@join__field(graph: SUBGRAPH_A)"),
        "id field should have @join__field for subgraph-a"
    );
    assert!(
        id_directives.contains("@join__field(graph: SUBGRAPH_C)"),
        "id field should have @join__field for subgraph-c"
    );
}

#[test]
fn interface_with_non_resolvable_key_does_not_require_all_implementations() {
    // subgraphA defines the interface with a resolvable key and all implementations
    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          iFromA: I
        }

        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
        }

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

    // subgraphB defines the interface with a non-resolvable key but does not
    // define the implementations
    let subgraph_b = ServiceDefinition {
        name: "subgraphB",
        type_defs: r#"
        interface I @key(fields: "id", resolvable: false) {
          id: ID!
          x: Int
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    // This should not error because a non-resolvable key doesn't require all implementations
    let _supergraph = result.expect(
        "Expected composition to succeed - non-resolvable interface key should not require all implementations"
    );
}

#[test]
fn interface_object_chains_are_not_supported() {
    let s1 = ServiceDefinition {
        name: "S1",
        type_defs: r#"
            type Query {
              i: I1
            }

            interface I1 @key(fields: "id") {
              id: ID!
              data: String!
            }

            interface I2 implements I1 @key(fields: "id") {
              id: ID!
              data: String!
              data2: String!
            }

            type T implements I1 & I2 @key(fields: "id") {
              id: ID!
              data: String!
              data2: String!
            }
        "#,
    };
    let s2 = ServiceDefinition {
        name: "S2",
        type_defs: r#"
            type I1 @interfaceObject @key(fields: "id") {
              id: ID!
              data3: Int
            }
        "#,
    };
    let result = compose_as_fed2_subgraphs(&[s1, s2]);
    assert_composition_errors(
        &result,
        &[(
            "INTERFACE_OBJECT_USAGE_ERROR",
            r#"Interfaces implementing @interfaceObject are not supported: @interfaceObject "I1" is implemented by an interface "I2"."#,
        )],
    );
}

// =============================================================================
// @interfaceObject JOIN FIELD EMISSION TESTS
//
// Verifies that @join__field directives are only emitted when necessary on
// interface fields involving @interfaceObject. Fields shared by ALL subgraphs
// that declare the type should omit @join__field (implicit semantics suffice).
// =============================================================================

#[test]
fn interface_object_shared_key_fields_omit_join_field() {
    // Mirrors the real-world pattern (e.g. ShoppableProduct) where an
    // interface spans two subgraphs — one as a real interface, the other as
    // an @interfaceObject — while additional unrelated subgraphs participate
    // in the composition. Key fields shared by both interface-declaring
    // subgraphs should NOT get @join__field; subgraph-specific fields should.
    //
    // The bystander subgraph is critical: without it, the post-merge
    // remove_redundant_join_fields cleanup would strip the extra directives
    // (since they'd cover all graphs globally), masking the bug.
    let subgraph_a = ServiceDefinition {
        name: "SubgraphA",
        type_defs: r#"
        type Query {
          items: [I]
        }

        interface I @key(fields: "id code") {
          id: ID!
          code: String!
          onlyInA: Int
        }

        type A implements I @key(fields: "id code") {
          id: ID!
          code: String!
          onlyInA: Int
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "SubgraphB",
        type_defs: r#"
        type I @interfaceObject @key(fields: "id code") {
          id: ID!
          code: String!
          onlyInB: Boolean
        }
        "#,
    };

    let bystander = ServiceDefinition {
        name: "Bystander",
        type_defs: r#"
        type Unrelated @key(fields: "id") {
          id: ID!
          name: String
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b, bystander]);
    let supergraph = result.expect("composition should succeed");
    let schema = supergraph.schema().schema();

    let iface = schema
        .types
        .get("I")
        .expect("type I should exist")
        .as_interface()
        .expect("I should be an interface");

    // Key fields present in BOTH subgraphs should NOT have @join__field
    for field_name in ["id", "code"] {
        let field = iface
            .fields
            .get(field_name)
            .expect("field should exist on interface I");
        let join_fields: Vec<_> = field
            .directives
            .iter()
            .filter(|d| d.name == "join__field")
            .collect();
        assert!(
            join_fields.is_empty(),
            "Interface field I.{field_name} is shared by all declaring subgraphs \
             and should NOT have @join__field, but has: {join_fields:?}"
        );
    }

    // Fields present in only ONE subgraph should have @join__field
    for field_name in ["onlyInA", "onlyInB"] {
        let field = iface
            .fields
            .get(field_name)
            .expect("field should exist on interface I");
        let has_join_field = field.directives.iter().any(|d| d.name == "join__field");
        assert!(
            has_join_field,
            "Interface field I.{field_name} exists in only one subgraph \
             and should have @join__field"
        );
    }
}

#[test]
fn interface_object_shared_key_fields_with_three_subgraphs() {
    // Interface declared in SubgraphA with all implementations,
    // @interfaceObject in SubgraphB and SubgraphC, plus a bystander.
    // Key fields shared by all three interface-declaring subgraphs should
    // omit @join__field; subgraph-specific fields should not.
    let subgraph_a = ServiceDefinition {
        name: "SubgraphA",
        type_defs: r#"
        type Query {
          items: [I]
        }

        interface I @key(fields: "id") {
          id: ID!
          aOnly: Int
        }

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

    let subgraph_b = ServiceDefinition {
        name: "SubgraphB",
        type_defs: r#"
        type I @interfaceObject @key(fields: "id") {
          id: ID!
          bOnly: Float
        }
        "#,
    };

    let subgraph_c = ServiceDefinition {
        name: "SubgraphC",
        type_defs: r#"
        type I @interfaceObject @key(fields: "id") {
          id: ID!
          cOnly: Boolean
        }
        "#,
    };

    let bystander = ServiceDefinition {
        name: "Bystander",
        type_defs: r#"
        type Unrelated @key(fields: "id") {
          id: ID!
          name: String
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b, subgraph_c, bystander]);
    let supergraph = result.expect("composition should succeed");
    let schema = supergraph.schema().schema();

    let iface = schema
        .types
        .get("I")
        .expect("type I should exist")
        .as_interface()
        .expect("I should be an interface");

    // `id` is in ALL three subgraphs → should NOT have @join__field
    let id_field = iface.fields.get("id").expect("id field should exist");
    let id_join_fields: Vec<_> = id_field
        .directives
        .iter()
        .filter(|d| d.name == "join__field")
        .collect();
    assert!(
        id_join_fields.is_empty(),
        "Interface field I.id is shared by all declaring subgraphs \
         and should NOT have @join__field, but has: {id_join_fields:?}"
    );

    // Fields in only one subgraph should have exactly 1 @join__field
    for field_name in ["aOnly", "bOnly", "cOnly"] {
        let field = iface
            .fields
            .get(field_name)
            .expect("field should exist on interface I");
        let join_count = field
            .directives
            .iter()
            .filter(|d| d.name == "join__field")
            .count();
        assert_eq!(
            join_count, 1,
            "Interface field I.{field_name} exists in only one subgraph \
             and should have exactly 1 @join__field, but has {join_count}"
        );
    }
}

#[test]
fn interface_object_partial_overlap_needs_join_field() {
    // When a non-key field is present in some-but-not-all subgraphs that
    // declare the interface, @join__field is required even with @interfaceObject.
    // SubgraphA: interface with id + partial, SubgraphB: @interfaceObject with
    // id + partial, SubgraphC: @interfaceObject with id only, plus a bystander.
    let subgraph_a = ServiceDefinition {
        name: "SubgraphA",
        type_defs: r#"
        type Query {
          items: [I]
        }

        interface I @key(fields: "id") {
          id: ID!
          partial: String
        }

        type X implements I @key(fields: "id") {
          id: ID!
          partial: String @shareable
        }
        "#,
    };

    let subgraph_b = ServiceDefinition {
        name: "SubgraphB",
        type_defs: r#"
        type I @interfaceObject @key(fields: "id") {
          id: ID!
          partial: String @shareable
        }
        "#,
    };

    let subgraph_c = ServiceDefinition {
        name: "SubgraphC",
        type_defs: r#"
        type I @interfaceObject @key(fields: "id") {
          id: ID!
        }
        "#,
    };

    let bystander = ServiceDefinition {
        name: "Bystander",
        type_defs: r#"
        type Unrelated @key(fields: "id") {
          id: ID!
          name: String
        }
        "#,
    };

    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b, subgraph_c, bystander]);
    let supergraph = result.expect("composition should succeed");
    let schema = supergraph.schema().schema();

    let iface = schema
        .types
        .get("I")
        .expect("type I should exist")
        .as_interface()
        .expect("I should be an interface");

    // `id` is in all three → no @join__field
    let id_field = iface.fields.get("id").expect("id field should exist");
    let id_join_count = id_field
        .directives
        .iter()
        .filter(|d| d.name == "join__field")
        .count();
    assert_eq!(
        id_join_count, 0,
        "I.id is shared by all subgraphs and should NOT have @join__field"
    );

    // `partial` is in A and B but NOT C → needs @join__field
    let partial_field = iface
        .fields
        .get("partial")
        .expect("partial field should exist");
    let partial_join_count = partial_field
        .directives
        .iter()
        .filter(|d| d.name == "join__field")
        .count();
    assert_eq!(
        partial_join_count, 2,
        "I.partial is in 2 of 3 subgraphs and should have exactly 2 @join__field directives, \
         but has {partial_join_count}"
    );
}

#[test]
fn interface_object_type_kind_mismatch_labels_correctly_when_object_processed_first() {
    // Regression test for a bug where `mismatched_types` and `subgraphs_with_interface_obj`
    // used TypeDefinitionPosition (kind-carrying enum) as keys. When a plain object subgraph
    // was processed before an @interfaceObject subgraph, the merged schema stored Object("I")
    // but the @interfaceObject map was keyed by Interface("I"), causing lookup failures.
    // This produced: (1) wrong labels in TYPE_KIND_MISMATCH (both subgraphs shown as
    // "Object Type"), and (2) a spurious INTERFACE_OBJECT_USAGE_ERROR because the
    // mismatch-suppression check also failed.

    let subgraph_a = ServiceDefinition {
        name: "subgraphA",
        type_defs: r#"
        type Query {
          iFromA: I
        }

        type 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
        }
        "#,
    };

    // Compose with the plain object subgraph processed first (alphabetical ordering).
    // Before the fix, this produced:
    // 1. TYPE_KIND_MISMATCH with wrong label (both shown as "Object Type")
    // 2. Spurious INTERFACE_OBJECT_USAGE_ERROR
    // After the fix, only TYPE_KIND_MISMATCH with correct labeling is produced.
    let result = compose_as_fed2_subgraphs(&[subgraph_a, subgraph_b]);
    assert_composition_errors(
        &result,
        &[(
            "TYPE_KIND_MISMATCH",
            r#"Type "I" has mismatched kind: it is defined as Object Type in subgraph "subgraphA" but Interface Object Type (Object Type with @interfaceObject) in subgraph "subgraphB""#,
        )],
    );
}