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.
A profile screen sends GET /users/42 to a REST API and gets the whole user back, with id, name, email, bio and createdAt, though only the name is used
To show that user's posts, the client must make a second request to GET /users/42/posts. This requires extra round trips and is called under-fetching.Every new screen tends to need its own custom endpoint. This is where GraphQL comes in.

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.
graphql
type User { name: String! posts: [Post!]! } type Post { title: String! }
GraphQL is not a database and is not tied to any specific programming language or storage engine.

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.
graphql
query GetUserAndPosts { user(id: "42") { name posts { title } } }
MutationA mutation changes server-side data. This mutation adds a new post for user 42 and returns the new post's title.
graphql
mutation AddNewPost { addPost(userId: "42", title: "GraphQL Explained") { title } }
SubscriptionA subscription keeps a connection open, usually over WebSockets, to push live updates. This subscription pushes the title of each new post user 42 adds, as soon as it is added.
graphql
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.
The query is sent as one POST to the GraphQL server at /graphql, validated against the schema, then a resolver runs for user, name, posts and title
The JSON response has a data key that mirrors the shape of the query exactly.
json
{ "data": { "user": { "name": "Ada Lovelace", "posts": [ { "title": "Analytical Engine Notes" }, { "title": "Future of Computing" } ] } } }
Resolving field by field is flexible, but it creates a separate performance risk.

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.
The GraphQL server sends 51 queries to the database: one for 50 users, then one posts query per user
To fix this, developers use DataLoader. It collects the loads made in one tick of the event loop and batches them into a single query.DataLoader also provides a cache, but it lasts for one request only.

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

FeatureGraphQLREST
EndpointsUsually one endpointMany URLs mapped to resources
Response shapeDecided by the clientDecided by the server
Data fetchingPrevents over and under-fetchingProne to over and under-fetching
HTTP cachingDifficult out of the boxWorks naturally by URL
Error signallingErrors in the JSON body with HTTP 200Indicated by HTTP status codes
Best fitComplex nested data and many varied clientsSimple, highly cacheable or public APIs

Recap

TermWhat it means
SchemaThe strongly typed definition of types, fields and relationships
QueryAn operation that reads data
MutationAn operation that changes data
SubscriptionAn operation that pushes updates over an open connection
ResolverA function that fetches data for one field
N+1 problemOne query for a list plus N separate queries
DataLoaderA utility that batches data fetches into a single query
Persisted queriesA 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.