Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/antoine-coulon/graphql-n-plus-one
Source code for blog posts related to the N+1 problem and one of its solution in the context of GraphQL
https://github.com/antoine-coulon/graphql-n-plus-one
Last synced: 14 days ago
JSON representation
Source code for blog posts related to the N+1 problem and one of its solution in the context of GraphQL
- Host: GitHub
- URL: https://github.com/antoine-coulon/graphql-n-plus-one
- Owner: antoine-coulon
- Created: 2024-09-12T12:04:59.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-12T12:09:22.000Z (2 months ago)
- Last Synced: 2024-10-09T10:06:27.563Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
This is a simple repository illustrating the N+1 Queries problem with GraphQL and SQLite.
### Observing the N+1 problem
1) Run the server
```sh
node --run start:problem
```2) Use GraphiQL to run queries at http://127.0.0.1:4000/graphiql
3) Run the query
```graphql
query NPlusOneQuery {
books {
id
reviews {
id
}
}
}
```4) Check the console
You should see one initial query (`select * books`) (what we refer as being +1) and then N queries executed for each book review.
Having 10 books, we will have something like
```sql
# 1
SELECT * FROM books;# + N
SELECT * FROM reviews WHERE book_id = 1;
SELECT * FROM reviews WHERE book_id = 2;
SELECT * FROM reviews WHERE book_id = 3;
SELECT * FROM reviews WHERE book_id = 4;
SELECT * FROM reviews WHERE book_id = 5;
```### Solving the N+1 problem
1) Run the server with loaders enabled
```sh
node --run start:solution
```Do the same 2) and 3) steps that are described above.
4) Check the console
You should see one initial query and then another query executed to get all reviews at once.
Having 10 books, we will have something like
```sql
# 1
SELECT * FROM books;# + 1
SELECT * FROM reviews WHERE book_id IN (1,2,3,4,5,6,7,8,9,10);
```