Field-level permissions in GraphQL - Time & Space Complexity
When checking field-level permissions in GraphQL, we want to know how the time to check access grows as the number of fields grows.
We ask: How does permission checking scale when more fields are requested?
Analyze the time complexity of the following GraphQL resolver snippet that checks permissions for each requested field.
query getUserData {
user(id: "123") {
id
name
email
address
}
}
// Resolver pseudocode:
// for each requested field, check if user has permission
// if allowed, fetch and return the field value
This code checks permissions for each field requested in the query before returning data.
Look for repeated checks or loops.
- Primary operation: Permission check for each requested field.
- How many times: Once per field requested in the query.
As the number of fields requested grows, the number of permission checks grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 fields | 10 permission checks |
| 100 fields | 100 permission checks |
| 1000 fields | 1000 permission checks |
Pattern observation: The work grows directly with the number of fields requested.
Time Complexity: O(n)
This means the time to check permissions grows linearly with the number of fields requested.
[X] Wrong: "Checking permissions once for the whole query is enough."
[OK] Correct: Each field can have different permissions, so skipping per-field checks can cause wrong data exposure.
Understanding how permission checks scale helps you design secure and efficient GraphQL APIs, a valuable skill in real projects.
What if we cached permission results for fields? How would that affect the time complexity?