Skip to content

Commit 5cf74db

Browse files
committed
Update the description for when to use a fragment
1 parent 43ae7ba commit 5cf74db

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

spec/Section 2 -- Language.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,9 @@ FragmentName : Name but not `on`
587587

588588
Fragments are the primary unit of composition in GraphQL.
589589

590-
Fragments allow for the reuse of common repeated selections of fields, reducing
591-
duplicated text in the document. Inline Fragments can be used directly within a
592-
selection to condition upon a type condition when querying against an interface
593-
or union.
590+
Fragments allow for the definition of selection sets that are colocated with the logic that requires those selections, making it easier to add and remove selections as needed.
594591

595-
For example, if we wanted to fetch some common information about mutual friends
596-
as well as friends of some user:
592+
For example, if we have some `friendProfile` logic that requires `id`, `name`, and `profilePic`, and we want to apply that logic to the mutual friends as well as friends of some user:
597593

598594
```graphql example
599595
query noFragments {
@@ -612,28 +608,32 @@ query noFragments {
612608
}
613609
```
614610

615-
The repeated fields could be extracted into a fragment and composed by a parent
616-
fragment or operation.
611+
The fields required of `friendProfile` can be extracted into a fragment and composed
612+
by a parent fragment or operation.
617613

618614
```graphql example
619615
query withFragments {
620616
user(id: 4) {
621617
friends(first: 10) {
622-
...friendFields
618+
...friendProfileFragment
623619
}
624620
mutualFriends(first: 10) {
625-
...friendFields
621+
...friendProfileFragment
626622
}
627623
}
628624
}
629-
630-
"Common fields for a user's friends."
631-
fragment friendFields on User {
625+
```
626+
```graphql example
627+
"Fields required to display a friend's profile"
628+
fragment friendProfileFragment on User {
632629
id
633630
name
634631
profilePic(size: 50)
635632
}
636633
```
634+
If `friendProfile` no longer needs `name`, the `name` field can be removed from
635+
`friendProfileFragment` and it will no longer be fetched in both locations the
636+
fragment is consumed.
637637

638638
Fragments are consumed by using the spread operator (`...`). All fields selected
639639
by the fragment will be added to the field selection at the same level as the

0 commit comments

Comments
 (0)