Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/funny-cups-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theguild/federation-composition": patch
---

Do not add `usedOverridden: true` to `@override` usage in case the overriden field is`@external` but has a matching interface field.
105 changes: 86 additions & 19 deletions __tests__/composition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,73 @@ testImplementations((api) => {
`);
});

test("@override(from:) no usedOverridden in case the overriden field is @external but has matching interface fields", () => {
const result = composeServices([
{
name: "a",
typeDefs: parse(/* GraphQL */ `
extend schema
@link(url: "https://specs.apollo.dev/link/v1.0")
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@tag", "@external", "@shareable", "@key", "@override"]
)

type Query {
ping: String
}

interface Base {
id: String!
items: [String]
}

type Object implements Base @key(fields: "id") @shareable {
id: String!
items: [String] @override(from: "b")
}
`),
},
{
name: "b",
typeDefs: parse(/* GraphQL */ `
extend schema
@link(url: "https://specs.apollo.dev/link/v1.0")
@link(
url: "https://specs.apollo.dev/federation/v2.0"
import: ["@tag", "@external", "@shareable", "@key"]
)

type Query {
pong: String
}

interface Base {
id: String!
items: [String]
}

type Object implements Base @key(fields: "id") @shareable {
id: String!
items: [String] @external
}
`),
},
]);

expect(result.errors).toEqual(undefined);
const line = result
.supergraphSdl!.split("\n")
.find((line) => line.includes("items: [String] @join__field"));

expect(line).toContain('@join__field(graph: A, override: "b")');
expect(line).toContain("@join__field(graph: B, external: true)");
// The usedOverridden should not appear
expect(line).not.toContain(
"@join__field(graph: B, usedOverridden: true, external: true)",
);
Comment on lines +313 to +315
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: usedOverridden and external is redundant here, as they both imply the same behaviour on gateway implementations.

});

test("@join__field(external: true) when field is overridden", () => {
let result = composeServices([
{
Expand Down Expand Up @@ -7382,25 +7449,25 @@ testImplementations((api) => {
typeDefs: parse(/* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable"]
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable"]
)
type Note @key(fields: "id") @shareable {
id: ID!
name: String
author: User
}
type User @key(fields: "id") {
id: ID!
name: String
id: ID!
name: String
}
type PrivateNote @key(fields: "id") @shareable {
id: ID!
note: Note
}
type Query {
note: Note @shareable
privateNote: PrivateNote @shareable
type PrivateNote @key(fields: "id") @shareable {
id: ID!
note: Note
}
type Query {
note: Note @shareable
privateNote: PrivateNote @shareable
}
`),
},
Expand All @@ -7425,9 +7492,9 @@ testImplementations((api) => {
result = api.composeServices([
{
name: "foo",
typeDefs: parse(/* GraphQL */ `
typeDefs: parse(/* GraphQL */ `
extend schema
@link(
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external", "@provides", "@shareable"]
)
Expand All @@ -7442,8 +7509,8 @@ testImplementations((api) => {
type PrivateNote @key(fields: "id") @shareable {
id: ID!
note: Note @provides(fields: "name author { id }")
}
type Query {
}
type Query {
note: Note @shareable
privateNote: PrivateNote @shareable
}
Expand All @@ -7461,8 +7528,8 @@ testImplementations((api) => {
id: ID!
name: String
author: User
}
type User @key(fields: "id") {
}
type User @key(fields: "id") {
id: ID!
name: String
}
Expand Down Expand Up @@ -7490,7 +7557,7 @@ testImplementations((api) => {
type Note @key(fields: "id") @shareable {
id: ID!
tag: String
}
}
`),
},
]);
Expand All @@ -7510,7 +7577,7 @@ testImplementations((api) => {
@join__field(external: true, graph: FOO)
@join__field(graph: BAR)
tag: String @join__field(graph: BAZ)
}
}
`);
});

Expand Down
14 changes: 14 additions & 0 deletions src/supergraph/composition/object-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,16 @@ export function objectTypeBuilder(): TypeBuilder<ObjectType, ObjectTypeState> {
);
const isRequired = f.required === true;

const inGraphs =
fieldNamesOfImplementedInterfaces[field.name];
const hasMatchingInterfaceFieldInGraph: boolean =
inGraphs && inGraphs.has(graphId);

// Apparently we need to keep it if it has a interface definition...
if (hasMatchingInterfaceFieldInGraph) {
return true;
}

return (
(isExternal && isRequired) ||
needsToPrintOverrideLabel ||
Expand Down Expand Up @@ -1014,6 +1024,10 @@ function provideUsedOverriddenValue(
const isOverridden =
field.override && graphNameToId(field.override) === graphId;

if (isOverridden && fieldStateInGraph.external) {
return false;
}

if (
isOverridden &&
(isUsedAsNonExternalKey || hasMatchingInterfaceFieldInGraph)
Expand Down