GraphQL vs REST comparison - Performance Comparison
When comparing GraphQL and REST, it's important to understand how the time to get data grows as the amount of data or requests increase.
We want to see how the number of operations changes when fetching data with each method.
Analyze the time complexity of these two data fetching approaches.
# GraphQL query fetching user and posts in one request
query {
user(id: "1") {
name
posts {
title
comments {
text
}
}
}
}
# REST approach requires multiple requests:
# GET /users/1
# GET /users/1/posts
# GET /posts/{postId}/comments for each post
This shows GraphQL fetching nested data in one request, while REST needs several requests.
Look at what repeats when fetching data.
- Primary operation: REST makes multiple HTTP requests, one per resource type and per nested item.
- How many times: For REST, number of requests grows with number of posts and comments; GraphQL sends one request regardless.
As the number of posts and comments grows, REST needs more requests, but GraphQL stays with one request.
| Input Size (posts + comments) | Approx. REST Requests |
|---|---|
| 10 | ~1 (user) + 1 (posts) + 10 (comments) = 12 |
| 100 | ~1 + 1 + 100 = 102 |
| 1000 | ~1 + 1 + 1000 = 1002 |
Pattern observation: REST requests grow linearly with nested items; GraphQL request count stays constant.
Time Complexity: O(n) for REST, O(1) for GraphQL
This means REST's work grows with the number of nested items, while GraphQL handles it in a single operation.
[X] Wrong: "GraphQL always takes longer because it fetches more data in one request."
[OK] Correct: GraphQL reduces the number of requests, so even if the query is complex, it often runs faster than many REST calls.
Understanding how data fetching scales helps you explain API choices clearly and shows you think about performance in real projects.
What if the REST API supported batch requests for nested data? How would that affect the time complexity?
Practice
Solution
Step 1: Understand GraphQL data fetching
GraphQL lets clients specify exactly what data they want in one request, avoiding over-fetching or under-fetching.Step 2: Compare with REST approach
REST typically requires multiple URLs and fixed data shapes, which can lead to multiple requests or extra data.Final Answer:
GraphQL allows fetching exactly the data you want in a single request. -> Option DQuick Check:
GraphQL = precise single-request data fetching [OK]
- Thinking GraphQL needs multiple URLs
- Believing GraphQL returns fixed data shapes
- Assuming GraphQL can't query nested data
Solution
Step 1: Identify GraphQL query syntax
GraphQL queries start with the keyword 'query' followed by the requested fields inside braces.Step 2: Check each option
query { user { name, email } } matches GraphQL syntax. GET /user/name,email is REST style URL. SELECT name, email FROM user is SQL. { user: { name, email } } is invalid JSON-like structure.Final Answer:
query { user { name, email } } -> Option BQuick Check:
GraphQL query syntax = query { user { name, email } } [OK]
- Using REST or SQL syntax instead of GraphQL
- Omitting the 'query' keyword
- Incorrect braces placement
query { book(id: "1") { title author { name } } }What data structure will the response most likely contain?
Solution
Step 1: Understand GraphQL response format
GraphQL responses wrap data inside a 'data' object, matching the query structure exactly.Step 2: Match nested fields
The query requests 'book' with 'title' and nested 'author' with 'name', so response must reflect nested objects.Final Answer:
{"data": {"book": {"title": "1984", "author": {"name": "George Orwell"}}}} -> Option AQuick Check:
GraphQL response = nested 'data' object [OK]
- Omitting the 'data' wrapper
- Flattening nested objects incorrectly
- Using arrays for single nested objects
query { user(id: 5) { name, email } }Solution
Step 1: Check argument types in GraphQL
GraphQL requires string arguments to be quoted. Here, id is likely a string and should be "5".Step 2: Verify field syntax
Fields 'name' and 'email' are correctly listed with commas, no parentheses needed.Final Answer:
The id value should be a string, so it needs quotes. -> Option CQuick Check:
String arguments need quotes in GraphQL [OK]
- Leaving string arguments unquoted
- Adding parentheses around fields
- Assuming query keyword is optional
Solution
Step 1: Analyze requirements for flexible data fetching and minimal requests
GraphQL excels at letting clients specify exactly what data they want, reducing multiple requests.Step 2: Consider caching and efficiency
While REST is simpler and caches well, GraphQL's single-request flexibility suits mobile apps needing efficient data transfer.Final Answer:
Use GraphQL because it allows clients to request exactly what they need in one request. -> Option AQuick Check:
Flexible, minimal requests = GraphQL [OK]
- Choosing REST for flexibility over GraphQL
- Thinking GraphQL needs multiple requests
- Assuming REST handles nested queries better
Ready to prove it?
Pass the test and this pattern moves unknown
