GraphQL vs REST
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
GraphQL is a typed query language and a server execution model. The server publishes a schema containing types, fields, arguments, and entry points such as Query, Mutation, and, when supported, Subscription. A client operation is checked against that schema before it runs. If it is valid, the server resolves the selected fields and returns a result shaped like the selection.
A product screen may need only a product name, seller name, and three review summaries. A GraphQL selection such as product { name seller { name } } asks for that connected view without requesting every available field. This is one GraphQL operation, but it is not automatically one database query; resolver and data-loading design determine the actual work.
REST starts from resources and a uniform interaction model. GET /products/42 retrieves a representation chosen by the server's media-type contract. That representation can be coarse-grained, include a seller summary, or link to related resources; REST does not require a separate request for every database table. HTTP metadata also supports conditional requests, shared caches, and intermediary visibility.
GraphQL moves response-shape choice toward the client, so the server must defend a larger execution surface. It should limit depth or calculated cost, cap list sizes, avoid the N+1 query pattern by batching repeated loads, and authorize every selected field in context. Execution can return both partial data and an errors list, so monitoring only the HTTP status can miss application-level failures.
GraphQL is useful when several clients need changing views over connected data and the team can operate its query engine safely. REST is useful when stable resource contracts, standard HTTP caching, broad clients, and visible intermediary behavior matter most. A system can use both at different boundaries. The decision should follow client needs, data shape, authorization, caching, and measured workload.
GraphQL vs REST
Direct Answer
GraphQL is a typed query language and execution model in which clients select fields from a schema and receive a response shaped like that selection. REST organizes interactions around identified resources and uniform representations. GraphQL suits flexible connected views; REST fits resource contracts and standard HTTP behavior. Neither is always the better choice.
Deep Dive & Architecture
A GraphQL schema defines object types, fields, arguments, and operation entry points. The type system allows an operation to be validated before execution and gives tools a basis for discovery and code generation. Query and mutation results contain data, while an errors collection can describe request or field problems; subscriptions produce a response stream when the schema supports them.
REST constrains interaction rather than prescribing one schema language. Resources have identifiers, representations can link to related resources, and HTTP gives common semantics to methods and metadata. A server can create coarse-grained REST representations for a screen; REST does not require a client to request every database object separately.
Graph traversal can hide expensive execution. Fetching orders, then items, then products per item can cause repeated database calls unless resolvers batch and cache within the request. Servers should limit depth, aliases, list sizes, or calculated cost and enforce authorization even when a field appears deep inside a query.
Caching differs rather than disappears. REST representations can use URI-based shared caches, freshness, and validators. GraphQL clients often normalize objects by identity, while persisted operations, GET for suitable queries, or gateway-specific caching can add other strategies. Teams should choose based on data shape and operational behavior, then test real workloads.
Code Example
query ProductCard($id: ID!) {
product(id: $id) {
id
name
seller { name }
reviews(first: 3) {
rating
summary
}
}
}
A successful result follows that selection:
{
"data": {
"product": {
"id": "P-42",
"name": "Desk lamp",
"seller": {"name": "Northwind"},
"reviews": [{"rating": 5, "summary": "Easy to assemble"}]
}
}
}
The schema must define these fields and arguments, and the server must authorize and execute each selected part efficiently. The query's convenience does not guarantee one database query.
Check your understanding
Answer these without looking back at the article.
- How can GraphQL resolvers create an N+1 query problem?
- Can REST return an embedded or client-specific representation?
- Why can a GraphQL response contain both `data` and `errors`?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.