Why GraphQL Beats REST for Modern APIs: Benefits and Code Examples

This article compares REST and GraphQL, highlighting REST's issues like excessive round‑trips, over‑/under‑fetching, and rigidity, and shows how GraphQL solves them with single‑request queries and flexible data selection, while offering guidance on when to choose each approach.

21CTO
21CTO
21CTO
Why GraphQL Beats REST for Modern APIs: Benefits and Code Examples

REST API Known Problems

REST has dominated API design for many years, but it suffers from three main drawbacks: too many server round‑trips, over‑ or under‑fetching of data, and limited flexibility.

Too Many Round‑Trips

Consider a social‑media app that needs to display a list of posts along with each author's name, avatar, and other profile details. With REST, you would first request /api/posts to get the posts, then issue a separate request for each user, such as /api/user/:id, resulting in many HTTP calls per page.

query {
    posts {
        title,
        content,
        tags,
        date,
        user {
            username,
            avatar,
            catchphrase,
            favorite_dog
        }
    }
}

GraphQL solves this by allowing the client to retrieve all required fields in a single request.

Over‑Fetching and Under‑Fetching

REST endpoints return a fixed set of fields regardless of what the client actually needs, leading to unnecessary data transfer or missing information. For example, /api/users/42 might return username, avatar, catchphrase, and favorite dog even if only the username and avatar are required.

// First we get the user's info
GET /api/users/42
{
    "username": "Mr. T",
    "avatar": "http://example.com/users/42/pic.jpg",
    "catchphrase": "I pity the fool",
    "favorite_dog": "beagle"
}

// Then we get their posts
GET /api/users/42/posts
{
    "posts": [{
        "title": "Hello World",
        "content": "Hi everyone!",
        "tags": "first post",
        "date": "July 1, 2020"
    }]
}

With GraphQL the client can request exactly the fields it needs, eliminating over‑fetching.

query {
    users {
        username,
        avatar
    }
}

query {
    users {
        username,
        avatar,
        favorite_dog
    }
}

Lack of Flexibility

REST APIs are often built to match a specific front‑end view. When the UI changes or multiple clients need different data shapes, new endpoints must be created, reducing agility. GraphQL, by contrast, lets each client specify the exact data shape it requires, providing greater flexibility.

REST or GraphQL?

Choosing between the two depends on project requirements:

If you want an easy‑to‑use, client‑driven API, GraphQL is a good fit.

REST has a gentler learning curve for developers already familiar with HTTP semantics.

GraphQL requires explicit error handling on the client side.

REST leverages built‑in HTTP error reporting.

Microservice architectures often align well with REST, keeping concerns separated.

GraphQL’s unified data graph is valuable for aggregating data from multiple sources.

Conclusion

Both REST and GraphQL have strengths and trade‑offs. The decision should be based on the specific needs, team expertise, and resources of your project.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend Developmentapi-designWeb servicesCode ExamplesrestGraphQL
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.