What is GraphQL? It is a query language for APIs and a runtime that executes those queries against your data. Instead of many fixed endpoints that each return a predefined response, a GraphQL API exposes a single endpoint and a typed schema, and clients ask for exactly the fields they need in one request. Created at Facebook in 2012 and open sourced in 2015, GraphQL is widely used for mobile and web applications, and for combining data from many backend services. This guide explains how GraphQL works, how it compares with REST, its benefits and risks, how to design and secure a GraphQL API and when it is the right choice.
Short answer: GraphQL lets clients specify the exact data they want, in a single request, from a strongly typed schema. It reduces over fetching and under fetching and speeds front end development, but it needs careful design for performance, caching and security.
What is GraphQL?
The GraphQL article on Wikipedia describes a data query and manipulation language for APIs. It does not replace your databases. It is a layer that sits in front of them and other services, often behind an API gateway, and it is one of several styles available when you invest in API development.
How GraphQL works

- Define the schema. The schema describes types, fields and relationships, such as Customer with name and orders.
- The client sends a query. It lists exactly the fields wanted, for example a customer name and the dates of their last three orders.
- The server validates the query. It checks the query against the schema and rejects invalid or too expensive requests.
- Resolvers fetch data. Functions called resolvers retrieve each field from databases, services or other APIs.
- The response mirrors the query. The JSON returned has the same shape as the query, with only the requested fields.
- Mutations change data. Writes use mutations, which are defined in the schema like queries.
- Subscriptions deliver updates. Clients can subscribe to events and receive changes in real time.
GraphQL operations
| Operation | Purpose | Example |
|---|---|---|
| Query | Read data | Get a customer and their recent orders |
| Mutation | Create, update or delete data | Create an order |
| Subscription | Receive live updates | Notify when order status changes |
| Fragment | Reuse a set of fields | Common customer fields across queries |
| Variable | Pass parameters safely | Customer id as input |
| Directive | Modify execution | Skip a field conditionally |
GraphQL compared with REST
| Aspect | REST | GraphQL |
|---|---|---|
| Endpoints | Many, one per resource | One endpoint |
| Response shape | Fixed by the server | Chosen by the client |
| Over and under fetching | Common | Avoided |
| Requests for related data | Often several | Usually one |
| Caching | Simple with HTTP caching | More complex, needs query aware caching |
| Versioning | Version in URL or headers | Evolve schema by adding fields and deprecating |
| Typing and documentation | Via OpenAPI specifications | Built in, introspectable schema |
| Learning curve | Familiar to most developers | Needs new concepts and tools |
Neither is universally better. REST remains excellent for simple, resource oriented and cacheable APIs, while GraphQL shines where clients have varied data needs, such as mobile apps and complex dashboards.
Benefits of GraphQL
- Clients receive exactly what they ask for, saving bandwidth on mobile networks.
- Fewer round trips, because related data comes in one request.
- A strongly typed schema acts as living documentation and enables tooling and validation.
- Front end and back end teams can work in parallel against the schema.
- APIs evolve without version numbers, by adding fields and deprecating old ones.
- A GraphQL layer can unify several backend services behind one interface.
Risks and how to manage them
| Risk | Why it happens | Mitigation |
|---|---|---|
| Expensive queries | Clients can request deeply nested data | Depth and complexity limits, timeouts, persisted queries |
| N+1 queries | Resolvers fetch related items one at a time | Batching tools such as dataloaders |
| Caching difficulty | One endpoint and POST requests | Persisted queries, CDN aware caching, response caching by field |
| Authorisation gaps | Fields reachable through many paths | Enforce permissions at field and object level. See what is OAuth |
| Introspection exposure | Schema is discoverable | Restrict or disable introspection in production where appropriate |
| Schema sprawl | Many teams edit one schema | Federation and clear ownership |
How to design and launch a GraphQL API
- Start from client needs. List the screens and workflows and the data each requires.
- Design the schema carefully. Use domain language, clear types and consistent naming, and think about nullability.
- Choose the architecture. A single server, a gateway over existing REST services or a federated graph owned by several teams.
- Implement resolvers efficiently. Batch and cache data access to avoid repeated queries.
- Secure it. Authenticate every request, authorise by field, limit depth and cost and log operations.
- Add tooling and tests. Use schema checks, contract tests and performance tests in the pipeline. See what is CI/CD.
- Monitor and evolve. Track slow fields and unused fields, deprecate gently and communicate changes.
Common GraphQL mistakes
- Exposing the database structure directly as the schema instead of modelling the domain.
- No limits on query depth or complexity.
- Ignoring the N+1 problem until performance collapses.
- Checking permissions only at the top level.
- Adopting GraphQL for a simple internal API where REST would be easier.
- Letting the schema change without review or ownership.
Our API development service designs REST and GraphQL APIs that are secure and maintainable, and our system integration service connects them to your backend systems.
A worked example: a retail mobile app
A retailer mobile app needs product details, stock levels in nearby stores, customer reviews and personalised offers on a single screen. With REST, the app made six calls and downloaded a lot of unused fields, which was slow on weak mobile connections. The retailer adds a GraphQL layer that sits in front of the product, inventory, reviews and offers services.
The app now sends a single query for exactly the fields in the design. Resolvers use batching to avoid repeated lookups and a short cache for popular products. The team sets limits on query depth and uses persisted queries, so the API only runs approved operations from the app. Screen load time improves noticeably and designers can add or remove fields without waiting for new endpoints. The back end services remain unchanged, and the schema is reviewed by a small group that owns its quality.
GraphQL federation
As organisations grow, a single schema owned by one team becomes a bottleneck. Federation lets each domain team own a portion of the schema and its resolvers, while a router composes them into one graph for clients. This keeps ownership clear and allows teams to deploy independently, but it needs shared conventions for naming, error handling and security, and tooling that checks that a change will not break other subgraphs. Consider it when many teams contribute to the same API, not as a starting point for a small project.
To compare GraphQL with other integration styles, read what is a webhook and what is event driven architecture.
Key takeaways
- GraphQL is a query language and runtime that lets clients request exactly the data they need from a typed schema.
- It reduces over fetching and round trips and gives strong tooling and documentation.
- It needs limits, batching, field level authorisation and thoughtful caching.
- REST remains a good choice for simple, cacheable APIs.
- Design from client needs and assign clear ownership of the schema.
Quick glossary
| Term | Meaning |
|---|---|
| Schema | The typed description of the data and operations |
| Resolver | A function that fetches the data for a field |
| Mutation | A GraphQL operation that changes data |
| Federation | Composing several GraphQL services into one graph |
| Persisted query | A pre-registered query the server will run by identifier |
Read next: What Is API Management? Lifecycle, Features, Tools · What Is a Message Queue? Patterns and Best Practices · What Is RabbitMQ? Exchanges, Queues and Use Cases
Frequently asked questions
What is GraphQL in simple terms?
It is a way for apps to ask an API for exactly the data they need in a single request, using a typed schema, rather than calling many fixed endpoints.
What is the difference between GraphQL and REST?
REST exposes many endpoints returning fixed responses. GraphQL exposes one endpoint where the client specifies the fields it wants, avoiding over fetching and extra requests.
Is GraphQL better than REST?
Not always. GraphQL suits varied client needs and complex data, while REST is simpler and caches well for straightforward resource APIs.
Is GraphQL a database?
No. It is a layer in front of databases and services. Resolvers fetch data from whatever sources you use.
Is GraphQL secure?
It can be, with authentication, field level authorisation, query depth and cost limits, rate limiting and careful control of introspection.
What is the N+1 problem in GraphQL?
It occurs when resolving a list triggers one extra query per item. Batching and caching tools such as dataloaders prevent the performance hit.
Can GraphQL work with existing REST APIs?
Yes. A GraphQL layer can wrap existing REST and database sources, giving clients a unified interface without rewriting the back end.
What is GraphQL federation?
Federation lets several teams own parts of one logical schema, composed by a router, so large organisations can scale API development without a single bottleneck.
