Skip to content

Commit

Permalink
Remove bad fragment recommendations in best practices docs (#11390)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller authored Nov 27, 2023
1 parent eef3a75 commit 4adbb2e
Showing 1 changed file with 0 additions and 81 deletions.
81 changes: 0 additions & 81 deletions docs/source/data/operation-best-practices.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -160,87 +160,6 @@ query GetGlobalStatus {
* If you have collections of components that _are_ always rendered together, you can use fragments to distribute the structure of a single query between them. See [Colocating fragments](./fragments/#colocating-fragments).
* If you're querying a list field that returns more items than your component needs to render, you should [paginate that field](../pagination/overview/).



## Use fragments to encapsulate related sets of fields

[GraphQL fragments](./fragments/) are sets of fields you can share across multiple operations. Here's an example declaration:

```graphql
# Recommended ✅
fragment NameParts on Person {
title
firstName
middleName
lastName
}
```

It's likely that multiple queries in an app require a person's full name. This `NameParts` fragment helps keep those queries consistent, readable, and short:

```graphql
# Recommended ✅
query GetAttendees($eventId: ID!) {
attendees(id: $eventId) {
id
rsvp
...NameParts # Include all fields from the NameParts fragment
}
}
```

### Avoid excessive or illogical fragments

If you use _too many_ fragments, your queries might become _less_ readable:

<ExpansionPanel title="Click to expand">

```graphql
# Use caution ⚠️
query GetAttendees($eventId: ID!) {
attendees(id: $eventId) {
id
rsvp
...NameParts
profile {
...VisibilitySettings
events {
...EventSummary
}
avatar {
...ImageDetails
}
}
}
}

```

</ExpansionPanel>

Additionally, only define fragments for sets of fields that share a logical semantic relationship. _Don't_ create a fragment just because multiple queries happen to share certain fields:

```graphql
# Recommended ✅
fragment NameParts on Person {
title
firstName
middleName
lastName
}

# Not recommended ❌
fragment SharedFields on Country {
population
neighboringCountries {
capital
rivers {
name
}
}
}
```

## Query global data and user-specific data separately

Some fields return the exact same data regardless of which user queries them:
Expand Down

0 comments on commit 4adbb2e

Please sign in to comment.