Skip to content

Commit 19cedbd

Browse files
committed
Modify N+1 example and explanation.
1 parent 955aab0 commit 19cedbd

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/pages/learn/performance.mdx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,29 @@ Sending hashed queries instead of their plaintext versions has the additional be
2626

2727
GraphQL is designed in a way that allows you to write clean code on the server, where every field on every type has a focused single-purpose function for resolving that value. However, without additional consideration, a naive GraphQL service could be very "chatty" or repeatedly load data from your databases.
2828

29-
Consider the following query—to fetch a hero along with a list of their friends, we can imagine that as the field resolvers execute there will be one request to the underlying data source to get the character object, and then three subsequent requests to load the friends' character objects:
29+
Consider the following query—to fetch a hero along with a list of their friends, we can imagine that as the field resolvers execute there will be one request to the underlying data source to get the character object, another to fetch their friends, and then three subsequent requests to load the lists of starships for each human friend:
3030

3131
```graphql
3232
# { "graphiql": true }
3333
query HeroWithFriends {
3434
# 1 request for the hero
3535
hero {
3636
name
37-
# 3 more requests for each friend
37+
# 1 request for the hero's N friends
3838
friends {
3939
name
40+
# N starship requests - one for each of the hero's N human friends
41+
... on Human {
42+
starships {
43+
name
44+
}
45+
}
4046
}
4147
}
4248
}
4349
```
4450

45-
This is known as the N+1 problem, where the first request to an underlying data source leads to N subsequent requests to resolve the data for all of the requested fields.
51+
This is known as the N+1 problem, where an initial request to an underlying data source (for a hero's friends) leads to N subsequent requests to resolve the data for all of the requested fields (for the friends' starship data).
4652

4753
This is commonly solved by a batching technique, where multiple requests for data from a backend are collected over a short period and then dispatched in a single request to an underlying database or microservice by using a tool like Facebook's [DataLoader](https://github.com/facebook/dataloader). Additionally, certain GraphQL implementations may have built-in capabilities that allow you to translate operation selection sets into optimized queries to underlying data sources.
4854

0 commit comments

Comments
 (0)