Introduction
GraphQL is a query language for APIs and a runtime for executing those queries. It was developed by Facebook to make data fetching more efficient and flexible compared to traditional REST APIs.
Instead of using multiple endpoints, GraphQL commonly uses a single endpoint where clients can specify exactly what data they need. This helps avoid problems such as over-fetching, where an API returns more data than required, and under-fetching, where the client does not receive enough data and needs additional requests.
Key Features of GraphQL
GraphQL provides several features that make it useful for modern applications.
Single Endpoint
GraphQL commonly exposes a single endpoint through which clients send their queries.
With REST, an application might use multiple endpoints such as:
/users
/users/1
/users/1/posts
With GraphQL, related data can be requested through a single query.
Strongly Typed Schema
GraphQL uses a schema to define the available types, fields, relationships, queries, mutations, and other operations supported by the API.
The schema provides a clear contract between the client and server.
Precise Queries
Clients specify the fields they want in the query.
This allows the response to closely match the structure requested by the client.
Mutations
Mutations are used when the client needs to modify data.
Typical operations include:
Creating data
Updating data
Deleting data
Subscriptions
Subscriptions can be used for real-time updates. A client can subscribe to events and receive new data when relevant changes occur.
GraphQL Query Example
Suppose an application has users, posts, comments, and authors.
With REST, retrieving all of this related information might require multiple API requests.
GraphQL allows the client to request the required nested data in a single query.
query {
user(id: "1") {
name
email
posts {
title
comments {
text
author {
name
}
}
}
}
}
The query requests:
The user's
nameThe user's
emailThe user's posts
The title of each post
Comments associated with each post
The name of each comment author
GraphQL Response
The server returns data that follows the structure requested by the query.
{
"data": {
"user": {
"name": "Alice",
"email": "[email protected]",
"posts": [
{
"title": "GraphQL Basics",
"comments": [
{
"text": "Great post!",
"author": {
"name": "Bob"
}
}
]
}
]
}
}
}
The response contains the requested name, email, posts, comments, and author name.
This is one of the important differences between GraphQL and traditional REST APIs: the client defines the shape of the requested data.
Over-Fetching and Under-Fetching
One of the common reasons developers consider GraphQL is to have more control over data fetching.
Over-Fetching
Over-fetching occurs when an API returns fields that the client does not need.
For example, a REST endpoint might return an entire user object when a mobile screen only needs the user's name.
With GraphQL, the client can request only:
{
user(id: "1") {
name
}
}
The response can then contain only the requested field:
{
"data": {
"user": {
"name": "Alice"
}
}
}

Join the conversation! Your thoughts help the community grow.