REST API vs. GraphQL API

Piyush Kumar
3 min readMar 21, 2023

REST API and GraphQL are both popular approaches for building web APIs, but there are some key differences between them.

What is REST API?

A REST API is the concept of having multiple endpoints which means multiple URLs are exposed by our web service to be precise URLs or paths on that domain where we host that web service that react to different http methods such as GET, POST, PUT, and DELETE. Each endpoint represents a resource in the API and allows clients to perform specific operations.

REST API challenges

  1. Over-fetching

Over-fetching occurs when a client receives unnecessary data beyond the specific subset they requested.

EX: Suppose we have one application which have data of client profile and we only have /employee/:id endpoint available to fetch information and we only need employee name and email address. To fetch information, we have to make an HTTP GET request.

`GET https://example.com/employee/1`

The server would then return the information in the body of the response, typically in JSON or XML format:

{
"id": 1,
"name": "Piyush Kr",
"email": "piyushkumar@xyz.com",
"occupation": "DevOps Er.",
"status": "Active",
}

Here, we are getting unwanted data also, this is over-fetching.

2. Under-fetching

Under-fetching occurs when an API endpoint does not provide enough data to fulfil a client’s request.

EX: Suppose with employee name and email address we also need employee’s team members information. We still have the /employee/:id endpoint available, but it’s now improved to only return the following:

{
"id": 1,
"name": "Piyush Kr",
"email": "piyushkumar@xyz.com",
"occupation": "DevOps Er.",
"status": "Active",
"teams": [2,3]
}

Here we can see in teams field contains a list of employee IDs. So this is under-fetching . However, because we need the information of each team, we now need to make the following separate requests to obtain this information:

`GET https://example.com/employee/2`

`GET https://example.com/employee/3`

What is GraphQL?

GraphQL is a query language for API works with one endpoint only which always reached with a POST request. The endpoint accepts GraphQL queries in the request body and returns a JSON response with the requested data. The use of a single endpoint and the POST method in GraphQL allows for a more efficient and flexible API design.

It provides a schema of the data in the API and gives clients the power to ask for exactly what they need.

EX: Suppose we want name and email address of employee having ID 1. Below is query to request specific employee information.

Query

query {
employee(id: 1) {
id
name
email
}
}

Output

{
"data": {
"employee": {
"id": "1",
"name": "Piyush Kr",
"email": "piyushkumar@xyz.com"
}
}
}

[Solved over-fetching issue] ✅

Query

query {
employee(id: 1) {
id
name
email
teams {
id
name
email
}
}
}

Output

{
"data": {
"employee": {
"id": 1,
"name": "Piyush Kr",
"email": "piyushkumar@xyz.com",
"teams": [
{
"id": 2,
"name": "xyz",
"email": "xyz@xyz.com"
},
{
"id": 3,
"name": "abc",
"email": "abc@abc.com"
}
]
}
}
}

[Solved under-fetching issue] ✅

Difference between REST API and GraphQL API

--

--