Repository navigation
Accept unquoted strings and quoted enums in directive arguments - #348
Draft
kamilkisiela wants to merge 7 commits into
Draft
kamilkisiela wants to merge 7 commits into
kamilkisiela wants to merge 7 commits into
Conversation
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.
Contributor
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.
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:
StringorID:@tag(name: Public)is read as@tag(name: "Public").@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:
IDarguments now accept string and int literals.nullis accepted for nullable arguments and nullable list items.