Web / API Series

🕸️ Understanding the Difference Between GraphQL and RESTful APIs From a Top 1% Perspective — How Over-Fetching and Under-Fetching Get Resolved

Introduction

This article is part of the Top 1% Series Complete Article Guide, and the fifth article in the Web/API Series.

Prerequisite Knowledge

Getting the Big Picture

Over-Fetching and Under-Fetching: REST’s Two Contrasting Problems

Consider building a “user list screen” with a RESTful API.

graph TB
    subgraph REST["With REST"]
        R1["/users<br/>(a large response with every field)"] -->|over-fetching| R2["Receive everything,<br/>even though you only need the name"]
        R3["/users<br/>(minimal information)"] -->|under-fetching| R4["/users/1/orders<br/>/users/2/orders<br/>...multiple calls"]
    end
    subgraph GraphQL["With GraphQL"]
        G1["Specify exactly the fields<br/>you want, in one request"] --> G2["A response containing exactly what's<br/>needed — name and order status, nothing more"]
    end

Deep Dive Into the Fundamentals

GraphQL’s Idea: the Client Specifies the Exact “Shape of Data” It Wants, as a Query

In GraphQL, there’s fundamentally just one endpoint (/graphql). The client side writes the exact fields and structure it wants, directly into the request, as a query.

query {
  users {
    name
    orders {
      status
    }
  }
}

Send this query, and the server returns a response matching exactly the fields specified in the query — just the name, and the order’s status. Fields you never specified in the query, like email or address, never appear in the response at all. This is how over-fetching gets eliminated.

And you receive information that spans across what would otherwise be entirely separate resources (separate REST endpoints) — “a user’s name” and “that user’s order status” — in a single request, as a single response. This is how under-fetching gets eliminated.

What's Actually Happening on the Server Side in GraphQL?

A GraphQL server parses the query it receives, and calls a function called a resolver, separately, for each field in the query (name, orders, status, and similar). The resolver tied to users fetches the user list from the database, and the resolver tied to the orders nested inside it fetches each user’s order information, and so on. From the client’s view, it’s one request — but internally, on the server side, multiple operations (potentially multiple database queries) are actually executing. That exact gap — “simplicity from the client’s view” versus “complexity inside the server” — is precisely the value GraphQL provides.

GraphQL Has Trade-Offs Too

While GraphQL eliminates over-fetching and under-fetching, it introduces new challenges of its own.

What a Pro Sees Here (Top 1% Understanding)

It’s Not “Which Is Better, REST or GraphQL” — It’s a Difference in “Who Decides What”

The essential difference between REST and GraphQL boils down to one single question: “who decides the response’s shape?” In REST, the server side (the API designer) decides the response’s shape ahead of time, per endpoint. In GraphQL, the client side decides the response’s desired shape, with every single request. When multiple different screens (a web app, a mobile app, and similar) each need a differently shaped set of data, GraphQL offers the strength of handling this without multiplying server-side endpoints. On the other hand, for simple use cases, or where caching efficiency matters most, REST’s URL-based simplicity can be the better fit. This is never a binary “which is better” choice — it’s a requirements-driven decision about who actually needs to decide the response’s shape.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. An expected field is missing from the GraphQL response: Check whether you forgot to explicitly specify that field in the query you sent.
  2. One specific query is extremely slow to respond: Check whether the query is deeply nested, or whether it triggers too many resolver calls.
  3. A GraphQL response gets cached unintentionally, or doesn’t get cached at all: Because GraphQL doesn’t lend itself to simple URL-based caching, you need a dedicated caching strategy (designing a cache key based on the query’s content, and similar).

Summary

Takeaways to Apply Today

  1. When stuck on an API design decision, first ask yourself: “who do I want deciding the response’s shape — the server side or the client side?”
  2. If you adopt GraphQL, consider your caching strategy and query-complexity limits from the earliest design stage.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).