DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on • Originally published at sreekarreddy.com

🎯 GraphQL Explained Like You're 5

Order exactly what you want, nothing more

Day 27 of 149

πŸ‘‰ Full deep-dive with code examples


The Custom Order

At most restaurants, you order a whole meal:

  • Burger + fries + drink + salad

But you maybe just wanted the burger! πŸ”

GraphQL lets you order exactly what you need!


REST vs GraphQL

REST (fixed menu):

GET /user/5
Returns: name, email, address, phone, birthday,
         avatar, preferences, history, ...
Enter fullscreen mode Exit fullscreen mode

You get everything, even stuff you don't need!

GraphQL (custom order):

{
  user(id: 5) {
    name
    email
  }
}
Enter fullscreen mode Exit fullscreen mode

You get exactly: name and email. That's it!


The Magic

One endpoint, infinite flexibility:

{
  user(id: 5) {
    name
    posts {
      title
      likes
    }
    friends {
      name
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Get user + their posts + their friends in ONE request!


When to Use

  • REST: Simple CRUD, standard needs
  • GraphQL: Complex data needs, mobile apps (save bandwidth)

In One Sentence

GraphQL lets you ask for exactly the data you need in one request, nothing more, nothing less.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)