GraphQL
- Pronunciation
- GRAF-kyoo-EL
In short
GraphQL is a query language and runtime for APIs that lets clients request exactly the data they need, often from a single endpoint in a single request.
What is GraphQL?
GraphQL is a way to build and use APIs in which the client writes a query describing the exact shape of the data it wants, and the server returns JSON in that same shape. It was created at Facebook in 2012, released as open source in 2015, and is now maintained by the GraphQL Foundation.
Every GraphQL API is built around a schema, a typed description of all the data and operations it offers. Clients send queries to read data, mutations to change it, and subscriptions to receive real-time updates. On the server, small functions called resolvers fetch the value for each field from a database, another API, or any other source.
A helpful analogy is a buffet versus a set menu. A REST endpoint is like a set menu that always serves the same plate, while GraphQL lets you choose exactly which dishes go on your plate. This is especially useful for mobile apps and complex user interfaces that need data from many related objects at once.
GraphQL is not a database and does not replace SQL; it sits in front of your data sources as an API layer. Compared with REST, it avoids over-fetching (receiving fields you don't need) and under-fetching (needing several requests to get everything), but it makes HTTP caching and rate limiting harder because most requests go to one endpoint.
At a glance
Key takeaways
- Clients specify exactly which fields they want in the response.
- A typed schema describes all available data and operations.
- Queries read data, mutations change it, and subscriptions stream updates.
- Most GraphQL APIs expose a single endpoint, often
/graphql. - GraphQL is an API layer, not a database.
Example
# Ask for a user's name and the titles of their 3 latest posts
query {
user(id: "42") {
name
posts(last: 3) {
title
}
}
}
# The response is JSON with exactly the same shape:
# { "data": { "user": { "name": "Ada", "posts": [{ "title": "..." }] } } }Readers ask
Is GraphQL better than REST?
Neither is better in every case. GraphQL shines when clients need flexible, nested data from many sources, while REST is simpler to build, cache, and monitor for straightforward resources.
Is GraphQL a database?
No. GraphQL is a query language for APIs; the server's resolvers fetch the actual data from databases, other services, or files behind the scenes.
Does GraphQL use HTTP?
Usually, yes. Most GraphQL APIs receive queries as HTTP POST requests to a single endpoint, although the specification itself does not require a particular transport.
Often compared
See also
- APIBackend & APIs, p. 2An API is a set of rules that lets one piece of software request data or actions from another in a predictable, documented way.
- REST APIBackend & APIs, p. 38A REST API is a web API that exposes data as resources identified by URLs and lets clients read or change them using standard HTTP methods.
- EndpointBackend & APIs, p. 12An endpoint is a specific URL, combined with an HTTP method, where an API receives requests and returns responses for one particular resource or action.
- JSONBackend & APIs, p. 25JSON is a lightweight, text-based format for storing and exchanging structured data as key-value pairs and lists, readable by both humans and machines.
- HTTPWeb Development, p. 19HTTP is the protocol that browsers, apps, and servers use to exchange web pages and data through a simple cycle of requests and responses.
- gRPCBackend & APIs, p. 20gRPC is an open-source framework for calling functions on a remote server as if they were local, using Protocol Buffers and HTTP/2 for fast, typed messages.
- Backend for FrontendSoftware Architecture, p. 2Backend for frontend is an architecture pattern in which each kind of client, such as a web or mobile app, gets its own small backend tailored to its needs.
Sources
Spotted a mistake or something missing on this page?Suggest an edit