REST VS GraphQL
REST vs GraphQL
REST and GraphQL both help apps talk to backends, but they divide responsibility differently. REST centers on backend-designed URLs, while GraphQL centers on client-written queries against a backend schema. This note compares their mental models, request shapes, Swift usage, tradeoffs, and when each approach fits best.
If you already know regular HTTP APIs, GraphQL is easier to understand as a different way for the client and backend to agree on data.
REST is more like:
The backend creates many endpoints, and the client requests a specific URL.
GraphQL is more like:
The backend provides a schema, and the client declares exactly what data it wants.
1. What Is REST?
REST APIs usually expose different resources through different URLs.
For example:
1 | |
The response might be:
1 | |
In Swift, a REST request often looks like this:
1 | |
The key idea:
In REST, the backend endpoint usually decides the shape of the response.
2. What Is GraphQL?
GraphQL usually uses a single endpoint:
1 | |
The client sends a GraphQL query in the HTTP body:
1 | |
Written more clearly, the GraphQL query looks like this:
1 | |
This means:
1 | |
The response matches the fields requested by the client:
1 | |
The key idea:
In GraphQL, the client declares the shape of the response.
3. The Core Difference
REST focuses on URLs:
1 | |
GraphQL focuses on queries:
1 | |
REST says:
Give me the data defined by this endpoint.
GraphQL says:
According to the schema, give me exactly these fields.
4. Field Selection
With REST, the response is usually fixed by the backend.
Maybe you only need this:
1 | |
But the REST API might return much more:
1 | |
With GraphQL, you can request only the field you need:
1 | |
Response:
1 | |
This avoids over-fetching, which means receiving data you do not need.
5. Nested Data
With REST, nested or related data may require multiple requests:
1 | |
With GraphQL, you can request related data in one query:
1 | |
This can reduce the number of network requests, especially on mobile apps where one screen may need data from multiple resources.
6. Operation Names and Variables
Consider this query:
1 | |
Breakdown:
1 | |
The variables are sent separately:
1 | |
Important detail:
1 | |
The JSON key does not include $.
You can also omit the operation name:
1 | |
You can also hardcode the argument:
1 | |
But in real projects, this is usually preferred:
1 | |
Because named operations are easier to debug, log, and use with code generation tools.
7. Making a GraphQL Request in Swift
GraphQL is still just an HTTP request.
A REST request might look like this:
1 | |
A GraphQL request usually looks like this:
1 | |
The important line is:
1 | |
It sends this JSON to the server:
1 | |
8. Backend vs Client Responsibility
GraphQL does not mean the client can request anything it imagines.
The backend defines a schema:
1 | |
The client can only select fields that exist in the schema.
A useful way to think about it:
1 | |
9. REST vs GraphQL Comparison
| Topic | REST | GraphQL |
|---|---|---|
| Endpoint | Usually many URLs | Usually one /graphql endpoint |
| Common HTTP Method | GET, POST, PUT, DELETE | Usually POST |
| Response Shape | Usually decided by the backend | Decided by the client query |
| Parameters | URL path, query string, or body | Variables in request body |
| Nested Data | May require multiple requests | Can be fetched in one query |
| Type System | Often documented separately | Built into the schema |
| Learning Curve | Lower | Higher |
| Caching | Works naturally with HTTP caching | Needs more specific strategy |
| Best For | Simple resource-based APIs | Apps that combine many related data types |
10. When Should You Use REST?
REST is a good fit when:
1 | |
Example:
1 | |
REST is direct, familiar, and works very well for many APIs.
11. When Should You Use GraphQL?
GraphQL is a good fit when:
1 | |
For iOS development, GraphQL is especially useful when a screen needs a carefully shaped set of data.
For example, a profile screen may need:
1 | |
With REST, that may require multiple endpoints.
With GraphQL, it can be one query.
12. Simple Mental Model
REST:
1 | |
GraphQL:
1 | |
REST is like going to a fixed service window.
GraphQL is like ordering from a menu:
1 | |
13. Final Summary
REST and GraphQL are both ways to build APIs.
REST is simpler and URL-centered.
GraphQL is more flexible and query-centered.
The biggest difference is not HTTP itself. Both can use HTTP.
The biggest difference is who controls the response shape:
1 | |