Created
March 14, 2026 15:12
-
-
Save mohashari/aabb88ccc9700b95c0407e0812106082 to your computer and use it in GitHub Desktop.
Code snippets — Graphql Vs Rest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # One request instead of 5 | |
| query DashboardData { | |
| currentUser { | |
| name | |
| recentOrders(limit: 10) { | |
| id | |
| total | |
| status | |
| items { name quantity } | |
| } | |
| notifications(unread: true) { | |
| message | |
| createdAt | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Without DataLoader — N+1 queries | |
| const resolvers = { | |
| Post: { | |
| author: (post) => User.findById(post.authorId) // 1 query per post! | |
| } | |
| } | |
| // With DataLoader — 1 batched query | |
| const userLoader = new DataLoader(async (ids) => { | |
| const users = await User.findAll({ where: { id: ids } }); | |
| return ids.map(id => users.find(u => u.id === id)); | |
| }); | |
| const resolvers = { | |
| Post: { | |
| author: (post) => userLoader.load(post.authorId) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # REST is easy to explore and debug | |
| curl -X GET https://api.example.com/users/42 \ | |
| -H "Authorization: Bearer token123" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Client asks for exactly what it needs | |
| query { | |
| user(id: "42") { | |
| name | |
| posts(last: 5) { | |
| title | |
| createdAt | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment