GraphQL is a query language and runtime for APIs that allows clients to request exactly the data they need.
Instead of exposing many fixed endpoints, a GraphQL API typically provides a structured schema that describes available data, fields, relationships, queries, and mutations.
Clients can then build requests around that schema and select specific fields from the response.
Also known as: GraphQL API, GraphQL query language.
GraphQL uses a schema to define the types of data an API provides and the operations clients can perform.
A typical workflow looks like this:
This allows clients to control the shape of the returned data more precisely than many fixed-response API designs.
A query is used to retrieve data.
For example:
query {
product {
name
price
availability
}
}
The server may return:
{
"data": {
"product": {
"name": "Wireless Mouse",
"price": 29.99,
"availability": true
}
}
}
Only the requested fields are included in the response.
A mutation is used to change data.
Mutations may perform actions such as:
Create a record
Update a record
Delete a record
Change an account setting
Submit a form
A mutation is conceptually similar to write operations in other API styles.
The schema defines the structure of a GraphQL API.
It can describe:
For example:
type Product {
id: ID!
name: String!
price: Float
availability: Boolean
}
This schema tells clients which fields are available for a product and what type of value each field returns.
Resolvers are functions that determine how individual fields receive their values.
For example, a resolver may retrieve data from:
GraphQL can therefore act as a unified interface over several backend systems.
GraphQL and REST are both commonly used to build APIs, but they structure data access differently.
REST usually exposes multiple endpoints for different resources.
GraphQL often provides a single API interface where clients specify the fields they need.
In simple terms:
For example, a REST application may use separate resources for products, reviews, and inventory.
A GraphQL query may request selected fields from all three in one operation if the schema connects them.
Over-fetching happens when an API returns more information than the client needs.
For example, a client may only require:
name
price
but a fixed API response may also return:
description
manufacturer
dimensions
reviews
inventory
created_at
updated_at
With GraphQL, the client can request only:
query {
product {
name
price
}
}
This can reduce unnecessary response data.
Under-fetching occurs when a client must make several requests to collect all required information.
For example, a REST-based application may need separate requests for:
Product details
Product reviews
Inventory status
Seller information
A GraphQL schema can allow related information to be retrieved within one query.
This can reduce the number of separate API calls required by the client.
GraphQL APIs often require controls to prevent inefficient or unauthorized requests.
Common protections include:
Because clients can construct flexible queries, servers need to validate not only who is making a request but also how expensive that request may be.
GraphQL is commonly used by modern web applications to retrieve structured data.
A web application may request:
Product information
User profiles
Search results
Reviews
Pricing
Inventory
When working with an authorized GraphQL API, structured queries can provide cleaner data than parsing rendered HTML.
For example, a data workflow may:
Suppose an application needs a product name, price, and category.
A GraphQL request may look like:
query {
product {
name
price
category {
name
}
}
}
The response may be:
{
"data": {
"product": {
"name": "Wireless Mouse",
"price": 29.99,
"category": {
"name": "Computer Accessories"
}
}
}
}
The client receives only the requested fields in a predictable structured format.
GraphQL is a query language and API runtime that lets clients request specific fields from a schema-defined data model.
Compared with fixed-response API styles, it can reduce over-fetching and under-fetching while giving clients more control over returned data. Its flexibility is powerful, but effective GraphQL systems also require careful schema design, resolver performance, and query security.