REST API vs GraphQL: What’s the Difference and When to Use Each?

Imagine you are in an interview and you are asked: “Your mobile banking app is suddenly slow. It only needs a customer’s name, mobile number and credit score. How would you improve it?” A good answer depends on understanding two ways of building APIs: REST, which has been the standard for decades, and GraphQL, which has become a popular alternative. This guide explains both in simple words, shows them side by side with real examples, compares their strengths and weaknesses, and tells you exactly when to choose each one.

What is an API?

Every product has three parts: a front end (what the user sees), a back end (the server) and a database (where data is stored). The front end should not talk to the database directly, because that isn’t recommended. Instead, it sends a request to the back end, the back end gets the data from the database and returns it. That request-and-response channel is the API, short for Application Programming Interface.

A coffee vending machine is a perfect analogy. You press one button and get a latte in your hand. All the mechanics behind the scenes are hidden. An API does the same: it hides the complexity and gives you only what is needed, which is good for security and performance, and means people don’t need to understand everything about your code.

Diagram: an app sends a request through an API to a server and database
Press a button, get coffee: the API hides the complexity.

What is REST?

REST stands for Representational State Transfer, and it is the style behind about 90% of the APIs used in production. You don’t need the theory in depth. What matters is how it is structured:

  • Resources. Everything is a resource (a user, a post, an order), and each has its own unique identifier, a URI.
  • Multiple endpoints. Each resource or type of data has its own endpoint, and each endpoint returns a predefined set of data.
  • HTTP methods. You act on resources using GET (read), POST (create), PUT (update) and DELETE.
  • Response format. The server returns a representation of the resource, usually JSON or XML.
  • Query parameters. Clients can filter, sort and paginate data using query parameters.

For a social media app, REST endpoints might look like this:

GET /users/42              -> user details
GET /users/42/posts        -> that user's posts
GET /users/42/followers    -> that user's followers

REST is a good “fixed menu”. Think of an Indian student mess that serves a fixed thali: two chapatis, rice, dal, vegetables and a sweet, all in set portions. If you want two extra sweets, you have to pay for a whole new thali. The server decides what you get.

The two problems: over-fetching and under-fetching

Because every endpoint returns a fixed shape of data, REST runs into two classic problems.

  • Over-fetching: you get more data than you need. If you only want a user’s name, /users/42 still sends the id, email, follower counts and everything else. The extra data wastes bandwidth and increases latency.
  • Under-fetching: one endpoint doesn’t give you enough. If you need the user’s details and their followers, you must call two different endpoints, which makes the app slower. This also leads to the well-known “N+1” problem, where related data is fetched with many separate requests.
REST returns all fields while GraphQL returns only the field you ask for
REST sends everything, GraphQL sends only what you ask for.

What is GraphQL?

GraphQL is a query language for APIs, developed at Facebook (now Meta) and introduced around 2012. Its big idea flips the REST model: instead of the server deciding what to send, the client describes exactly what it wants. Going back to the food analogy, it is like an à la carte buffet. Everything is on the table, you pick what you want and pay only for that. If you are fasting, you take only fruit.

In GraphQL there is usually one single endpoint (for example /graphql). The client sends a query to that endpoint saying which resources and which fields it needs, and gets exactly those.

REST needs several endpoints while GraphQL uses one endpoint
Three calls with REST, one call with GraphQL.

The building blocks of GraphQL

  • Schema: the blueprint that defines all the data clients can request, including the types and their relationships.
  • Query: a request for data that follows the structure defined in the schema. In CRUD terms, it is the “read”.
  • Mutation: a request that modifies data (create, update and delete).
  • Subscription: a way for clients to receive notifications when data changes.
  • Resolver: server-side code that fetches the requested data, maybe from several databases or services, and assembles it into a response that matches the query.
The four building blocks of GraphQL: schema, query, mutation and resolver
The core GraphQL building blocks.

Side by side: the same data in REST and GraphQL

Suppose we want a user’s name and the titles of their posts.

With REST

GET /users/42          -> returns ALL user fields (over-fetching)
GET /users/42/posts    -> second call needed (under-fetching)

With GraphQL

First, the schema describes the available types and what can be queried:

schema
type Post {
  id: ID!
  title: String!
}

type User {
  id: ID!
  name: String!
  posts: [Post!]!
}

type Query {
  user(id: ID!): User
}

Then one request asks for exactly what is needed:

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

The response has the same shape as the query, with nothing extra:

response
{
  "data": {
    "user": {
      "name": "Sara",
      "posts": [
        { "title": "My first post" },
        { "title": "Learning GraphQL" }
      ]
    }
  }
}

Changing data uses a mutation (this is an illustrative example):

mutation {
  createPost(title: "Hello") {
    id
    title
  }
}

How REST and GraphQL are similar

  • Both are used to build APIs that let applications talk to each other over the internet.
  • Both usually run over HTTP (although GraphQL itself is protocol agnostic).
  • Both commonly send and receive JSON.
  • Both rely on frameworks and libraries that handle network details for you.

REST vs GraphQL: comparison table

Feature REST GraphQL
Approach Resource-oriented Query-oriented
Endpoints Many, usually one per resource Usually a single endpoint
Who decides the response shape The server (fixed shape) The client (it picks the fields)
Over-fetching Common Avoided
Under-fetching Often needs several requests One request can cover many resources
Learning curve Low, widely known Higher
Tooling needed Basic (cURL or a browser is enough) Heavier client and server tooling
HTTP caching Works well (GET is cacheable) Harder (single entry point, POST by default)
Changing the API Easy to add a new endpoint Schema changes can be complicated
Best for Simple CRUD, public APIs Complex, related data and mobile apps

The drawbacks of GraphQL

GraphQL is powerful, but it isn’t free. Software engineering is about trade-offs, so consider these before choosing it.

  • Upfront cost and tooling. The beauty of REST is that you can call an API with cURL or a browser. GraphQL needs heavier tooling on both client and server, which can be a sizable investment and may not be worth it for very simple CRUD APIs.
  • Caching is harder. REST uses HTTP GET, which has well-defined caching that browsers, CDNs, proxies and web servers already use. GraphQL has a single entry point and uses POST by default, which prevents full use of HTTP caching. It can be configured to do better, but it is extra work and not trivial.
  • Risky queries. Letting clients request exactly what they want also means they can request expensive things. Imagine a mobile app update that triggers an unexpected full table scan on a critical database table, bringing the database down when the app goes live. There are ways to reduce this risk (such as limiting query depth or complexity), but they add yet more complexity.
  • More complex architecture. Adding or changing something in an existing schema is more complicated than adding a new REST endpoint, so GraphQL is harder to learn, use and maintain.

When to use REST and when to use GraphQL

When to choose REST and when to choose GraphQL
Pick based on your data, or use both together.

Choose REST when

  • Your resources are simple and the data you need is predictable. An e-commerce site that lets customers browse products, add items to a cart and place orders fits REST perfectly using GET, POST, PUT and DELETE.
  • You are building a public API, or a straightforward service-to-service integration, such as a weather API.
  • You are building admin panels and dashboards where data is consistent and operations are simple create, read, update and delete.
  • You want fast implementation, easy caching and a team that already knows it.

Choose GraphQL when

  • Different screens need different slices of the same data. Think of a social platform: one place needs a user’s full details, another only their picture, another their picture, name and role. With REST you would end up creating many endpoints that are hard to maintain and scale. With GraphQL, the client simply queries the fields it needs.
  • You must pull data from multiple sources or deeply related data. A financial planning tool that needs bank transactions, investment portfolios and credit scores can expose a single endpoint, with resolvers fetching from each source.
  • You are building a front-end heavy mobile or web application where performance matters and bandwidth is limited.
  • You have a microservices architecture and want one clean entry point for clients.

Can they work together?

Yes. GraphQL doesn’t dictate a specific architecture, and it isn’t a replacement for REST. Since so many REST APIs are already built and well planned, you can introduce GraphQL on top of an existing REST API and keep using your existing API management tools. Some apps and clients can use GraphQL while others continue to use REST, giving you the best of both worlds.

How to answer the interview question

Back to the scenario: a mobile loan banking app is slow, and it only needs a customer’s full name, mobile number and credit score. A strong answer would go like this:

  1. Diagnose: the app is probably over-fetching, receiving much more data than needed (address, date of birth, father’s name), or making several REST calls to collect data from different endpoints.
  2. Propose: put a GraphQL layer on top of the existing REST APIs, and design a schema so the app asks for only the three fields it needs.
  3. Result: smaller responses, fewer round trips and a faster app, while the existing REST services stay in place.
  4. Show maturity: mention the trade-offs: caching, tooling and protecting backend databases from expensive queries.

Interview tip: never say one is “better”. Say it depends on the use case, explain the trade-off, and recommend the one that fits. That is how architects think.

Common mistakes and gotchas

Mistake Why it causes trouble
Choosing GraphQL for a very simple CRUD app The tooling and learning cost outweigh the benefits
Thinking GraphQL replaces REST They can coexist, and REST is still ideal for many cases
Ignoring caching in GraphQL A single POST endpoint doesn’t get HTTP caching for free
Allowing unlimited query depth or complexity A single heavy query can overload your database
Creating a new REST endpoint for every screen Endpoints multiply and become hard to maintain
Returning whole records from REST when clients need little Over-fetching wastes bandwidth and slows mobile apps
Changing a GraphQL schema without planning Schema changes are complicated and can break clients

Frequently asked questions

What is the main difference between REST and GraphQL?

REST gives you fixed data from multiple endpoints, with the server deciding the shape. GraphQL gives you a single endpoint where the client asks for exactly the fields it needs.

Is GraphQL better than REST?

Not always. GraphQL is great for complex, related data and mobile apps, but REST is simpler, widely known and easier to cache. The right choice depends on your project.

What are over-fetching and under-fetching?

Over-fetching is getting more data than you need. Under-fetching is getting too little from one endpoint and having to make extra calls for the rest.

Who created GraphQL?

It was developed at Facebook, now Meta, and introduced around 2012.

What is a resolver in GraphQL?

A resolver is server-side code that fetches the data for a query, possibly from several sources, and assembles the response in the shape the query asks for.

What are mutations and subscriptions?

Mutations modify data (create, update, delete). Subscriptions let clients receive notifications when data changes.

Is GraphQL harder to cache than REST?

Yes by default. REST uses cacheable GET requests, while GraphQL typically uses one endpoint with POST, so extra setup is needed to get good caching.

Can I use REST and GraphQL together?

Yes. You can add GraphQL on top of an existing REST API and let some clients use one and some use the other.

Which should a beginner learn first?

Start with REST. It is simpler, widely used, and understanding it makes GraphQL’s benefits easier to appreciate.

Conclusion

REST and GraphQL solve the same problem, moving data between a front end and a back end, in different ways. REST is simple, familiar and easy to cache, which makes it the best fit for public APIs, admin panels and straightforward CRUD. GraphQL lets clients ask for exactly what they need from a single endpoint, which removes over-fetching and under-fetching and shines with complex data and mobile apps, at the cost of more tooling, harder caching and extra care around expensive queries.

The best choice is the one that fits your project, and sometimes it is both. Start from your data and your clients: if they need predictable, simple resources, use REST. If different screens need different slices of related data from many sources, use GraphQL, ideally on top of what you already have.

Leave a Reply

Your email address will not be published. Required fields are marked *