Skip to content

Accept unquoted strings and quoted enums in directive arguments - #348

Draft
kamilkisiela wants to merge 7 commits into
mainfrom
kamil/accept-unquoted-directive-args
Draft

kamilkisiela wants to merge 7 commits into
mainfrom
kamil/accept-unquoted-directive-args

Conversation

@kamilkisiela

Copy link
Copy Markdown
Contributor

Directive argument values written the "wrong" (we just want to match A's behavior, to make migrations easier) way are now read the way they were meant, instead of failing composition:

  • An unquoted value where the argument is a String or ID: @tag(name: Public) is read as @tag(name: "Public").
  • A quoted value where the argument is an enum, if the enum has that value: @example(level: "HIGH") is read as @example(level: HIGH).

The value is rewritten once, before anything else reads the subgraph, so the supergraph SDL and schema contract tag filters only ever see the usual form. @link(url:) and @link(as:) stay strict, since they are read earlier to work out which federation version the subgraph uses.

Checking every combination of argument type and value also turned up a few existing mistakes, fixed here too:

  • ID arguments now accept string and int literals.
  • null is accepted for nullable arguments and nullable list items.
  • A list value is rejected for a non-list argument, and every item of a list is checked at every level, not just the first.

A directive argument declared as `String` now accepts an unquoted,
enum-like value (`@tag(name: Public)`) and treats it as the equivalent
string. The value is rewritten before the subgraph state is built, so
validation and the supergraph SDL both see a regular string.

Applies to `String`, `String!` and list-of-String arguments; arguments
of other types are unaffected.
Extends the leniency added for `String` arguments to the other
direction: a directive argument declared as an enum now accepts a quoted
string naming one of the enum's values and treats it as that value,
which is what lands in the supergraph SDL.

A string that is not a value of the enum is still rejected, as are
values of any other kind.
The coercion ran after createSubgraphValidationContext had already
snapshotted the subgraph document, so rules reading directive arguments
from that snapshot still saw the original values. @context(name: Ctx)
passed the value check but was never registered as a context owner,
failing composition with a misleading "is never set" error.

Move it into the context factory, ahead of the maps that hold references
to the document's nodes, and widen it to the link spec definitions so
@link(for: "EXECUTION") is coerced the same way the validation rule
resolves it.

Aliases from @link(import: [{ name, as }]) are applied as lookup keys
rather than by renaming the shared spec nodes, which must keep happening
late. Directive nodes are also left untouched when nothing is coerced,
so the pass no longer rebuilds the whole document.
ID arguments rejected every literal, because the value/type comparison
only special-cased Int for Float; an explicit null was rejected for any
nullable argument, since the null guard only covered non-null types.
Both are valid GraphQL.

In the other direction, a list value was accepted for a non-list
argument and only the first item of a list was ever checked, so
["a", 1] passed for [String]. Lists are now matched against the
declared type and every item is checked, with null items allowed only
when the item type is nullable.

An unquoted, enum-like value is now also coerced for ID, which accepts
string literals.
The list check compared every item against the innermost named type, so
a nested list like [[], ["ok"]] for [[String!]!]! was rejected while
[["ok", 42]] passed, because only the first item of each list was ever
looked at. Replace the printed-string comparison with a recursive walk
of the type and the value together.

Schema contracts extract tags straight from the subgraph documents,
before composition normalizes them, so a tag written unquoted was
accepted by composition but ignored by the contract filter. Accept both
spellings where the tag name is read.

@link(url:) and @link(as:) are read before normalization, to work out
which spec a subgraph uses, so they are excluded from it and stay
strict; an unquoted url is reported rather than silently skipping
duplicate feature detection.
Walk only the nodes that can hold a directive (definitions, fields,
arguments and enum values) instead of every node of the document, skip
a directive's arguments once it has been handled, and resolve
@link(import:) aliases through a map built once per subgraph instead of
a regex per spec directive and import. The federation spec defines no
enums, so the loop collecting spec enum values is gone.

Collapse the new argument value tests into two tables, one for what has
to compose and one for what has to be rejected, keeping every case.
Say what is accepted instead of calling it lenient, in the comment and
the changeset, and move the comment back above the function it
describes.
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@theguild/federation-composition 0.27.2-alpha-20261119121615-d33b3b3ff5644d3661f45cb8804fdb7e311848d3 npm ↗︎ unpkg ↗︎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant