MongoDB

GraphQL vs REST: API Design Tradeoffs

Compare GraphQL and REST API designs — covering over-fetching, under-fetching, N+1 problems, schema design, and when each excels.

S

srikanthtelkalapally888@gmail.com

GraphQL vs REST

GraphQL and REST represent two different philosophies in API design.

REST

Resource-based URLs:

GET /users/123
GET /users/123/posts
GET /posts/456/comments

Problems:

  • Over-fetching: Gets more data than needed
  • Under-fetching: Needs multiple requests (N+1 problem)

GraphQL

Single endpoint, client specifies exactly what it needs:

query {
  user(id: 123) {
    name
    email
    posts(first: 5) {
      title
      comments(first: 3) {
        text
        author { name }
      }
    }
  }
}

One request, exactly the data needed.

N+1 Problem

REST: Get 10 posts → 10 requests for each post's author
GraphQL: Same, but solved with DataLoader (batching)

Schema

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

type Post {
  id: ID!
  title: String!
  author: User!
}

Strong typing enables code generation and auto-documentation.

Mutations & Subscriptions

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

# Real-time subscription
subscription { newMessage { text author } }

Decision Guide

GraphQL → Complex, nested data; multiple clients; rapid iteration
REST    → Simple CRUD; public APIs; file uploads; HTTP caching

Conclusion

GraphQL shines for complex, multi-entity queries with multiple frontends. REST remains ideal for simple resources and public APIs.

Share this article