Skip to content

Instantly share code, notes, and snippets.

@mohashari
Created March 14, 2026 15:12
Show Gist options
  • Select an option

  • Save mohashari/aabb88ccc9700b95c0407e0812106082 to your computer and use it in GitHub Desktop.

Select an option

Save mohashari/aabb88ccc9700b95c0407e0812106082 to your computer and use it in GitHub Desktop.
Code snippets — Graphql Vs Rest
# One request instead of 5
query DashboardData {
currentUser {
name
recentOrders(limit: 10) {
id
total
status
items { name quantity }
}
notifications(unread: true) {
message
createdAt
}
}
}
// 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)
}
}
# REST is easy to explore and debug
curl -X GET https://api.example.com/users/42 \
-H "Authorization: Bearer token123"
# Client asks for exactly what it needs
query {
user(id: "42") {
name
email
posts(last: 5) {
title
createdAt
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment