Skip to content

Polyfill @defer/@stream validation rules for graphql@16 - #3296

Closed
tsnobip wants to merge 4 commits into
graphile:mainfrom
tsnobip:defer-v16
Closed

tsnobip wants to merge 4 commits into
graphile:mainfrom
tsnobip:defer-v16

Conversation

@tsnobip

@tsnobip tsnobip commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

Follow-up to the discussion about supporting @defer/@stream without a specific graphql version.

When the installed graphql doesn't provide @defer/@stream (e.g. graphql@^16), Grafast already polyfills the directives themselves (incremental.ts, added to the schema by StreamDeferPlugin / makeGrafastSchema({ enableDeferStream: true })). What was missing is validation of how those directives are used, since GraphQL.js 16's specifiedRules know nothing about them.

This PR adds polyfills of the GraphQL.js v17 incremental delivery validation rules (written from the 17.x.x behaviour, not copied), in grafast/src/incrementalValidation.ts:

  • DeferStreamDirectiveLabelRule – labels must be static strings, unique across the document
  • DeferStreamDirectiveOnRootFieldRule – no @defer/@stream on root mutation/subscription selections
  • DeferStreamDirectiveOnValidOperationsRule – in subscriptions, only allowed when disabled (if: false or a variable)
  • StreamDirectiveOnListFieldRule – @stream only on list fields
  • StreamDirectiveOverlappingFieldsRule – the @stream part of v17's OverlappingFieldsCanBeMergedRule; Grafast's planner relies on this

Only the aggregate is exported, as incrementalValidationRules; it's applied by grafast() and Grafserv's default validationRules. The list is empty when graphql already provides @defer/@stream, so rules never run twice.

Notes for review

  • Overlap rule strictness: I deliberately followed the modern v17 rule (any overlap involving @stream is a conflict, even with identical arguments — see Same stream across fragments graphql/defer-stream-wg#100) rather than the pre-2023 SameStreamDirective. It rejects a superset of what the old rule did, so it's safe for the current implementation, and documents accepted today stay valid once Grafast moves to the modern incremental delivery format. Happy to switch to the looser rule if you'd prefer.
  • Directive definitions unchanged: the polyfilled if/initialCount args are still nullable (v17 uses Boolean! = true / Int! = 0); changing them would alter printed schemas, so I left that alone.
  • Out of scope, but spotted: when graphqlHasStreamDefer is true, StreamDeferPlugin/makeGrafastSchema assume the old experimental graphql@16 API (enableDeferStream, no directives added). On v17 I believe the directives won't reach the schema. Not tested on v17; likely part of the move to the modern format.
  • Envelop: grafast/envelop's useGrafast() only replaces execute/subscribe, so Envelop/Yoga users on graphql@16 still won't get these rules unless they add incrementalValidationRules themselves. This gap predates the PR; I left it out to keep scope tight, but it's a ~5 line onValidate hook if you'd like it here.
  • Grafserv peer range: Grafserv now imports incrementalValidationRules from grafast, but its peer range is still ^1.0.0-rc.8. A new Grafserv with an older Grafast would fail at startup. I didn't bump the range (AGENTS.md: avoid version changes unless essential) — let me know if you'd like it bumped.
  • StreamDeferPlugin is still not in graphile-build's default preset, so this PR doesn't change that it's opt-in.

Performance impact

Negligible: the overlap rule is skipped entirely if the schema has no @stream, memoises each merged group of selection sets (so fragment cycles and fan-out can't blow up), and only compares pairs involving a @stream field. The other four simple rules do cheap per-directive/per-operation checks; the overlap rule only does its work if the document contains a @stream directive.

Testing

  • New grafast/__tests__/incrementalValidation-test.ts (10 tests, including via grafast(), plus regression tests for fragment cycles, fragment fan-out and duplicate reports).
  • Fuzzed the overlap rule against a brute-force reference on thousands of random small documents (acyclic and cyclic): no mismatches. Adversarial documents under ~5KB stay within ~2.3× of specifiedRules alone.
  • Full grafast mocha suite passes (485) and test-bundle passes.
  • No Postgres locally, so I didn't run the dataplan-pg/postgraphile suites; instead I validated all 15 dataplan-pg *.test.graphql documents using @stream/@defer against the example schema with the new rules — no new errors. (Note those test helpers call plain validate(), so they don't exercise these rules.)

Security impact

Improves robustness: previously invalid @defer/@stream usage could reach the planner unvalidated on graphql@16.

Checklist

  • My code matches the project's code style and yarn lint:fix passes.
  • I've added tests for the new feature.
  • I've added a changeset.

🤖 Generated with Claude Code

tsnobip and others added 2 commits November 30, 2026 16:18
When the installed graphql doesn't provide @defer/@stream, Grafast already
polyfills the directives themselves, but their usage wasn't validated.

Add polyfills of the GraphQL.js v17 incremental delivery validation rules
(label uniqueness/staticness, no defer/stream on root mutation/subscription
fields, disabled-only in subscriptions, @stream on list fields only, and no
overlapping fields with @stream), exported as `incrementalValidationRules`.
These are applied by `grafast()` and by Grafserv's default validation rules;
the list is empty when graphql already ships these rules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4036870

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
grafast Patch
grafserv Patch
postgraphile Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@tsnobip

tsnobip commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

tsnobip and others added 2 commits December 1, 2026 11:48
The individual polyfill rules are an implementation detail; nothing needs
them and they'd become obsolete public API once graphql@17 is required.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
StreamDirectiveOverlappingFieldsRule could recurse forever on fragment
cycles (throwing RangeError from validate()) and took exponential time on
fragment fan-out, both triggerable by any document containing @stream.

- Memoise check() on the group of selection sets being merged
- Only compare pairs involving a @stream field; dedupe fields with a Set
- Report each conflicting pair once regardless of order
- Skip the rule entirely when the schema has no @stream directive

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@benjie benjie added the ✋ on hold Not planning to move this one forward (at least not right now) label Dec 2, 2026
@benjie

benjie commented Dec 2, 2026

Copy link
Copy Markdown
Member

We're hoping to punt this to just use the GraphQL v17 rules rather than copying them into Grafast

@tsnobip tsnobip closed this Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✋ on hold Not planning to move this one forward (at least not right now)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants