Back to Overview

GraphQL

api backend query advanced

What is GraphQL?

GraphQL is a way to ask for exactly what you want from a website. Instead of getting a fixed set of information (like getting a whole burger when you only want the bun and meat), you can ask for just the pieces you need. This means you can get all the information you want in a single request, without getting extra stuff you don’t need.

Simple Analogy

Think of GraphQL like ordering food:


  • Regular Websites (REST): Like ordering from a fixed menu. If you want a burger, you get the complete burger as it’s made - with pickles, onions, and everything else. If you don’t want pickles, too bad! You get them anyway and have to remove them yourself. If you also want fries, you need to place a separate order.

  • GraphQL: Like ordering at a custom sandwich shop. You tell the server exactly what you want: “I want bread, lettuce, tomato, but no onions, plus a side of fries.” You get precisely what you asked for - nothing more, nothing less - all in one order.

Key Concepts

  • Schema: Like a menu that tells you what you can order
  • Queries: Ways to ask for information (like saying “I want a burger”)
  • Mutations: Ways to change information (like saying “add cheese to my burger”)
  • Resolvers: The kitchen staff that knows how to make what you ordered
  • Single Endpoint: Instead of going to different counters for different food, you order everything at one place
  • No Over-fetching/Under-fetching: You get exactly what you ask for, no more and no less

Example

# Asking for a user and their posts
query {
  user(id: "123") {
    name
    email
    posts {
      title
      content
      createdAt
    }
  }
}

With regular websites, this might require going to different pages:

  1. First go to /users/123 to get the user’s name and email
  2. Then go to /users/123/posts to get their posts

But with GraphQL, you get everything in one request!