Back to all posts
Programming

Introduction to GraphQL: A Modern Alternative to REST

By Huzi

The Problem with Traditional REST APIs

For years, REST has been the standard for building APIs. It's a powerful and flexible architectural style, but it has some common pain points, especially as applications become more complex:

  1. Over-fetching: This happens when a client downloads more data than it actually needs. For example, a /users/:id endpoint in a REST API might return a user object with 20 fields, but the client only needs to display the user's name and email. The other 18 fields are wasted bandwidth.

  2. Under-fetching: This is the opposite problem. To get all the data it needs, a client has to make multiple requests to different endpoints. For example, to get a user's details and their last 5 blog posts, a client might have to hit /users/:id and then /users/:id/posts. This leads to multiple network round trips, which can be slow, especially on mobile devices.

What is GraphQL?

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. It was developed by Facebook in 2012 and open-sourced in 2015. GraphQL isn't tied to any specific database or storage engine; instead, it's backed by your existing code and data.

The key idea behind GraphQL is to give clients the power to ask for exactly what they need, and nothing more.

How GraphQL Solves the Problem

With GraphQL, the client specifies the exact shape of the data it requires in a single request. The server then responds with a JSON object that mirrors the structure of the query.

Let's look at an example. Imagine we want to get a user's name and the titles of their last three posts.

A GraphQL Query:

query {
  user(id: "1") {
    name
    posts(last: 3) {
      title
    }
  }
}

The JSON Response:

{
  "data": {
    "user": {
      "name": "Alice",
      "posts": [
        { "title": "My First Post" },
        { "title": "Learning GraphQL" },
        { "title": "Advanced Topics" }
      ]
    }
  }
}

In a single request, we got exactly the data we needed, with no over-fetching or under-fetching.

Core Concepts of GraphQL

  1. Schema Definition Language (SDL): A GraphQL API is organized in terms of types and fields, not endpoints. You define the capabilities of your API in a schema using the SDL. This schema serves as a contract between the client and the server.

    type User {
      id: ID!
      name: String
      email: String
      posts: [Post]
    }
    
    type Post {
      id: ID!
      title: String
      content: String
      author: User
    }
    
    type Query {
      user(id: ID!): User
      posts: [Post]
    }
    
  2. Queries: Used for fetching data (read operations). This is what we saw in the example above.

  3. Mutations: Used for modifying data (write operations like create, update, delete). Mutations are structured just like queries but use the mutation keyword.

    mutation {
      createPost(title: "New Post Title", content: "...") {
        id
        title
      }
    }
    
  4. Resolvers: On the server, resolvers are functions that are responsible for fetching the data for a specific field in your schema. When a query comes in, the GraphQL server calls the appropriate resolvers to construct the response.

GraphQL vs. REST: A Summary

Feature REST GraphQL
Data Fetching Client is dependent on server-defined endpoints. Client specifies the exact data it needs.
Endpoints Multiple endpoints for different resources. Typically a single endpoint (/graphql).
Over/Under-fetching A common problem. Solved by design.
API Versioning Often requires versioning (e.g., /v2/users). Can evolve without versioning by adding new fields.
Schema/Typing No built-in schema. Often uses OpenAPI/Swagger. Strongly typed with a built-in schema (SDL).

Conclusion

GraphQL is not a replacement for REST, but it is a powerful alternative that addresses many of REST's limitations, particularly for complex applications with evolving data requirements. By giving control to the client, it enables more efficient data fetching, faster development cycles, and more resilient APIs. If you're building a modern application, especially with a component-based frontend framework like React, GraphQL is definitely worth considering.


You Might Also Like


Related Posts