Skip to content

Commit 9dbac33

Browse files
committed
RFC: Fragment Arguments
1 parent 45ffddb commit 9dbac33

4 files changed

+307
-52
lines changed

spec/Appendix B -- Grammar Summary.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ Arguments[Const] : ( Argument[?Const]+ )
168168

169169
Argument[Const] : Name : Value[?Const]
170170

171-
FragmentSpread : ... FragmentName Directives?
171+
FragmentSpread : ... FragmentName Arguments? Directives?
172172

173173
InlineFragment : ... TypeCondition? Directives? SelectionSet
174174

175-
FragmentDefinition : fragment FragmentName TypeCondition Directives?
176-
SelectionSet
175+
FragmentDefinition : fragment FragmentName VariablesDefinition? TypeCondition
176+
Directives? SelectionSet
177177

178178
FragmentName : Name but not `on`
179179

spec/Section 2 -- Language.md

+65-8
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,10 @@ which returns the result:
516516

517517
## Fragments
518518

519-
FragmentSpread : ... FragmentName Directives?
519+
FragmentSpread : ... FragmentName Arguments? Directives?
520520

521-
FragmentDefinition : fragment FragmentName TypeCondition Directives?
522-
SelectionSet
521+
FragmentDefinition : fragment FragmentName VariablesDefinition? TypeCondition
522+
Directives? SelectionSet
523523

524524
FragmentName : Name but not `on`
525525

@@ -1209,13 +1209,70 @@ size `60`:
12091209

12101210
**Variable Use Within Fragments**
12111211

1212-
Variables can be used within fragments. Variables have global scope with a given
1213-
operation, so a variable used within a fragment must be declared in any
1214-
top-level operation that transitively consumes that fragment. If a variable is
1215-
referenced in a fragment and is included by an operation that does not define
1216-
that variable, that operation is invalid (see
1212+
Variables can be used within fragments. Operation-defined variables have global
1213+
scope with a given operation, so a variable used within a fragment must either
1214+
be declared in any top-level operation that transitively consumes that fragment,
1215+
or by that same fragment as a fragment variable definition. If a variable is
1216+
referenced in a fragment is included by an operation where neither the fragment
1217+
nor the operaiton defines that variable, that operation is invalid (see
12171218
[All Variable Uses Defined](#sec-All-Variable-Uses-Defined)).
12181219

1220+
## Fragment Variable Definitions
1221+
1222+
Fragments may define locally scoped variables. This allows fragments to be
1223+
reused while enabling the caller to specify the fragment's behavior.
1224+
1225+
For example, the profile picture may need to be a different size depending on
1226+
the parent context:
1227+
1228+
```graphql example
1229+
query withFragmentArguments {
1230+
user(id: 4) {
1231+
...dynamicProfilePic(size: 100)
1232+
friends(first: 10) {
1233+
id
1234+
name
1235+
...dynamicProfilePic
1236+
}
1237+
}
1238+
}
1239+
1240+
fragment dynamicProfilePic($size: Int! = 50) on User {
1241+
profilePic(size: $size)
1242+
}
1243+
```
1244+
1245+
In this case the `user` will have a larger `profilePic` than those found in the
1246+
list of `friends`.
1247+
1248+
A fragment-defined variable is scoped to the fragment that defines it.
1249+
Fragment-defined variables are allowed to shadow operation-defined variables.
1250+
1251+
```graphql example
1252+
query withShadowedVariables($size: Int) {
1253+
user(id: 4) {
1254+
...variableProfilePic
1255+
}
1256+
secondUser: user(id: 5) {
1257+
...dynamicProfilePic(size: 10)
1258+
}
1259+
}
1260+
1261+
fragment variableProfilePic on User {
1262+
...dynamicProfilePic(size: $size)
1263+
}
1264+
1265+
fragment dynamicProfilePic($size: Int!) on User {
1266+
profilePic(size: $size)
1267+
}
1268+
```
1269+
1270+
The profilePic for `user` will be determined by the variables set by the
1271+
operation, while `secondUser` will always have a profilePic of size 10. In this
1272+
case, the fragment `variableProfilePic` uses the operation-defined variable,
1273+
while `dynamicProfilePic` uses the value passed in via the fragment spread's
1274+
argument `size`.
1275+
12191276
## Type References
12201277

12211278
Type :

0 commit comments

Comments
 (0)