GraphQL is a query language for APIs that lets clients request only the required data, making APIs more efficient than traditional approaches.
- Enables precise data fetching, avoiding over-fetching and under-fetching
- Gives clients full control over queries with a flexible and structured schema
- Reduces API complexity by using a single endpoint instead of multiple endpoints.
1. Fast Development
GraphQL is client-driven, meaning the client controls what data to request and receives only the required information, enabling efficient updates to the interface.
- Client defines GraphQL queries to get the exact data.
- Then, GraphQL resolvers resolves query and gather or fetch the required data from sources.
- Here, Over-fetching and under-fetching are minimized because client is getting only the required data.
2. Contract Based Schema
A contract-based schema in GraphQL acts as a blueprint and agreement between the client and server, defining what data is available and how it can be requested.
- Defines data structure and allowed operations through a schema.
- Acts as a contract between client and server for predictable communication.
- Helps developers understand available queries and data clearly.
Example of contract based schema:
type book {
id: ID!
title: String!
author: User!
}
type ReaderID {
id: ID!
name: String!
email: String!
}
type Query {
getAllBooks: [book!]!
getBookById(id: ID!): book!
}
- Book: Represents a book with id, title, and author.
- ReaderID: Represents a reader with id, name, and email.
- Query: Defines operations to fetch data (getAllBooks, getBookById).
3. No Under-Fetching and Over-Fetching
GraphQL eliminates under-fetching and over-fetching problems by allowing clients to request exactly the data they need using queries defined by a contract-based schema.
- The client specifies what data is required.
- The server returns only that data.
- No unnecessary or missing information.
Example: Fetch the user’s name, the titles of their articles, and the names of the last two followers.
query {
user(id: "1") {
name
articles {
title
}
followers(last: 2) {
name
}
}
}
Output:
{
"data": {
"user": {
"name": "Alen",
"articles": [
{ "title": "Learn GraphQL" },
{ "title": "API Design" }
],
"followers": [
{ "name": "A" },
{ "name": "B" }
]
}
}
}
4. Real-Time Data With Subscription
GraphQL subscriptions enable real-time data updates, allowing clients to receive data instantly when specific events occur on the server. This is especially useful for applications like chat apps or live notifications.
Subscription
A Subscription is a special GraphQL type that provides a stream of data instead of a single response.
- Defined in the schema along with Query and Mutation.
- Sends updates automatically when events occur.
- Helps build real-time, high-performance applications.
type Subscription {
currentUpdate(Id: ID!): MessageAlert
}
Data Fetching in REST Vs GraphQL
GraphQL improves upon REST by solving issues like inflexibility, over-fetching, and under-fetching, enabling efficient data retrieval and better overall performance.
Fetching of Data Using REST
Data retrieval in REST is done through multiple resource-specific endpoints, such as /users/{id} for user data, with additional endpoints required for related resources, resulting in multiple API calls.

A secondary endpoint /users/{id}/articles is used to retrieve the articles associated with a specific user, representing a related resource in REST.

A third endpoint /users/{id}/followers is used to retrieve the follower data associated with the specified user, representing another related resource in REST.

Fetching of Data Using GraphQL
Data retrieval in GraphQL is performed through a single endpoint, where the client specifies exactly what data is needed in a query and receives a precise, structured response.
