GraphQL VS REST API

What is GraphQL ?

GraphQL API is send only one single request in between the client and server.

What is REST API?

REST API is send multiple requests in between client and server.

Difference in between GraphQL vs REST API?

Here’s a clear, side-by-side look at sample syntax for both REST API and GraphQL, so you can compare how they work in practice.


REST API (Representational State Transfer)

REST uses URLs + HTTP methods (GET, POST, PUT, DELETE).

Example: Get a user
GET /api/users/123

Response

{
  "id": 123,
  "name": "Alice",
  "email": "alice@example.com"
}

Example: Create a user

POST /api/users
Content-Type: application/json
{
  "name": "Bob",
  "email": "bob@example.com"
}

Key characteristics

  • Multiple endpoints (/users, /posts, etc.)

  • Fixed response structure (server decides)

  • Over-fetching or under-fetching is common


GraphQL

GraphQL uses a single endpoint and lets the client specify exactly what it needs.


Example: Query (Get user)

POST /graphql
Content-Type: application/json
{
  "query": "query { user(id: 123) { id name email } }"
}

Response

{
  "data": {
    "user": {
      "id": 123,
      "name": "Alice",
      "email": "alice@example.com"
    }
  }
}

Example: Mutation (Create user)

{
  "query": "mutation { createUser(name: \"Bob\", email: \"bob@example.com\") { id name } }"
}

Key characteristics

  • Single endpoint (/graphql)

  • Client controls response shape

  • Avoids over-fetching

  • Requires schema definition


Direct Comparison

FeatureRESTGraphQL
EndpointsMultipleSingle
Data FetchingFixedFlexible
Over-fetchingCommonAvoided
ComplexitySimplerMore complex

Important Reality Check

GraphQL isn’t “better” in all cases:

  • It adds server complexity

  • Caching is harder than REST

  • Not ideal for simple CRUD APIs

REST remains the better choice for:

  • Simple services

  • Public APIs

  • Systems with heavy HTTP caching

Realtime Example Video:

https://www.youtube.com/shorts/ZFq6leCVQng

Comments

Popular posts from this blog

SQL Basics | Shahul Hameed

DogCat

Blog Machine - Try Hack Me