Skip to content

Commit ef8aaa3

Browse files
authored
Merge pull request #2 from benjie/fragments-editorial
Fragment arguments editorial
2 parents 4805a96 + d4bf325 commit ef8aaa3

File tree

3 files changed

+153
-119
lines changed

3 files changed

+153
-119
lines changed

spec/Section 2 -- Language.md

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,10 +1251,15 @@ avoiding costly string building in clients at runtime.
12511251
If not defined as constant (for example, in {DefaultValue}), a {Variable} can be
12521252
supplied for an input value.
12531253

1254-
Variables must be defined at the top of an operation and are in scope throughout
1255-
the execution of that operation. Values for those variables are provided to a
1256-
GraphQL service as part of a request so they may be substituted in during
1257-
execution.
1254+
:: An _operation variable_ is a variable defined at the top of an operation. An
1255+
operation variable is in scope throughout the execution of that operation.
1256+
Values for operation variables are provided to a GraphQL service as part of a
1257+
request so they may be substituted in during execution.
1258+
1259+
:: A _fragment variable_ is a variable defined on a fragment. A fragment
1260+
variable is locally scoped, it may only be referenced within that fragment
1261+
(non-transitively). Values for fragment variables are provided to fragment
1262+
spreads.
12581263

12591264
In this example, we want to fetch a profile picture size based on the size of a
12601265
particular device:
@@ -1283,20 +1288,21 @@ size `60`:
12831288

12841289
**Variable Use Within Fragments**
12851290

1286-
Variables can be used within fragments. Operation-defined variables have global
1287-
scope within a given operation. Fragment-defined variables have local scope
1288-
within the fragment definition in which they are defined. A variable used within
1289-
a fragment must either be declared in each top-level operation that transitively
1290-
consumes that fragment, or by that same fragment as a fragment variable
1291-
definition. If a variable referenced in a fragment is included by an operation
1292-
where neither the fragment nor the operation defines that variable, that
1293-
operation is invalid (see
1291+
Variables can be used within fragments. An _operation variable_ has global scope
1292+
within that operation, including within any fragments that operation
1293+
transitively consumes. A variable used within a fragment must either be defined
1294+
by that fragment, or must be declared in each top-level operation that
1295+
transitively consumes that fragment. If a variable referenced in a fragment is
1296+
defined in both the fragment and the operation, the fragment definition will be
1297+
used. If a variable is referenced in a fragment and is not defined by that
1298+
fragment, then any operation that transitively references the fragment and does
1299+
not define that variable is invalid (see
12941300
[All Variable Uses Defined](#sec-All-Variable-Uses-Defined)).
12951301

12961302
## Fragment Variable Definitions
12971303

1298-
Fragments may define locally scoped variables. This allows fragments to be
1299-
reused while enabling the caller to specify the fragment's behavior.
1304+
Fragments may define locally scoped variables. This allows the caller to specify
1305+
the fragment's behavior.
13001306

13011307
For example, the profile picture may need to be a different size depending on
13021308
the parent context:
@@ -1321,8 +1327,8 @@ fragment dynamicProfilePic($size: Int! = 50) on User {
13211327
In this case the `user` will have a larger `profilePic` than those found in the
13221328
list of `friends`.
13231329

1324-
A fragment-defined variable is scoped to the fragment that defines it.
1325-
Fragment-defined variables are allowed to shadow operation-defined variables.
1330+
A _fragment variable_ is scoped to the fragment that defines it. A fragment
1331+
variable may shadow an _operation variable_.
13261332

13271333
```graphql example
13281334
query withShadowedVariables($size: Int!) {
@@ -1343,11 +1349,11 @@ fragment dynamicProfilePic($size: Int!) on User {
13431349
}
13441350
```
13451351

1346-
The profilePic for `user` will be determined by the variables set by the
1347-
operation, while `secondUser` will always have a `profilePic` of size `10`. In
1348-
this case, the fragment `variableProfilePic` uses the operation-defined
1349-
variable, while `dynamicProfilePic` uses the value passed in via the fragment
1350-
spread's `size` argument.
1352+
In the example above, the profilePic for `user` will be determined by the
1353+
variables set by the operation, while `secondUser` will always have a
1354+
`profilePic` of size `10`. In this case, the fragment `variableProfilePic` uses
1355+
the value of the _operation variable_, while `dynamicProfilePic` uses the value
1356+
of the _fragment variable_ passed in via the fragment spread's `size` argument.
13511357

13521358
## Type References
13531359

0 commit comments

Comments
 (0)