@@ -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
@@ -1215,13 +1215,70 @@ size `60`:
1215
1215
1216
1216
** Variable Use Within Fragments**
1217
1217
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
1223
1224
[ All Variable Uses Defined] ( #sec-All-Variable-Uses-Defined ) ).
1224
1225
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
+
1225
1282
## Type References
1226
1283
1227
1284
Type :
0 commit comments