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

Set external to true for external fields that are not in the key, but are used in keys for other graphs
80 changes: 80 additions & 0 deletions __tests__/composition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4182,6 +4182,86 @@ testImplementations((api) => {
`);
});

test("@external and not a @key in its graph, but is @key in other graph", () => {
const result = composeServices([
{
name: "a",
typeDefs: parse(/* GraphQL */ `
extend schema @link(url: "https://specs.apollo.dev/federation/${version}", import: ["@key", "@shareable"])

type Product @key(fields: "id") @shareable{
id: ID!
productId: String!
}

type Query {
products: [Product]
}
`),
},
{
name: "b",
typeDefs: parse(/* GraphQL */ `
extend schema @link(url: "https://specs.apollo.dev/federation/${version}", import: ["@key"])

type Product @key(fields: "id", resolvable: false) {
id: ID!
}
`),
},
{
name: "d",
typeDefs: parse(/* GraphQL */ `
extend schema @link(url: "https://specs.apollo.dev/federation/${version}", import: ["@shareable", "@external", "@key", "@requires"])

extend type Product @shareable @key(fields: "id productId") {
id: ID! @external
productId: String! @external
bField: Boolean
}
`),
},
{
name: "e",
typeDefs: parse(/* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable", "@external", "@requires"]
)

extend type Product @shareable @key(fields: "id") {
id: ID! @external
productId: String! @external
bField: Boolean @requires(fields: "productId")
}
`),
},
]);

assertCompositionSuccess(
result,
`Composition failures:\n${result.errors?.map((e) => `- ${e.message}`).join("\n")}`,
);

expect(result.supergraphSdl).toContainGraphQL(/* GraphQL */ `
type Product
@join__type(graph: A, key: "id")
@join__type(graph: B, key: "id", resolvable: false)
@join__type(graph: D, key: "id productId", extension: true)
@join__type(graph: E, key: "id", extension: true) {
id: ID!
productId: String!
@join__field(external: true, graph: E)
@join__field(graph: A)
@join__field(graph: D)
bField: Boolean
@join__field(graph: D)
@join__field(graph: E, requires: "productId")
}
`);
});

test("@external + @tag", () => {
const subgraphs = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/supergraph/composition/object-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ export function objectTypeBuilder(): TypeBuilder<ObjectType, ObjectTypeState> {

// mark field as external if it's annotated with @external, but it's not used as a key on the extension type
if (
fieldState.usedAsKey &&
fieldStateInGraph.usedAsKey &&
objectType.byGraph.get(graphId)!.extension === true
) {
return false;
Expand Down
Loading