What is GraphQL? GraphQL Explained
Blog / What is GraphQL? GraphQL Explained
What is GraphQL? And why do so many apps use it instead of a classic REST API?
The Problem: Over-fetching and Under-fetching
With REST, the server decides the shape of each endpoint's response. A profile screen that needs only a user's name gets the whole user object back from GET /users/42.This wastes bandwidth and is called over-fetching.
One Endpoint and a Schema
GraphQL is an open-source query language for APIs and a runtime that answers those queries. It was created by Facebook in 2012 and open-sourced in 2015.Usually, a server exposes just one single endpoint, typically /graphql. The server publishes a strongly typed schema that defines types like User and Post, their fields, and how they connect to form a graph.type User {
name: String!
posts: [Post!]!
}
type Post {
title: String!
}
Queries, Mutations and Subscriptions
Operations in GraphQL fall into three categories.QueryA query reads data. This one reads user 42, its name and the title of all of its posts.query GetUserAndPosts {
user(id: "42") {
name
posts {
title
}
}
}
mutation AddNewPost {
addPost(userId: "42", title: "GraphQL Explained") {
title
}
}
subscription OnPostAdded {
postAdded(userId: "42") {
title
}
}
A Query End to End
Let's walk through one query asking for a user's name and the titles of their posts.The client sends it as one POST request to /graphql. The server validates it against the schema. Then it runs a resolver for each requested field.A resolver is a function that fetches the data for that specific field.
{
"data": {
"user": {
"name": "Ada Lovelace",
"posts": [
{ "title": "Analytical Engine Notes" },
{ "title": "Future of Computing" }
]
}
}
}
The N+1 Problem and DataLoader
Because resolvers run per field, asking for 50 users and their posts can trigger inefficient database calls. The server runs one query for the users, plus 50 separate queries for the posts.This is the N+1 problem.
Errors and Caching: Two Interview Traps
In interviews, you must watch out for two common traps.Errors come back with 200 OKGraphQL usually returns an HTTP 200 OK status code even when part of the query fails. The response holds partial data plus an errors array.The client must check errors in the JSON body, not just the status code.HTTP cachingStandard REST responses are cached by URL with GET requests. GraphQL sends many different queries as POST requests to one endpoint, so HTTP caching and CDNs do not work out of the box.Client caches and persisted queries, which send a registered query by its hash over a GET request, are the usual fixes.GraphQL vs REST: When to Pick Each
| Feature | GraphQL | REST |
|---|---|---|
| Endpoints | Usually one endpoint | Many URLs mapped to resources |
| Response shape | Decided by the client | Decided by the server |
| Data fetching | Prevents over and under-fetching | Prone to over and under-fetching |
| HTTP caching | Difficult out of the box | Works naturally by URL |
| Error signalling | Errors in the JSON body with HTTP 200 | Indicated by HTTP status codes |
| Best fit | Complex nested data and many varied clients | Simple, highly cacheable or public APIs |
Recap
| Term | What it means |
|---|---|
| Schema | The strongly typed definition of types, fields and relationships |
| Query | An operation that reads data |
| Mutation | An operation that changes data |
| Subscription | An operation that pushes updates over an open connection |
| Resolver | A function that fetches data for one field |
| N+1 problem | One query for a list plus N separate queries |
| DataLoader | A utility that batches data fetches into a single query |
| Persisted queries | A caching fix that sends a query hash over a GET request |
About TechPrep
Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.