RFC X / Superseded

Fragment Arguments (parameterized fragments) for GraphQL

Opened on
Closed on

At a glance

Project
GraphQL Spec
Identifier
#865
Stage
RFC X / Superseded
Champion
@mjmahone
Updated
2025-06-12
Latest activity
Advanced to RFC 1 on
Source

Pull request description

This is a proposal to bring Relay-style Fragment Arguments into the Spec as an optional, client-only language feature.

Overview

We would allow clients to write GraphQL like:

query Q1 {
  ...Number(x: 100)
}

query Q2($i: Int = 3) {
  ...Number(x: $i)
}

fragment Number($x: Int!) on Query {
  number(value: $x)
}

which would, prior to sending to the server, be transformed into two unique queries that current spec-compliant servers would understand, such that it behaved like the queries were written like:

query Q1 {
  ...Number
}

fragment Number on Query {
  number(value: 100)
}

and

query Q2($i: Int = 3) {
  ...Number
}

fragment Number on Query {
  number(value: $i)
}

The exact mechanics of how the query gets rewritten could be different per client, but the behavior should be identical. This RFC seeks to describe the new syntax, as well as adding additional validation for clients that choose to support fragment arguments.

Background

Relay has seen a lot of usage of their non-spec-compliant @arguments and @argumentDefinitions directives, but they're both cumbersome to use and fail basic Spec validation (directives with arguments used but never defined). They accomplish this by pre-compiling any GraphQL definitions using @arguments and @argumentDefinitions to transform the document such that, by the time it's sent to the server, it is spec compliant. However, Relay's user-facing directives both do not conform to the spec and also provide a pretty cumbersome, somewhat unintuitive user-facing design.

This proposal is to allow clients that pre-compile their GraphQL prior to sending it to the server (such as Relay) to have a built-in syntax for passing argument values into fragment spreads for fragments with well-defined argument definitions.

Champion: @mjmahone

Timeline

June 2025

January 2023

June 2021

  • Advanced to RFC 1 on by leebyron
  • Discussed in GraphQL WG

    Fragment Arguments RFC (10m, Matt)

    Meeting notes · GraphQL WG · 2021-06-03

    Fragment Arguments RFC (10m, Matt)

    • Pending Implementation PR
    • Matt: I took the feedback from the last working group that maybe we should treat fragment arguments to be as part of the implementation. So I've added a step in GraphQL.js so that when we hit the fragment spread that defines arguments we go through and match the relevant variables. It might not be performant, but I've opened it up on GraphQL.js but it's turned off by default.
    • Matt: the harder part will be defining the validation rules to provide good errors because all variables used to be defined at the operation root or arguments at the field level; however now we have arguments whose type is defined in the executable document itself. Weird thing to resolve with the current validation setup.
    • Matt: RFC blocker is getting the validation rules up for the GraphQL.js implementation. After that, not certain what the next best steps will be.
    • Lee: I have a hunch that validation will be the bulk of the work and will inform the executor - may let you do something more performant for example.
    • Matt: one validation rule that might simplify the executor is only allowing constant arguments to be supplied to the fragments.
    • Matt: I'm trying to prevent implementations having to create an argument/variable stack. By making it so that you can only use variables defined as fragment arguments in the same fragment then the collectfields scope is extremely limited and means we don't have to pass the stack down.
    • Ivan: can we require that if you specify variables on a fragment then all downstream fragments can only reference fragments passed down explicitly?
    • Matt: that was one of the options in the original issue (posted 2016); now that we have the giant corpus of actual GraphQL that would mean only new queries could use it - we couldn't really add a fragment with arguments that calls a fragment without arguments. This spec RFC is preserving Relay's @arguments / @argumentDefinitions directives. We allow the fragment variables and operation variables to be intermixed at will.
    • Ivan: so if you explicitly define a fragment with variables you can still use the operation variables too?
    • Matt: yes. That's definitely a discussion point though; we should discuss the pros/cons on that RFC.
    • Lee: great progress - nice work!
    • Lee: Next steps: continue work on validation/execution. The PR that you have now is added but disabled; but I'd love to see it go through the whole process. I reviewed it before the meeting and was surprised it was disabled.
    • Matt: is anyone else interested in pushing this along? There's 4-5 validation rules that need writing and I'd love to have some help on this. It's probably Ivan's purview, but I don't know if it's in a state where we should add it in a branch on GraphQL.js to allow people to send PRs directly or if they should send to my fork.
    • Lee: I suggest that the PR is open to GraphQL.js and allow others to contribute to your PR. Start building a test suite and use that to gut-check the validation rules.
    • Ivan: I requested the rule to forbid during validation because people will start using this feature; if we change something later it's important the user explicitly agreed to enable this experimental feature, rather than implicitly. Maybe instead of a branch we can merge it as is but make it clear that it's experimental. I'm against flags; it complicates code and makes everything harder.
    • Matt: makes sense to me.
    • Lee: I anticipate the default value coercion stuff colliding in potentially interesting ways.
    Meeting notes · GraphQL WG · 2021-06-03

May 2021