Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Fragment arguments/variables (syntax/validation/execution) - #1081

Open
JoviDeCroock wants to merge 63 commits into
graphql:mainfrom
JoviDeCroock:fragment-args-2024-amendments
Open

JoviDeCroock wants to merge 63 commits into
graphql:mainfrom
JoviDeCroock:fragment-args-2024-amendments

Conversation

@JoviDeCroock

@JoviDeCroock JoviDeCroock commented Feb 16, 2024 •

Copy link
Copy Markdown
Contributor

This spec contains amendments to #1010, a diffed view is available at mjmahone#3.

These amendments are made from comments on the implementation PR and alterations from the new implementation

In general the biggest changes are that we introduce fragmentVariableValues which will be on the groupedFieldSet, these are derived from the arguments in scope of the fragmentDefinition where this field is used.

We introduce localFragmentVariables which as we are traversing down fragment-spreads are a coerced set of variables i.e.

query($b: String!, $c: String!) {
  field3(b: $b) ## uses b from variable-values
  ...A(a: "A", b: "B")
}

fragment A($a: String!, $b: String!) on X {
  field(a: $a) ## uses $a from localVariableValues
  field2(c: $c) ## we have access to variableValues so we take c from there
  ...B(b: $b) ## uses $b from localVariableValues
}

fragment B($b: String!) on X {
  ## This fragment has access to $b and $c but not to $a
  field4(b: $b) ## uses $b from localVariableValues
}

Last but not least we introduce getArgumentValuesFromSpread which looks at the spread and fragment-definition and establishes a coerced set of localVariableValues.


@netlify

netlify Bot commented Feb 16, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for graphql-spec-draft ready!

Name Link
🔨 Latest commit a2ae060
🔍 Latest deploy log https://app.netlify.com/projects/graphql-spec-draft/deploys/6b0aa4b4041ca5a5a54adca0
😎 Deploy Preview https://deploy-preview-1081--graphql-spec-draft.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@mjmahone mjmahone left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clear to me :)

Very excited to see progress, and thank you for cleaning up and clarifying the PR!

Comment thread spec/Section 2 -- Language.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 2 -- Language.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
@JoviDeCroock
JoviDeCroock force-pushed the fragment-args-2024-amendments branch from 5a73543 to d7590fa Compare March 8, 2024 07:04
@JoviDeCroock

Copy link
Copy Markdown
Contributor Author

@graphql/tsc is there anything I can do to move this forward?

Comment thread spec/Section 6 -- Execution.md Resolved

@benjie benjie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like section 2 should comment on the fact that shadowed variables may not be referenced implicitly by child fragment spreads. (This is probably not the right way of saying that!) E.g. in the following query, Forbidden is spread inside of F and attempts to use variable $a which is ambiguous, and hence not permitted.

query Q($a: Int!, $b: Int!) {
  ...F(a: 7)
}
fragment F($a: Int!) {
  ...Fine
  ...Forbidden
}
fragment Fine {
  b: echo(input: $b)
}
fragment Forbidden {
  a: echo(input: $a)
}

Comment thread spec/Section 2 -- Language.md Resolved
Comment thread spec/Section 2 -- Language.md Resolved
Comment thread spec/Section 2 -- Language.md Resolved
Comment thread spec/Section 2 -- Language.md Resolved
Comment thread spec/Section 2 -- Language.md Resolved
Comment thread spec/Section 2 -- Language.md Resolved
Comment thread spec/Section 2 -- Language.md Resolved
@JoviDeCroock

JoviDeCroock commented Mar 27, 2024 •

Copy link
Copy Markdown
Contributor Author

@benjie from a purely technical perspective I agree, I was approaching that more from a backwards compatibility perspective. The Fragments variables are only applicable within the context of a FragmentDefinitiion so we don't carry nested values. A fragment is always looked at in isolation and not from a call-stack point of view.

i.e.

query ($a: String!) {
  operation {
    ...fields(a: 'some-value')
  }
}

fragment fields($a: String) on Type {
  field(x: $a) ## this carries 'some-value'
  ...moreFields
}

fragment moreFields on Type {
  field2(x: $a) ## this would carry the value passed into the operation
}

This also makes Fragment-Arguments easier to reason about for me atleast as it's either the Variables passed into the definition or from the operation itself.

@JoviDeCroock
JoviDeCroock force-pushed the fragment-args-2024-amendments branch from ac9fdbc to 03ba255 Compare March 27, 2024 13:39

@benjie benjie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't got as far as section 6 yet.

Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
@benjie

benjie commented Mar 27, 2024

Copy link
Copy Markdown
Member

@JoviDeCroock Ah! I thought we landed on the other side with this one, this does simplify things significantly because we can look at fragments in isolation 👍

@benjie benjie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work Jovi! Here's a few comments, but this was only a quick review so it's not exhaustive.

Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
Comment thread spec/Section 5 -- Validation.md Resolved
@linux-foundation-easycla

linux-foundation-easycla Bot commented May 17, 2024 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@benjie

benjie commented May 20, 2024

Copy link
Copy Markdown
Member

@JoviDeCroock did you rebase on a different computer? Often this happens when git is configured to use a different email address on one computer than another, so some commits don't match EasyCLA whilst others do.

@JoviDeCroock

JoviDeCroock commented May 20, 2024 •

Copy link
Copy Markdown
Contributor Author

@benjie I did not, the commits it points at are code-suggestions I applied so maybe it's not good at the co-authored ones? Not sure 😅 can rebase to fix it

@JoviDeCroock
JoviDeCroock force-pushed the fragment-args-2024-amendments branch from dc25c04 to f306736 Compare May 20, 2024 08:53
yaacovCR added a commit to yaacovCR/graphql-js that referenced this pull request Jul 20, 2026
The initial v17 release included validation of fragment arguments using an earlier, rejected version of the field selection merging rules. This PR updates fragment arguments validation to conform to the current spec proposal.

The proposal intentionally requires variables in otherwise identical arguments to refer to the same variable definition. Following argument values through fragment spreads could allow more documents, but it makes the logic for determining whether arguments are the same significantly more complex. The current fragment arguments proposal proposal takes the conservative approach of rejecting these cases.

References:
- Current fragment arguments proposal: graphql/graphql-spec#1224
- Earlier (now-closed) fragment arguments proposal: graphql/graphql-spec#1081
- Benjie's rationale for the conservative validation rule: graphql/graphql-spec#1081 (comment)
- Benjie's follow-up PR updating the proposal: JoviDeCroock/graphql-spec#2
- Proposal commit requiring references to the same variable definition: graphql/graphql-spec@6659416
- Initial GraphQL.js fragment arguments implementation: graphql#4015
yaacovCR added a commit to yaacovCR/graphql-js that referenced this pull request Jul 20, 2026
The initial v17 release included validation of fragment arguments using an earlier, rejected version of the field selection merging rules. This PR updates fragment arguments validation to the current spec proposal.

The current spec proposal intentionally requires variables in otherwise identical arguments to refer to the same variable definition. Following argument values through fragment spreads could allow more documents, but it makes the logic for determining whether arguments are the same significantly more complex. The current fragment arguments proposal takes the conservative approach of rejecting these cases.

For example, these fields appear to use the same operation variable:

```graphql
query Example($size: Int!) {
  user {
    image(size: $size)
    ...Image(size: $size)
  }
}

fragment Image($size: Int!) on User {
  image(size: $size)
}
```

The first field uses the operation variable, while the field in `Image` uses the fragment variable. Treating them as equal requires resolving the spread argument before comparing the fields.

A literal may also match a fragment variable default:

```graphql
query Example {
  user {
    image(size: 10)
    ...Image
  }
}

fragment Image($size: Int = 10) on User {
  image(size: $size)
}
```

Treating these fields as equal requires applying the omitted fragment argument default during field selection merging.

Bindings may also pass through several fragments:

```graphql
query Example($size: Int!) {
  user {
    image(size: $size)
    ...Outer(size: $size)
  }
}

fragment Outer($size: Int!) on User {
  ...Image(size: $size)
}

fragment Image($size: Int!) on User {
  image(size: $size)
}
```

Treating these fields as equal requires following the operation variable through both fragment variable definitions.

Allowing these cases means a fragment cannot be checked on its own. The validator must carry variable bindings into each spread, apply defaults, follow bindings through nested fragments, cache the same fragment separately for different bindings, and handle cycles while doing so. Whether two arguments are the same then depends on the full path to the fragment.

Requiring the same variable definition keeps the check local. Variables are compared by their definition rather than by tracing the values passed to them. The stricter rule can also be relaxed later without invalidating documents that are already valid, but v17 incorrectly released the more lenient rule despite the currently stricter stance within the specification.

References:
- Current fragment arguments proposal: graphql/graphql-spec#1224
- Earlier (now-closed) fragment arguments proposal: graphql/graphql-spec#1081
- Benjie's rationale for the conservative validation rule: graphql/graphql-spec#1081 (comment)
- Benjie's follow-up PR updating the proposal: JoviDeCroock/graphql-spec#2
- Proposal commit requiring references to the same variable definition: graphql/graphql-spec@6659416
- Initial GraphQL.js fragment arguments implementation: graphql#4015
yaacovCR added a commit to graphql/graphql-js that referenced this pull request Jul 20, 2026
…#4839)

The initial v17 release included validation of fragment arguments using
an earlier, rejected version of the field selection merging rules. This
PR updates fragment arguments validation to the current spec proposal.

The current spec proposal intentionally requires variables in otherwise
identical arguments to refer to the same variable definition. Following
argument values through fragment spreads could allow more documents, but
it makes the logic for determining whether arguments are the same
significantly more complex. The current fragment arguments proposal
takes the conservative approach of rejecting these cases.

For example, these fields appear to use the same operation variable:

```graphql
query Example($size: Int!) {
  user {
    image(size: $size)
    ...Image(size: $size)
  }
}

fragment Image($size: Int!) on User {
  image(size: $size)
}
```

The first field uses the operation variable, while the field in `Image`
uses the fragment variable. Treating them as equal requires resolving
the spread argument before comparing the fields.

A literal may also match a fragment variable default:

```graphql
query Example {
  user {
    image(size: 10)
    ...Image
  }
}

fragment Image($size: Int = 10) on User {
  image(size: $size)
}
```

Treating these fields as equal requires applying the omitted fragment
argument default during field selection merging.

Bindings may also pass through several fragments:

```graphql
query Example($size: Int!) {
  user {
    image(size: $size)
    ...Outer(size: $size)
  }
}

fragment Outer($size: Int!) on User {
  ...Image(size: $size)
}

fragment Image($size: Int!) on User {
  image(size: $size)
}
```

Treating these fields as equal requires following the operation variable
through both fragment variable definitions.

Allowing these cases means a fragment cannot be checked on its own. The
validator must carry variable bindings into each spread, apply defaults,
follow bindings through nested fragments, cache the same fragment
separately for different bindings, and handle cycles while doing so.
Whether two arguments are the same then depends on the full path to the
fragment.

Requiring the same variable definition keeps the check local. Variables
are compared by their definition rather than by tracing the values
passed to them. The stricter rule can also be relaxed later without
invalidating documents that are already valid, but v17 incorrectly
released the more lenient rule despite the currently stricter stance
within the specification.

References:
- Current fragment arguments proposal:
graphql/graphql-spec#1224
- Earlier (now-closed) fragment arguments proposal:
graphql/graphql-spec#1081
- Benjie's rationale for the conservative validation rule:
graphql/graphql-spec#1081 (comment)
- Benjie's follow-up PR updating the proposal:
JoviDeCroock/graphql-spec#2
- Proposal commit requiring references to the same variable definition:
graphql/graphql-spec@6659416
- Initial GraphQL.js fragment arguments implementation:
#4015
@JoviDeCroock
JoviDeCroock restored the fragment-args-2024-amendments branch November 13, 2026 06:07
@JoviDeCroock JoviDeCroock reopened this Nov 13, 2026
@JoviDeCroock

Copy link
Copy Markdown
Contributor Author

Pushed some updates to make life easier on the next champion

@benjie benjie reopened this Nov 13, 2026
@benjie

benjie commented Nov 13, 2026

Copy link
Copy Markdown
Member

I’ve reopened this as the core PR and closed the clone. Hopefully we can find a champion to get this over the line 🤞

@jerelmiller

Copy link
Copy Markdown

FYI, the upcoming Apollo Client 4.4 release (still in alpha) will support fragment arguments (PR)

Replace the non-normative note with SameArguments() and
SameVariableScope(): arguments referencing a fragment variable only match
when both selections were visited via fragment spreads of the same
fragment with the same arguments. Previously two selections referencing
the same fragment variable definition could merge with different values.
Define field detail and fragment variable values as data types, pass the
fragment's variable definitions to CoerceArgumentValues() directly instead
of building signatures, and remove GetVariableSignature(). Update the
field set definition and the variable coercion note to account for
fragment variables.
The fragment variable was nullable but used in a non-null argument
position, so the example shown as valid failed validation.
Coercing the arguments of a fragment spread can raise an execution error
while collecting fields, before any field in the selection set has a
response position. Raise it at the response position being completed, or
set data to null when collecting the root selection set.
yaacovCR pushed a commit to graphql/graphql-js that referenced this pull request Dec 2, 2026
…#5023)

Report duplicate argument names on fragment spreads and duplicate
variable names on fragment definitions. Both `...F(x: 1, x: 2)` and
`fragment F($x: Int, $x: Int)` previously passed validation.

Found while comparing the implementation against the [fragment arguments
spec text](graphql/graphql-spec#1081), which
requires both under Argument Uniqueness and Variable Uniqueness.
yaacovCR pushed a commit to graphql/graphql-js that referenced this pull request Dec 3, 2026
…nts (#5024)

Stop reporting a conflict when two spreads of one fragment have
different arguments below mutually exclusive parent fields, such as `...
on Dog { owner { ...Name(surname: true) } }` and `... on Cat { owner {
...Name(surname: false) } }`. Those selection sets are never collected
for the same object. Spreads within one selection set must still have
the same arguments.

Found while comparing the implementation against the [fragment arguments
spec text](graphql/graphql-spec#1081), where
field selection merging only compares spreads in selection sets that can
merge.

This branch has not been deployed

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

Labels

📄 Draft (RFC 2) RFC Stage 2 (See CONTRIBUTING.md) 👻 Needs Champion RFC Needs a champion to progress (See CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parameterized fragments

9 participants