Repository navigation
feat: compose contracts together with the source supergraph - #347
Merged
Merged
Conversation
Contributor
A contract only adds @inaccessible to an already composed graph. Satisfiability ignores inaccessible fields in the paths it requires while still using them to resolve those paths, so a contract of a satisfiable graph is satisfiable too. When the base graph composed successfully, callers can now skip the check, which is the most expensive part of composing each contract. Also adds tests for the errors only a contract can introduce (REFERENCED_INACCESSIBLE, REQUIRED_INACCESSIBLE, ONLY_INACCESSIBLE_CHILDREN), run with and without the skip.
Add composeServicesWithContracts, which composes the supergraph once and derives each contract from its state instead of composing all the subgraphs again. The tag filter is applied to the supergraph state with an undo log, only the rules about @inaccessible run, and the state is restored for the next contract. Errors and output are the same as composeServices followed by composeSchemaContract for each contract. The filter mirrors the decisions the subgraph filter makes, including the ones the merged state cannot tell on its own: tags matched by the type names used in each subgraph (renamed root types), type-level @external in Federation v1, @tag the filter leaves in place, federation types declared in subgraphs, and QUERY_ROOT_TYPE_INACCESSIBLE reported by subgraph validation. When the subgraphs fail to compose, each contract is composed from the subgraphs as before.
A contract's supergraph document is almost entirely the same as the supergraph it is derived from, yet every contract kept its own copy of each definition. A contract document now reuses the definitions of the supergraph document that print the same, so each additional contract adds little memory: base + 8 contracts on the huge-schema fixture needs 145 MB of heap instead of 279 MB. Add benchmarks of a medium and a huge schema with 3 contracts, comparing composeServices followed by composeSchemaContract per contract with composeServicesWithContracts.
Mirrors the include-filter argument behavior from #345 in the state-based contract filter.
kamilkisiela
force-pushed
the
kamil-contracts
branch
from
November 19, 2026 16:06
b9b922f to
1dae6bc
Compare
The subgraph transform and the supergraph state filter now make every include/exclude decision through the same functions, so a rule change (like argument tag inheritance) lands in both. The random-tags test now covers several tag densities and a second fixture to catch any remaining drift.
n1ru4l
marked this pull request as ready for review
December 1, 2026 11:56
n1ru4l
pushed a commit
that referenced
this pull request
Dec 1, 2026
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @theguild/federation-composition@0.28.0 ### Minor Changes - [#347](#347) [`4b1748e`](4b1748e) Thanks [@n1ru4l](https://github.com/n1ru4l)! - 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. - [#347](#347) [`4b1748e`](4b1748e) Thanks [@n1ru4l](https://github.com/n1ru4l)! - 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. Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
kamilkisiela
added a commit
to graphql-hive/federation-composition-rs
that referenced
this pull request
Dec 3, 2026
Ports upstream [v0.27.3](https://github.com/graphql-hive/federation-composition/releases/tag/v0.27.3) and [v0.28.0](https://github.com/graphql-hive/federation-composition/releases/tag/v0.28.0), one commit each, and moves `UPSTREAM` to `v0.28.0`. ## v0.27.3 - `DeprecatedInterfaceFieldImplementationRule` (graphql-hive/federation-composition#349): a deprecated field whose interface field is not deprecated fails composition with `INVALID_GRAPHQL`, since graphql-js 17 rejects such a schema. - The new `INVALID-GRAPHQL` test and the updated `huge-schema` fixture are ported. ## v0.28.0 From graphql-hive/federation-composition#347: - `composeSchemaContract` gains a `skipSatisfiability` option (`ComposeSchemaContractOptions`; `compose_schema_contract` takes an `options` argument). - `composeServicesWithContracts` (new `contracts/supergraph_state_contract.rs`) composes the supergraph once and derives each contract from its state: the tag filter is applied to the state, only the six `@inaccessible` rules run (`validate_inaccessible_usage`), the supergraph is built again, and the state is restored for the next contract. When the subgraphs fail to compose, each contract is composed in full. - The three filter predicates (`isExcludedByFilter`, `isExcludedByExcludeFilter`, `isArgumentExcludedByFilter`) are shared by both filters in `tag_extraction.rs`. - The wasm bindings and the npm shim export `composeServicesWithContracts` and `SchemaContract`. The shim shares a contract's unchanged definitions with the supergraph's document, as upstream does. Where the Rust differs in mechanism, not in result: - **External fields' `@tag`s.** Upstream reads them from the subgraph states' types, which the Rust drops after merging each subgraph. A stripped subgraph state now keeps its tagged fields' tags (`SubgraphState::field_tags`); untagged fields cost nothing. - **Restoring the state.** Upstream mutates the state through undo closures. The Rust decides every change from the state read-only, applies them, and logs each previous value by its position, so `restore` puts it back. - **"a contract reuses the definitions it does not change"** checks JS object identity. It passes on wasm through the shim; the Rust test compares the printed definitions, since a Rust success is its SDL. ## Verification - `pnpm conformance` at v0.28.0: 7,401 passed, 25 skipped, 8 todo on wasm (same as upstream's own TypeScript); 3,714/3,714 compositions identical. - `cargo test --workspace`: the suite has 3,823 passed, 17 ignored; graphql-js differentials, doctests and unit tests pass. The random-tag tests on `dgs` and `stars-stuff` compare every derived contract with the full per-contract composition. - `pnpm test:npm`: 16/16. - clippy (native and wasm32) and `cargo doc`, with `-D warnings`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every contract used to be a full composition. A contract only adds
@inaccessibleto a graph that already composed, so most of that work is redundant.Changes
composeServicesWithContracts(services, contracts)composes once, then for each contract:@inaccessible(REFERENCED_INACCESSIBLE,REQUIRED_INACCESSIBLE,ONLY_INACCESSIBLE_CHILDREN, …);Output and errors equal
composeServices+composeSchemaContractper contract. If the subgraphs don't compose, it falls back to composing each contract from the subgraphs.The filter mirrors the subgraph filter, including what the merged state can't see: per-subgraph type names (renamed roots), type-level
@external(Fed v1),@tagthe filter leaves in place, federation types declared in subgraphs, andQUERY_ROOT_TYPE_INACCESSIBLE.Shared filter rules: both filters (the subgraph transform behind
composeSchemaContractand the state filter) make every include/exclude decision through the same functions intag-extraction.ts:isExcludedByFilter,isExcludedByExcludeFilter, andisArgumentExcludedByFilter(untagged arguments inherit field tags forinclude, from #345). A rule change lands in both paths at once.Shared definitions: a contract document reuses each supergraph definition that prints the same, which is almost all of them (4,279 / 4,282 on the huge fixture).
composeSchemaContract(..., { skipSatisfiability }): satisfiability ignores inaccessible fields in required paths but still uses them to resolve, so a contract of a graph that composed is satisfiable. Opt-in: only safe after the base composition succeeded.Results
Huge fixture (191 subgraphs), compared with
main(composeServices+composeSchemaContractper contract), minimum heap / median time:mainAt 8 contracts: −57% heap, ~3× faster. Each contract costs ~25 MB / 0.9 s on
mainand ~0.2 s here. Timings were taken on a loaded machine (relative numbers hold, absolute ones are inflated).pnpm benchnow includes contract groups.Testing
compose-services-with-contracts.spec.tschecks results against the old path: one case per behaviour above, contract-only errors, no leaking between contracts, definition sharing, and argument tag inheritance.A drift guard puts random tags on the DGS and stars-stuff fixtures at three densities × three seeds and compares both paths under include, exclude and mixed filters (~6 s). It fails when either filter's argument rule or type rule is changed on its own.
Follow-ups
composeServicesWithContracts.main:@inaccessibleon an interface field argument is dropped from the supergraph. A contract that excludes such an argument leaks it into the API schema. Separate PR.@tagleft on directive definitions and scalar/union extensions, the v1 type-level@externalmismatch), then makecomposeSchemaContractuse the state path and delete the subgraph transform.Unknown typeinstead of returning an error.