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
17 changes: 17 additions & 0 deletions .changeset/quick-hounds-skip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"@theguild/federation-composition": minor
---

Add a `skipSatisfiability` option to `composeSchemaContract`. A contract only adds `@inaccessible` to an already composed graph, so when the same services composed successfully without the contract, the satisfiability check cannot find anything new. Skipping it cuts roughly 45% of the time spent composing each contract.

```ts
const base = composeServices(services);

if (!base.errors) {
const contract = composeSchemaContract(services, filter, true, {
skipSatisfiability: true,
});
}
```

The `@inaccessible` rules that contracts can trigger (for example `REFERENCED_INACCESSIBLE`, `REQUIRED_INACCESSIBLE` and `ONLY_INACCESSIBLE_CHILDREN`) keep running.
20 changes: 20 additions & 0 deletions .changeset/wise-crabs-derive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"@theguild/federation-composition": minor
---

Add `composeServicesWithContracts` to compose a supergraph together with its schema contracts.

Instead of composing all the subgraphs again for every contract, each contract is derived from the composed supergraph: the tag filter is applied to it, and only the rules about `@inaccessible` run, since those are the only ones a contract can fail. The results are the same as `composeServices` followed by `composeSchemaContract` for each contract, at a fraction of the time and memory.
The document of a contract reuses the definitions it has in common with the supergraph's document, so each contract adds little memory, however many there are.

```ts
const { supergraph, contracts } = composeServicesWithContracts(services, [
{ filter: { include: new Set(["public"]), exclude: new Set() } },
{
filter: { include: new Set(), exclude: new Set(["internal"]) },
removeUnreachableTypes: false,
},
]);
```

When the subgraphs fail to compose, each contract is composed from the subgraphs, as before.
135 changes: 135 additions & 0 deletions __tests__/contracts/compose-schema-contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,3 +994,138 @@ describe("composeSchemaContract", () => {
`);
});
});

describe("composeSchemaContract: errors introduced by the contract", () => {
const link = /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@tag"]
)
`;
const excludeInternal = {
include: new Set<string>(),
exclude: new Set(["internal"]),
};

// These compose as a base graph but fail as a contract. They come from the
// @inaccessible rules, which must keep running when satisfiability is skipped.
const cases = [
{
title: "excluded type is still referenced by an included field",
code: "REFERENCED_INACCESSIBLE",
sdl: /* GraphQL */ `
type Query {
a: A @tag(name: "public")
}
type A @tag(name: "internal") {
id: ID!
}
`,
},
{
title: "required argument is excluded",
code: "REQUIRED_INACCESSIBLE",
sdl: /* GraphQL */ `
type Query {
a(id: ID! @tag(name: "internal")): String @tag(name: "public")
}
`,
},
{
title: "every Query field is excluded",
code: "ONLY_INACCESSIBLE_CHILDREN",
sdl: /* GraphQL */ `
type Query {
a: String @tag(name: "internal")
}
`,
},
];

for (const { title, code, sdl } of cases) {
for (const skipSatisfiability of [false, true]) {
test(`${title} (skipSatisfiability: ${skipSatisfiability})`, () => {
const result = composeSchemaContract(
[{ name: "a", typeDefs: parse(link + sdl) }],
excludeInternal,
true,
{ skipSatisfiability },
);

expect(result.errors).toContainEqual(
expect.objectContaining({
extensions: expect.objectContaining({ code }),
}),
);
});
}
}
});

describe("composeSchemaContract: skipSatisfiability", () => {
// An unsatisfiable graph: "feed" requires User.name, but cannot reach "users"
// because the key includes a field "feed" only has as @external.
const services = [
{
name: "users",
typeDefs: parse(/* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key"]
)

type Query {
users: [User]
}

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

type Query {
usersByIds(ids: [ID!]!): [User!]!
}

extend type User @key(fields: "id") {
id: ID @external
name: String! @external
comments: [String] @requires(fields: "name")
}
`),
},
];
const filter = { include: new Set<string>(), exclude: new Set<string>() };

test("runs satisfiability by default", () => {
const result = composeSchemaContract(services, filter);

expect(result.errors).toContainEqual(
expect.objectContaining({
extensions: expect.objectContaining({ code: "SATISFIABILITY_ERROR" }),
}),
);
});

test("skips satisfiability when asked", () => {
// Only safe when the base graph already passed composition.
// This graph does not, which is how the test can see the check was skipped.
const result = composeSchemaContract(services, filter, true, {
skipSatisfiability: true,
});

expect(result.errors).toBeUndefined();
});
});
Loading
Loading