GraphQL - A Modern Approach to work with API's.

GraphQL - A Modern Approach to work with API's.

Normally We tend to use the popular REST API's . But What is this GraphQL thing ???

If you are extensively work with API's and use REST for the same, then GraphQL might sound interesting for you. If you are yet to start working with REST, read 3-4 useful lines below , go and study REST but do come back after that to read my article.

REST First !

REST API (Representational State Transfer) is an architectural style for designing networked applications. It relies on a stateless, client-server, cacheable communications protocol -- the HTTP. There are 2 key concepts in REST-

  • Resources: Data is represented as resources, each identified by a unique URL.

  • Endpoints: URLs that clients interact with to perform actions on resources (e.g., /users, /orders).

Most importantly, REST is stateless-

Each request from a client to the server must contain all the information the server needs to understand and process the request. No client context is stored on the server between requests.

Each request is independent, making it easier to handle.

But the problem comes with REST where it has several disadvantages.

  • Over-fetching/Under-fetching Data: Clients might receive too much or too little data, as they cannot specify exactly what they need.

  • Multiple Requests: Fetching complex data often requires multiple requests to different endpoints.

  • Latency and Bandwidth Usage:

    Multiple requests to different endpoints increase latency and bandwidth usage, especially in mobile or low-bandwidth environments.

  • Inconsistent Data:

    Different endpoints might return data in different formats or structures, leading to inconsistencies in how data is consumed by clients.

  • Error Handling Complexity:

    Handling errors can be inconsistent across different endpoints, leading to complexities in managing and debugging issues.

  • State Management Challenges:

    While REST is stateless, managing application state across multiple requests can become complicated, especially in applications with complex user interactions.

Now , There comes a Saviour for the API planet, The GRAPHQL

What is this GraphQL thing...????

GraphQL- Query language for API's

GraphQL is a query language for APIs and a runtime for executing those queries by using a type system you define for your data. Developed by Facebook in 2012 and released publicly in 2015, GraphQL provides a more flexible and efficient alternative to REST.

Why GraphQL Over REST ?

  • Data Fetching:

    • GraphQL: Clients can specify exactly what data they need, reducing the number of requests and preventing over-fetching.

    • REST: Clients often need to make multiple requests to different endpoints to get the required data, leading to over-fetching or under-fetching.

  • Endpoints:

    • GraphQL: Uses a single endpoint to access all data.

    • REST: Uses multiple endpoints for different resources.

  • Schema and Type System:

    • GraphQL: Uses a strongly typed schema, allowing for better validation, introspection, and tooling support.

    • REST: Typically uses JSON or XML without a strict schema, leading to potential inconsistencies.

  • Real-Time Data:

    • GraphQL: Supports subscriptions for real-time data updates.

    • REST: Does not natively support real-time updates; relies on polling or server-sent events.

  • API Evolution:

    • GraphQL: Encourages a versionless API, making it easier to evolve without breaking existing clients.

    • REST: Often relies on versioning to manage changes, leading to maintenance challenges.

  • Error Handling:

    • GraphQL: Provides a consistent way to handle errors, with errors returned in the response alongside partial results.

    • REST: Error handling can be inconsistent across different endpoints.

Example-

// REST API EXAMPLE 
// HTTP Request to the server
GET /api/users
// API response from the server (JSON)
[
    {
        "id": 1,
        "name": "John Doe",
        "email": "john@example.com"
    },
    {
        "id": 2,
        "name": "Jane Smith",
        "email": "jane@example.com"
    }
]
// GraphQL
// GraphQL Query request - ask only what you need!
{
    users {
        id
        name
        email
    }
}
// Response - Nested JSON response containing only requested fields
{
    "data": {
        "users": [
            {
                "id": 1,
                "name": "John Doe",
                "email": "john@example.com"
            },
            {
                "id": 2,
                "name": "Jane Smith",
                "email": "jane@example.com"
            }
        ]
    }
}

You can visit the official documentation of GraphQL for more understanding and implementation.

I will be uploading a separate blog on how to implement GraphQL in basic with apollo server with express and how to use Query to fetch the data from the API's.

Stay Tuned, Thanks for Reading.