What is GraphQL?

◕ GraphQLLesson 1Beginner

GraphQL is a query language for APIs and a runtime for executing those queries, developed by Facebook in 2012 and open-sourced in 2015. Unlike REST, GraphQL lets clients request exactly the data they need — no more, no less — in a single request.

1 The Core Problems GraphQL Solves
  • Over-fetching: REST returns entire objects even when you only need two fields. GraphQL returns only what you ask for.
  • Under-fetching: REST often requires multiple round-trips (e.g., fetch user, then fetch their posts, then fetch post comments). GraphQL fetches it all in one query.
  • Rigid endpoints: REST has fixed URL endpoints. GraphQL exposes a single endpoint (/graphql) and the query shape determines the response.
  • Type safety: GraphQL schemas are strongly typed, enabling tooling, auto-complete, and validation at the schema level.
2 REST vs GraphQL Side-by-Side
REST — Multiple Requests
GET /users/1        -> { id, name, email, address, phone, ... } (over-fetch)
GET /users/1/posts  -> [ { id, title, body, createdAt, ... } ]
GET /posts/42/comments -> [ { id, text, author, ... } ]

// 3 round trips, lots of unused data
GraphQL — Single Request, Exact Data
POST /graphql

# Query
{
  user(id: "1") {
    name
    posts {
      title
      comments {
        text
        author { name }
      }
    }
  }
}

# Response — exactly what was asked
{
  "data": {
    "user": {
      "name": "Balaji",
      "posts": [{ "title": "...", "comments": [...] }]
    }
  }
}
3 When to Use GraphQL vs REST
ScenarioBest Choice
Public API with many different consumersREST
Complex UIs with many data relationshipsGraphQL
Mobile apps (limited bandwidth)GraphQL
Simple CRUD microservicesREST
Real-time featuresGraphQL (Subscriptions)
Third-party integrationsREST
4 Code Challenge
Challenge: Explore the public GitHub GraphQL API at https://api.github.com/graphql using the GitHub Explorer. Write a query that fetches your GitHub username, your 5 most recent repositories (name + star count), and your follower count — all in one request.