Skip to content

GraphQL

This content is for Backend. Switch to the latest version for up-to-date documentation.

GraphQL is an API style where the client can request exactly the fields it needs.

Instead of multiple endpoints (/users, /users/:id, /users/:id/posts), GraphQL often exposes a single endpoint (commonly /graphql) and the client sends queries.

  • Frontends that need flexible queries (complex pages, many UI components)
  • Public APIs where clients need different shapes of the same data
  • GitHub: GraphQL API for developer/platform integrations
  • Shopify: GraphQL Admin API
  • Meta (Facebook): created GraphQL and uses it heavily

Pros:

  • Reduces over-fetching/under-fetching (client chooses fields)
  • Strong typing via schema
  • Great developer experience with tooling

Cons:

  • Caching can be harder than REST (queries vary)
  • You still need to design your schema carefully
  • Performance pitfalls (e.g., N+1 queries) if resolvers are naive

Query:

query {
user(id: 42) {
id
name
posts {
id
title
}
}
}

Response:

{
"data": {
"user": {
"id": 42,
"name": "Alice",
"posts": [{ "id": 1, "title": "Hello" }]
}
}
}
Built with passion by Ngineer Lab