Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use keyDirective() for entity detection #3188

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion composition-js/src/__tests__/hints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,51 @@ test('hints on field of object value type not being in all subgraphs', () => {
+ '"T.b" is defined in subgraph "Subgraph1" but not in subgraph "Subgraph2".',
'T'
);
})
});

test('use of federation__key does not raise hint', () => {
const subgraph1 = gql`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.7")

type Query {
a: Int
}

union U = T

type T @federation__key(fields:"id") {
id: ID!
b: Int
}
`;

const subgraph2 = gql`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.7")

type Query {
b: Int
}

type T @federation__key(fields:"id") {
id: ID!
c: Int
}
`;
const result = composeServices([
{
name: 'subgraph1',
typeDefs: subgraph1,
},
{
name: 'subgraph2',
typeDefs: subgraph2,
},
]);
assertCompositionSuccess(result);
expect(result).toNotRaiseHints();
});

test('hints on field of interface value type not being in all subgraphs', () => {
const subgraph1 = gql`
Expand Down
7 changes: 5 additions & 2 deletions composition-js/src/merging/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1097,11 +1097,14 @@ class Merger {
private hintOnInconsistentEntity(sources: Sources<ObjectType>, dest: ObjectType): boolean {
const sourceAsEntity: ObjectType[] = [];
const sourceAsNonEntity: ObjectType[] = [];
for (const source of sources.values()) {
for (const [idx, source] of sources.entries()) {
if (!source) {
continue;
}
if (source.hasAppliedDirective('key')) {

const sourceMetadata = this.subgraphs.values()[idx].metadata();
const keyDirective = sourceMetadata.keyDirective();
if (source.hasAppliedDirective(keyDirective)) {
sourceAsEntity.push(source);
} else {
sourceAsNonEntity.push(source);
Expand Down