Scalar types (String, Int, Float, Boolean, ID) in GraphQL - Time & Space Complexity
When working with scalar types in GraphQL, it's important to understand how the time to process these values changes as the amount of data grows.
We want to know how the cost of handling these simple data types scales when many values are involved.
Analyze the time complexity of the following GraphQL query fetching scalar fields.
query GetUsers {
users {
id
name
age
isActive
}
}
This query requests scalar fields (ID, String, Int, Boolean) for each user in the list.
Look for repeated actions in the query processing.
- Primary operation: Reading scalar fields for each user.
- How many times: Once per user in the list.
As the number of users grows, the total work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Reading 10 users' scalar fields |
| 100 | Reading 100 users' scalar fields |
| 1000 | Reading 1000 users' scalar fields |
Pattern observation: The work increases evenly as the number of users increases.
Time Complexity: O(n)
This means the time to fetch scalar fields grows directly with the number of items requested.
[X] Wrong: "Fetching scalar fields is instant and does not depend on the number of items."
[OK] Correct: Even simple fields must be read for each item, so more items mean more work.
Understanding how simple data types scale helps you explain performance clearly and shows you grasp the basics of data fetching costs.
"What if we added a nested list of posts for each user? How would the time complexity change?"