What is GraphQL and how does it differ from REST?
Learn what GraphQL is, how its single typed endpoint works, and how it differs from REST by letting clients fetch exactly the data they need.
Expected Interview Answer
GraphQL is a query language and server-side runtime for APIs that lets the client ask for exactly the data it needs from a single endpoint, whereas REST exposes many fixed endpoints that each return a predefined shape of data.
With REST you typically call multiple URLs and often get back more (over-fetching) or less (under-fetching) than you need, forcing extra round trips. GraphQL exposes one endpoint backed by a strongly typed schema; the client sends a query describing the precise fields and nested relationships it wants, and the server resolves each field and returns a matching JSON tree. This shifts control of the response shape from the server to the client while keeping a single contract.
- Fetch exactly the fields needed — no over- or under-fetching
- Single endpoint instead of many URLs
- Strongly typed schema acts as a live contract
- Fewer network round trips for nested data
- Introspection enables great tooling and docs
AI Mentor Explanation
REST is like asking the scorer for a fixed printed scorecard: you get the whole sheet even if you only wanted one batter's strike rate. GraphQL is like telling the scorer 'give me just Kohli's runs, balls, and fours' — one request, and you receive precisely those numbers and nothing else, drawn from the same match record.
Step-by-Step Explanation
Step 1
Define the schema
Describe your types, fields, and their relationships in the schema so the server has a typed contract.
Step 2
Client writes a query
The client sends a query listing exactly the fields (and nested fields) it wants from the single endpoint.
Step 3
Server validates
GraphQL validates the query against the schema, rejecting unknown or malformed fields before execution.
Step 4
Resolvers run
Each requested field is resolved by a resolver function that fetches its value from a database, service, or other source.
Step 5
Shaped response returns
The server assembles a JSON tree matching the query shape and returns it in one round trip.
What Interviewer Expects
- Clear definition of GraphQL as a query language and runtime
- The single-endpoint vs many-endpoints contrast with REST
- Understanding of over-fetching and under-fetching
- Awareness of the typed schema as a contract
- Honest trade-offs (caching, complexity) not just hype
Common Mistakes
- Calling GraphQL a database — it is an API layer, not storage
- Saying it replaces REST entirely rather than being an alternative
- Ignoring that HTTP caching is harder with a single POST endpoint
- Claiming GraphQL is always faster regardless of use case
- Confusing the schema with the underlying data source
Best Answer (HR Friendly)
“GraphQL is a way for apps to ask an API for exactly the data they need, using one address instead of many. Compared to REST, it avoids getting too much or too little data, which usually means fewer requests and simpler front-end code.”
Code Example
query {
user(id: "42") {
name
email
posts(last: 2) {
title
}
}
}// GET /users/42 -> full user object (over-fetch)
// GET /users/42/posts -> all posts (over-fetch)
// Then filter to last 2 posts and pick title on the clientFollow-up Questions
- How does GraphQL handle caching compared to REST?
- What is over-fetching and under-fetching?
- What is the N+1 problem in GraphQL and how do you solve it?
- Can GraphQL and REST coexist in the same system?
- How does introspection help API tooling?
MCQ Practice
1. What is the main advantage of GraphQL over REST for data fetching?
GraphQL lets the client specify precisely which fields to return, eliminating over- and under-fetching common with fixed REST endpoints.
2. How many endpoints does a typical GraphQL API expose?
GraphQL usually exposes a single endpoint that accepts all queries, mutations, and subscriptions, unlike REST's many URLs.
Flash Cards
What is GraphQL? — A query language and server runtime that lets clients request exactly the data they need from a single, strongly typed API endpoint.
Over-fetching vs under-fetching — Over-fetching = getting more data than needed; under-fetching = needing extra requests to get enough. GraphQL avoids both.
GraphQL vs REST endpoints — REST exposes many fixed URLs; GraphQL exposes one endpoint driven by client queries against a schema.
Who controls the response shape? — In GraphQL the client controls the shape via its query; in REST the server defines it per endpoint.
Continue Learning
Related Interview Questions
What is the difference between client-side and server-side GraphQL?
easy
What is a GraphQL schema and what is the Schema Definition Language (SDL)?
medium
What is the difference between a query, a mutation, and a subscription in GraphQL?
medium
How does GraphQL versioning differ from REST versioning?
medium