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?
Practice
field-level permissions in GraphQL?Solution
Step 1: Understand the concept of field-level permissions
Field-level permissions restrict who can see or edit specific fields in data, protecting sensitive information.Step 2: Identify the main purpose
The main goal is to control access to individual data fields, not to speed up queries or define schema structure.Final Answer:
To control access to individual data fields -> Option BQuick Check:
Field-level permissions = control access [OK]
- Confusing permissions with schema design
- Thinking it speeds up queries
- Assuming it caches data
Solution
Step 1: Recall GraphQL directive syntax
Directives use the@directiveName(arguments)format placed after the field type.Step 2: Match the correct syntax
type User { email: String @auth(role: "admin") } uses@auth(role: "admin")correctly. Options A, B, and D have syntax errors or wrong symbols.Final Answer:
type User { email: String @auth(role: "admin") } -> Option AQuick Check:
Directive syntax = @name(args) [OK]
- Omitting the @ symbol
- Using brackets [] instead of parentheses ()
- Commenting out the directive
type User { id: ID! name: String email: String @auth(role: "admin") }If a user with role "user" runs:
{ user { id name email } }What will be the result?
Solution
Step 1: Understand the permission on the email field
The@auth(role: "admin")directive restricts email access to admin role only.Step 2: Predict query result for a user role
The user role is not admin, so email field is omitted from the response, not returned as null or error.Final Answer:
{ "data": { "user": { "id": "1", "name": "Alice" } } } -> Option DQuick Check:
Unauthorized fields are omitted, not null or error [OK]
- Expecting null instead of omitted field
- Expecting an error response
- Assuming all fields return regardless of role
type User { id: ID! name: String email: String @auth(role: admin) }Why does this cause an error when applying field-level permissions?
Solution
Step 1: Check the directive argument syntax
Directive arguments must be valid GraphQL values; strings require quotes.Step 2: Identify the error in role argument
The role value admin is unquoted, causing a syntax error. It should be "admin".Final Answer:
The role value admin should be a string in quotes -> Option AQuick Check:
Directive args strings need quotes [OK]
- Forgetting quotes around string arguments
- Thinking directives can't be on fields
- Assuming non-null types disallow directives
email field but only admins to see others' emails. Which field-level permission setup achieves this?Solution
Step 1: Analyze the requirement
Users can see their own email; admins can see all emails. This needs conditional logic.Step 2: Evaluate options for conditional access
Use a custom directive checking if user is owner or admin uses a custom directive to check ownership or admin role, fitting the requirement precisely.Step 3: Why other options fail
Use@auth(role: "admin")on email field only restricts email only to admins, blocking users from their own email. Make email field public and filter in resolver exposes email publicly, unsafe. Remove email field from schema and add a separate query complicates schema unnecessarily.Final Answer:
Use a custom directive checking if user is owner or admin -> Option CQuick Check:
Conditional access needs custom logic [OK]
- Using simple role checks only
- Exposing sensitive fields publicly
- Overcomplicating schema with extra queries
Ready to prove it?
Pass the test and this pattern moves unknown
