Huzi Blogs
Blog
Blog
Disclaimer & Data Privacy Policy
Project by huzi.pk

© 2026 blogs.huzi.pk. All Rights Reserved.

    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.

    Advertisements


    You Might Also Like

    Luxury Heavy Embroidered Velvet Suit | 4-Side Border Chiffon Dupatta

    Luxury Heavy Embroidered Velvet Suit | 4-Side Border Chiffon Dupatta

    PKR 9500

    Arabic Aura Watch for Boys – Fiber Chain, Arabic Dial, Light 44 g

    Arabic Aura Watch for Boys – Fiber Chain, Arabic Dial, Light 44 g

    PKR 1699

    Luxury Handwork Heavy Embroidered Organza Suit | Emb. Shamoz Silk Trouser

    Luxury Handwork Heavy Embroidered Organza Suit | Emb. Shamoz Silk Trouser

    PKR 7750

    Heavy Embroidered Velvet Party Wear Suit 2026 | Emb. Organza Dupatta & Silk Trouser

    Heavy Embroidered Velvet Party Wear Suit 2026 | Emb. Organza Dupatta & Silk Trouser

    PKR 8700

    Luxury Heavy Embroidered Velvet Wedding Suit 2026 | Emb. Silk Trouser & Organza Print Dupatta

    Luxury Heavy Embroidered Velvet Wedding Suit 2026 | Emb. Silk Trouser & Organza Print Dupatta

    PKR 9300

    Advertisements


    Related Posts

    Programming
    Building a REST API with Node.js and Express
    Learn how to build a robust and scalable REST API from scratch using Node.js and the Express framework. This guide covers routing, middleware, and connecting to a database.

    By Huzi

    Read More
    Programming
    C# Essential Guide to Modern Programming Techniques
    C# is a versatile programming language from Microsoft, running on the .NET platform. It supports multiple programming styles and is used to build a wide range of applications, including web, desktop, mobile, and games. It is known for its clear syntax and strong typing.

    By Huzi

    Read More
    Programming
    The Ultimate Beginner's Guide to Building a Website with Cloudflare Pages
    This guide provides a comprehensive walkthrough for deploying a modern website on Cloudflare Pages, covering everything from Git setup and local development to serverless functions, security, and monitoring.

    By Huzi

    Read More
    Education
    HEC Scholarships Application Process for Pakistani Students (2025 Guide)
    A complete guide on how to apply for HEC scholarships in 2025. Learn about overseas and local scholarships, the step-by-step application process, and tips to increase your chances.

    By Huzi

    Read More
    Legal
    Comprehensive Guide: Filing Police Misconduct Complaints in Pakistan (2025)
    In 2025, your complaint receipt is your shield—wield it against the 'thana culture'.

    By Huzi

    Read More
    Sports
    The Agony and the Ecstasy: A Bittersweet Night in Denver (Broncos vs Bills)
    The Denver Broncos advanced to the AFC Championship, but a season-ending injury to Bo Nix turns their 33-30 win over the Bills into a hollow triumph.

    By Huzi

    Read More