Skip to content

Commit b850f4a

Browse files
mjmahoneJoviDeCroock
authored andcommitted
RFC: Fragment Arguments
1 parent 0ba7cdf commit b850f4a

4 files changed

+307
-52
lines changed

spec/Appendix B -- Grammar Summary.md

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

171171
Argument[Const] : Name : Value[?Const]
172172

173-
FragmentSpread : ... FragmentName Directives?
173+
FragmentSpread : ... FragmentName Arguments? Directives?
174174

175175
InlineFragment : ... TypeCondition? Directives? SelectionSet
176176

177-
FragmentDefinition : fragment FragmentName TypeCondition Directives?
178-
SelectionSet
177+
FragmentDefinition : fragment FragmentName VariablesDefinition? TypeCondition
178+
Directives? SelectionSet
179179

180180
FragmentName : Name but not `on`
181181

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

@@ -1215,13 +1215,70 @@ size `60`:
12151215

12161216
**Variable Use Within Fragments**
12171217

1218-
Variables can be used within fragments. Variables have global scope with a given
1219-
operation, so a variable used within a fragment must be declared in any
1220-
top-level operation that transitively consumes that fragment. If a variable is
1221-
referenced in a fragment and is included by an operation that does not define
1222-
that variable, that operation is invalid (see
1218+
Variables can be used within fragments. Operation-defined variables have global
1219+
scope with a given operation, so a variable used within a fragment must either
1220+
be declared in any top-level operation that transitively consumes that fragment,
1221+
or by that same fragment as a fragment variable definition. If a variable is
1222+
referenced in a fragment is included by an operation where neither the fragment
1223+
nor the operation defines that variable, that operation is invalid (see
12231224
[All Variable Uses Defined](#sec-All-Variable-Uses-Defined)).
12241225

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

12271284
Type :

0 commit comments

Comments
 (0)