@@ -516,10 +516,10 @@ which returns the result:
516
516
517
517
## Fragments
518
518
519
- FragmentSpread : ... FragmentName Directives?
519
+ FragmentSpread : ... FragmentName Arguments? Directives?
520
520
521
- FragmentDefinition : fragment FragmentName TypeCondition Directives?
522
- SelectionSet
521
+ FragmentDefinition : fragment FragmentName VariablesDefinition? TypeCondition
522
+ Directives? SelectionSet
523
523
524
524
FragmentName : Name but not ` on `
525
525
@@ -1209,13 +1209,70 @@ size `60`:
1209
1209
1210
1210
** Variable Use Within Fragments**
1211
1211
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
1217
1218
[ All Variable Uses Defined] ( #sec-All-Variable-Uses-Defined ) ).
1218
1219
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
+
1219
1276
## Type References
1220
1277
1221
1278
Type :
0 commit comments