100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

REST vs GraphQL: What Is the Difference?

Understand REST vs GraphQL — fixed endpoints vs client-specified queries, caching, and the N+1 problem — with examples and interview questions.

mediumQ5 of 224 in Web Development Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

REST exposes fixed, resource-shaped endpoints where the server decides what each response contains, while GraphQL exposes a single endpoint with a typed schema where the client specifies exactly which fields it wants in one query.

In REST, each URL returns a predetermined shape, so fetching a user with their posts and comments often means multiple round trips or an over-fetched, bloated response. GraphQL instead lets the client send a query describing precisely the nested fields it needs, and the server resolves that graph in a single request, avoiding both over-fetching and under-fetching. GraphQL enforces a strongly typed schema with introspection, so tooling can validate queries before they ever hit the network. The tradeoff is that REST benefits from simple HTTP caching and is easier to reason about operationally, while GraphQL needs more careful query-cost analysis, caching strategy, and resolver design to avoid performance pitfalls like the N+1 problem.

  • Client controls exact response shape, cutting over-fetching and under-fetching
  • Single endpoint aggregates data from multiple resources in one request
  • Strongly typed schema enables introspection and strong tooling
  • Fewer round trips for deeply nested or related data

AI Mentor Explanation

REST is like a scoreboard that always shows the full fixed set of stats — runs, wickets, overs — whether you need all of them or not. GraphQL is like asking the scorer directly, "just give me the current run rate and last over," and getting exactly that back in one answer. You skip the extra stats you never wanted and never need a second trip to ask for something the first answer left out. That precise, single-request, client-specified shape is what GraphQL adds over REST endpoints.

Step-by-Step Explanation

  1. Step 1

    Define the schema

    GraphQL requires a typed schema of objects, fields, and relationships upfront.

  2. Step 2

    Client writes the query

    The client specifies exactly which fields and nested relations it needs.

  3. Step 3

    Server resolves fields

    Resolvers fetch each requested field, often from multiple underlying services or tables.

  4. Step 4

    Single response returned

    One JSON payload matches the query shape exactly — no over- or under-fetching.

What Interviewer Expects

  • Clear articulation of over-fetching/under-fetching in REST
  • Understanding of single-endpoint, typed-schema GraphQL model
  • Awareness of tradeoffs: HTTP caching vs query flexibility
  • Mention of N+1 resolver problem and query-cost concerns

Common Mistakes

  • Claiming GraphQL always replaces REST rather than discussing tradeoffs
  • Ignoring caching complexity that comes with a single POST endpoint
  • Forgetting that GraphQL needs schema design and resolver discipline
  • Not mentioning the N+1 query problem in naive resolver implementations

Best Answer (HR Friendly)

REST gives you fixed endpoints that always return the same shape of data, so sometimes you get more than you need or have to make several calls. GraphQL lets the client ask for exactly the fields it wants in one request, which is efficient for complex screens, though it needs more careful setup on the server side.

Code Example

REST: two requests vs GraphQL: one query
// REST: two round trips
const user = await fetch('/api/users/42').then(r => r.json())
const posts = await fetch('/api/users/42/posts').then(r => r.json())

// GraphQL: one request, exact shape
const query = `
  query {
    user(id: 42) {
      name
      posts { title createdAt }
    }
  }
`
const result = await fetch('/graphql', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ query }),
}).then(r => r.json())

Follow-up Questions

  • How does GraphQL handle caching compared to REST?
  • What is the N+1 problem and how do you solve it in GraphQL resolvers?
  • When would you still choose REST over GraphQL?
  • How does GraphQL handle versioning compared to REST?

MCQ Practice

1. What is the main advantage GraphQL offers over REST?

GraphQL lets clients specify precise field selections, avoiding over- and under-fetching.

2. What is a common performance pitfall in naive GraphQL resolvers?

Resolving nested fields one at a time can trigger many redundant database queries.

3. Why is HTTP caching harder with GraphQL than REST?

A single endpoint with varying query bodies defeats simple URL-based HTTP caching.

Flash Cards

REST response shape?Fixed per endpoint, decided by the server.

GraphQL response shape?Defined by the client query, resolved from a typed schema.

Main GraphQL pitfall?The N+1 problem in naive field resolvers.

Main REST advantage?Simple, well-understood HTTP caching per URL.

1 / 4

Continue Learning