Backend

GraphQL vs REST: Which API Style Should You Use?

image 13 GraphQL vs REST: Which API Style Should You Use?
On this page
  1. The same data in both styles
  2. REST
  3. GraphQL
  4. Side-by-side comparison
  5. Where GraphQL shines
  6. Where REST is the better choice
  7. Problems GraphQL brings with it
  8. A simple rule
  9. Building the same feature both ways in Node.js
  10. REST with Express and Mongoose
  11. GraphQL with Apollo Server
  12. Caching: the biggest practical difference
  13. Versioning and changing the API
  14. Security considerations
  15. A decision checklist
  16. Other options worth knowing: tRPC and BFFs
  17. Frequently asked questions
  18. Oh hi there 👋It’s nice to meet you.
  19. Sign up to receive awesome content in your inbox.

REST and GraphQL are two ways to design how clients talk to your server. Both work well, and big companies run each at huge scale. The right choice depends on who uses your API and what kind of data they need. Let’s compare them with the same example, then end with a simple rule.

The same data in both styles

A React profile page needs a user’s name, their last three posts, and the number of comments on each post.

REST

GET /api/users/42
GET /api/users/42/posts?limit=3
GET /api/posts/101/comments/count
GET /api/posts/102/comments/count
GET /api/posts/103/comments/count

Five requests, and each response may include fields the page never shows. You can fix this by adding a custom endpoint such as GET /api/users/42/profile-summary, which is exactly what many REST teams do.

GraphQL

query Profile {
  user(id: "42") {
    name
    posts(limit: 3) {
      title
      commentCount
    }
  }
}

One request to a single endpoint (usually POST /graphql), and the response contains exactly these fields, in this shape.

Side-by-side comparison

REST GraphQL
Endpoints Many, one per resource Usually one
Who decides the response shape The server The client, within the schema
Over-fetching and under-fetching Common without custom endpoints Rare
HTTP caching (CDN, browser) Works naturally with GET requests Needs extra work, such as persisted queries
Errors HTTP status codes (404, 401, 500) Often a 200 response with an errors array
Types and docs Optional, through OpenAPI Built in: the schema is the contract
File uploads Simple (multipart forms) Awkward; often handled by a separate REST endpoint
Learning curve Low Higher on the server side

Where GraphQL shines

  • Many different clients. A web app, a mobile app and partner integrations each fetch exactly what they need, without the backend team writing a new endpoint for every screen.
  • Deeply related data. Users, posts, comments and authors are naturally a graph, and one query can walk it.
  • Fast-moving frontends. Frontend developers can add a field to a query without waiting for a backend change, as long as it exists in the schema.

Where REST is the better choice

  • Simple CRUD APIs, which describes most MERN projects.
  • Public APIs, where HTTP caching and familiar conventions matter.
  • Small teams that don’t want to run schema tooling, resolvers and query-cost limits.
  • File-heavy APIs with uploads and downloads.

Problems GraphQL brings with it

The N+1 query problem. A naive resolver for posts { author { name } } runs one database query per post. Use a batching tool such as DataLoader so the authors load in one query.

Expensive queries. Clients can send deeply nested queries that overload your database. Limit query depth and complexity, and consider allowing only pre-approved (persisted) queries in production.

Monitoring. Every request hits /graphql, so normal per-URL dashboards tell you little. You need operation-level metrics.

A simple rule

Start with REST. Move to GraphQL, or add it alongside REST, when you notice you are building a new endpoint for every screen, or when several clients need different shapes of the same related data. Many teams run both: REST for simple and public endpoints, GraphQL for complex app screens.

Designing a REST API? Read REST vs RESTful: the rules that matter.

Building the same feature both ways in Node.js

Seeing a small server in each style makes the difference concrete. Here is a list of products with their seller’s name.

REST with Express and Mongoose

app.get('/api/products', async (req, res) => {
  const products = await Product.find({ status: 'active' })
    .select('title price seller')
    .populate('seller', 'name')
    .limit(20);
  res.json(products);
});

The server decides the shape. If the mobile app also needs each seller’s rating, you either add it here for everyone or create another endpoint.

GraphQL with Apollo Server

const typeDefs = `#graphql
  type Seller { id: ID!, name: String!, rating: Float }
  type Product { id: ID!, title: String!, price: Int!, seller: Seller! }
  type Query { products(limit: Int = 20): [Product!]! }
`;

const resolvers = {
  Query: {
    products: (_, { limit }) => Product.find({ status: 'active' }).limit(Math.min(limit, 100)),
  },
  Product: {
    seller: (product, _, { loaders }) => loaders.seller.load(product.seller.toString()),
  },
};

The web app asks for title price seller { name } and the mobile app asks for title seller { name rating }, from the same server and with no extra endpoint. The loaders.seller part is DataLoader, which collects every seller id needed for one request and loads them in a single query instead of one per product.

Caching: the biggest practical difference

With REST, every resource has its own URL, and HTTP caching works for free. A CDN can cache GET /api/products?category=phones for 60 seconds, browsers respect Cache-Control headers, and you can see in your logs which URLs are popular.

GraphQL usually sends every query as a POST to /graphql, so CDNs and browsers can’t cache responses by URL. GraphQL teams solve this in other ways: client-side caches such as Apollo Client’s normalized cache, which reuses objects across queries; persisted queries, which turn known queries into short GET requests with an id; and server-side caching of resolver results. All of these work, but they are extra moving parts that REST doesn’t need.

For public, read-heavy content, such as a product catalog or blog, this alone is often a good reason to stay with REST.

Versioning and changing the API

REST APIs usually handle breaking changes with versions in the URL, such as /api/v1 and /api/v2, and keep the old version running until clients move.

GraphQL takes a different approach: you rarely version the whole API. Because clients ask only for the fields they use, you can add new fields freely, mark old ones with @deprecated(reason: "Use priceInPaise"), watch your logs until no client requests them, and then remove them. This works very well when you control all the clients. For public APIs used by unknown third parties, it requires good monitoring and communication.

Security considerations

Both styles need authentication, authorization and input validation. GraphQL adds a few concerns of its own:

  • Query depth and cost. A malicious client can request deeply nested data, such as products, their sellers, those sellers’ products and so on, to overload your server. Set a maximum depth and a cost limit.
  • Field-level permissions. In REST you protect an endpoint. In GraphQL, any query can reach any field, so sensitive fields such as a user’s email need checks in their resolvers.
  • Introspection in production. The schema can describe itself, which is great for tools but reveals your whole data model. Many teams turn introspection off in production for private APIs.
  • Batching attacks. Several operations can be sent in one request, which can bypass simple per-request rate limits on login mutations. Rate-limit by operation, not just by request.

A decision checklist

Answer these questions about your project:

  1. Do several different clients need different shapes of the same data? If yes, that points to GraphQL.
  2. Is most traffic public, cacheable reads? That points to REST.
  3. Is your data deeply connected, like a social graph? That points to GraphQL.
  4. Is your team small and new to GraphQL, with a deadline? That points to REST.
  5. Will outside developers use your API? That leans towards REST, which is more familiar to them.

If the answers are mixed, start with REST and add a GraphQL layer later for the screens that need it. Both can call the same Mongoose models and services, so nothing is wasted.

Other options worth knowing: tRPC and BFFs

REST and GraphQL aren’t the only choices for a JavaScript team. Two other approaches come up often in MERN and Next.js projects.

tRPC lets a TypeScript frontend call server functions directly with full type safety and no schema files. Change a function’s input on the server, and the React code that calls it shows a type error immediately. It is excellent when the same team owns the frontend and the backend in one TypeScript codebase, but it isn’t meant for public APIs or for clients written in other languages.

A backend for frontend (BFF) is a thin server, often a set of Next.js route handlers or a small Express app, that sits between one frontend and your real services. It reshapes REST responses into exactly what each screen needs. This gives you much of GraphQL’s convenience without running a GraphQL server, at the cost of maintaining one more layer.

Frequently asked questions

Is GraphQL faster than REST?

Not automatically. GraphQL can reduce the number of requests and the size of responses, but a poorly designed resolver can be slower than a simple REST endpoint. Speed depends on your database queries.

Can I use GraphQL with MongoDB and Express?

Yes. Libraries such as Apollo Server or GraphQL Yoga run on Node.js, and resolvers can call Mongoose models like any other code.

Will GraphQL replace REST?

No. Both are widely used and solve different problems. REST remains the default for most APIs.

Does GraphQL work with React?

Very well. Apollo Client, urql and Relay are popular GraphQL clients for React, with caching and loading states built in. For REST, TanStack Query gives similar caching and loading features.

logo

Oh hi there đź‘‹
It’s nice to meet you.

Sign up to receive awesome content in your inbox.

We don’t spam! Read our privacy policy for more info.

Saurabh

Saurabh Pathak is an AI engineer and MERN stack developer, and the founder of mernstackdev.com. He builds open-source projects such as the Haat gateway and MCP safety tooling, and he writes hands-on tutorials on React, Node.js, MongoDB and AI agents. He cares most about code that works in production.