Skip to content

Commit 78868dd

Browse files
authored
Use keyDirective() for entity detection (#3188)
For entity detection, make sure we're using the keyDirective as it appears in the subgraph schema fixes #3187
1 parent 0d0e697 commit 78868dd

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

composition-js/src/__tests__/hints.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,51 @@ test('hints on field of object value type not being in all subgraphs', () => {
302302
+ '"T.b" is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
303303
'T'
304304
);
305-
})
305+
});
306+
307+
test('use of federation__key does not raise hint', () => {
308+
const subgraph1 = gql`
309+
extend schema
310+
@link(url: "https://specs.apollo.dev/federation/v2.7")
311+
312+
type Query {
313+
a: Int
314+
}
315+
316+
union U = T
317+
318+
type T @federation__key(fields:"id") {
319+
id: ID!
320+
b: Int
321+
}
322+
`;
323+
324+
const subgraph2 = gql`
325+
extend schema
326+
@link(url: "https://specs.apollo.dev/federation/v2.7")
327+
328+
type Query {
329+
b: Int
330+
}
331+
332+
type T @federation__key(fields:"id") {
333+
id: ID!
334+
c: Int
335+
}
336+
`;
337+
const result = composeServices([
338+
{
339+
name: 'subgraph1',
340+
typeDefs: subgraph1,
341+
},
342+
{
343+
name: 'subgraph2',
344+
typeDefs: subgraph2,
345+
},
346+
]);
347+
assertCompositionSuccess(result);
348+
expect(result).toNotRaiseHints();
349+
});
306350

307351
test('hints on field of interface value type not being in all subgraphs', () => {
308352
const subgraph1 = gql`

composition-js/src/merging/merge.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,11 +1097,14 @@ class Merger {
10971097
private hintOnInconsistentEntity(sources: Sources<ObjectType>, dest: ObjectType): boolean {
10981098
const sourceAsEntity: ObjectType[] = [];
10991099
const sourceAsNonEntity: ObjectType[] = [];
1100-
for (const source of sources.values()) {
1100+
for (const [idx, source] of sources.entries()) {
11011101
if (!source) {
11021102
continue;
11031103
}
1104-
if (source.hasAppliedDirective('key')) {
1104+
1105+
const sourceMetadata = this.subgraphs.values()[idx].metadata();
1106+
const keyDirective = sourceMetadata.keyDirective();
1107+
if (source.hasAppliedDirective(keyDirective)) {
11051108
sourceAsEntity.push(source);
11061109
} else {
11071110
sourceAsNonEntity.push(source);

0 commit comments

Comments
 (0)