GraphQL vs REST

Ravi Tiwari
4 min readApr 28, 2023

--

A Comparative Analysis with Example

GraphQL vs REST

Introduction

In the world of web development, two prominent technologies have emerged for managing API communication: GraphQL and REST. Both are designed to handle data transfer between clients and servers, but they differ in structure, flexibility, and performance. In this article, we will compare GraphQL and REST using examples to help you determine which technology is best suited for your needs.

GraphQL Overview

GraphQL, developed by Facebook in 2015, is a query language and runtime for APIs. It enables clients to request only the specific data they need, reducing the amount of data sent over the network. GraphQL’s flexible structure allows for nested queries, real-time updates through subscriptions, and easy handling of complex data relationships.

REST Overview

Representational State Transfer (REST) is an architectural style introduced by Roy Fielding in 2000. It uses standard HTTP methods (GET, POST, PUT, DELETE) to perform operations on resources identified by URLs. RESTful APIs are stateless, cacheable, and follow a uniform interface, making them easy to understand and consume.

Comparison: GraphQL vs REST

Flexibility and Query Structure

GraphQL provides a more flexible query structure than REST. In REST, you often need to make multiple API calls to retrieve or update related data. On the other hand, GraphQL allows you to request nested data in a single query, reducing the number of round-trips to the server.

Example:

Suppose we have an e-commerce application with the following data models:

  • User
  • Product
  • Order

In REST, to fetch a user’s details, their orders, and the products in each order, you might need to make three separate API calls:

GET /users/:userId
GET /users/:userId/orders
GET /orders/:orderId/products

With GraphQL, you can fetch the same data in one query:

query {
user(id: "userId") {
id
name
email
orders {
id
date
products {
id
name
price
}
}
}
}

Over-fetching and Under-fetching

REST is prone to over-fetching and under-fetching. Over-fetching occurs when the server sends more data than the client needs, while under-fetching requires multiple API calls to gather the required information. GraphQL solves both issues by allowing clients to request exactly the data they need.

Versioning

REST APIs typically use versioning to manage changes, resulting in multiple API versions that clients must accommodate. GraphQL eliminates the need for versioning by allowing clients to request only the fields they need, enabling the API to evolve without breaking existing applications.

Real-time Updates

GraphQL supports real-time updates through subscriptions, a feature that REST does not offer natively. With GraphQL subscriptions, clients can receive live updates from the server whenever data changes, making it ideal for real-time applications.

Example

To illustrate the differences between GraphQL and REST, let’s consider a complex e-commerce application that requires searching for products, adding products to a cart, and checking out.

REST Example:

// Search for products
GET /products?search=smartphone

// Add a product to the cart
POST /carts/:cartId/products
{
"productId": "productId",
"quantity": 2
}

// Checkout
POST /orders
{
"userId": "userId",
"cartId": "cartId",
"paymentMethod": "credit_card",
"shippingAddress": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001",
}

// Get the order details
GET /orders/:orderId

GraphQL Example:

# Search for products
query {
searchProducts(query: "smartphone") {
id
name
price
}
}

# Add a product to the cart
mutation {
addToCart(cartId: "cartId", productId: "productId", quantity: 2) {
id
products {
id
name
quantity
}
}
}

# Checkout
mutation {
createOrder(
userId: "userId",
cartId: "cartId",
paymentMethod: "credit_card",
shippingAddress: {
street: "123 Main St",
city: "New York",
state: "NY",
zip: "10001"
}
) {
id
status
total
}
}

# Get the order details
query {
order(id: "orderId") {
id
status
total
products {
id
name
price
}
}
}

As shown in the example above, the GraphQL approach is more concise and flexible, allowing clients to request only the necessary data in a single query. In contrast, the REST approach requires multiple API calls and often provides more data than needed, leading to over-fetching.

Conclusion

Both GraphQL and REST have their merits and use cases. GraphQL offers greater flexibility, efficiency, and real-time capabilities, making it well-suited for complex, data-driven applications. On the other hand, REST is a simpler, more established architecture that’s easy to understand and consume.

When choosing between GraphQL and REST, consider your application’s requirements, team familiarity with each technology, and the complexity of your data. It’s also possible to use a hybrid approach, combining GraphQL and REST to leverage the strengths of both technologies.

--

--

Ravi Tiwari

Experienced hands-on CTO with 20+ years of cloud native microservices expertise, driving solutions for large-scale enterprises.