Schema visibility control in GraphQL - Time & Space Complexity
When controlling which parts of a GraphQL schema are visible, it's important to understand how the time to check visibility grows as the schema gets bigger.
We want to know how the effort to decide what is visible changes when the schema has more types and fields.
Analyze the time complexity of the following code snippet.
query IntrospectionQuery {
__schema {
types {
name
fields(includeDeprecated: true) {
name
isDeprecated
}
}
}
}
This query fetches all types in the schema and their fields, checking if each field is deprecated.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all types, then looping through all fields of each type.
- How many times: Once for each type, and once for each field inside each type.
As the number of types and fields grows, the time to check visibility grows too.
| Input Size (n = total fields) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of operations grows roughly in direct proportion to the total number of fields.
Time Complexity: O(n)
This means the time to check visibility grows linearly with the number of fields in the schema.
[X] Wrong: "Checking visibility is constant time no matter how many fields there are."
[OK] Correct: Each field must be checked individually, so more fields mean more work.
Understanding how schema visibility checks scale helps you design efficient APIs and shows you can think about performance in real projects.
What if we cached visibility results for fields? How would that change the time complexity?
Practice
Solution
Step 1: Understand schema visibility control concept
Schema visibility control is about restricting access to parts of the GraphQL schema based on user roles or permissions.Step 2: Identify the main goal
The goal is to protect sensitive data and tailor the API by limiting what users can see or use.Final Answer:
To limit what parts of the schema users can see or access -> Option AQuick Check:
Schema visibility control = limit schema access [OK]
- Confusing visibility control with caching
- Thinking it auto-generates docs
- Assuming it converts queries to SQL
Solution
Step 1: Understand directive usage for visibility
Directives like@hide(if: true)can be used on fields to conditionally hide them based on logic.Step 2: Evaluate options
@privateis not a standard directive;@skip(if: false)means do not skip (show field);@deprecatedmarks fields as deprecated but does not hide them.Final Answer:
@hide(if: true)on the field definition -> Option DQuick Check:
Use conditional directives like @hide to control visibility [OK]
- Using @deprecated to hide fields
- Applying directives on queries instead of fields
- Misunderstanding @skip logic
type User {
id: ID!
email: String @hide(if: $isGuest)
name: String
}What fields will a guest user see if
$isGuest is true?Solution
Step 1: Analyze the directive condition
The@hide(if: $isGuest)directive hides theemailfield when$isGuestis true.Step 2: Determine visible fields for guest
Since$isGuestis true,emailis hidden. The visible fields areidandname.Final Answer:
id and name only -> Option AQuick Check:
@hide(if: true) hides field = id and name visible [OK]
- Including hidden fields in output
- Confusing @hide with @skip
- Ignoring variable values in directives
email: String @hide(if: isGuest)
But the field is always visible. What is the likely error?
Solution
Step 1: Check variable usage in directive
GraphQL variables in directives must be prefixed with$. Here,isGuestis missing$.Step 2: Understand impact of missing $
Without$, the directive condition is invalid or always false, so the field is never hidden.Final Answer:
Missing $ before variable name in directive condition -> Option BQuick Check:
Variables need $ prefix in directives [OK]
- Forgetting $ before variable in directive
- Assuming @hide is unsupported
- Thinking directives can't be on scalar fields
Solution
Step 1: Identify requirement for conditional visibility
The email field should be visible only to admins, so visibility depends on user role.Step 2: Evaluate options for controlling visibility
Use a directive like@hide(if: $isNotAdmin)on the email field and pass$isNotAdminfrom server context. This uses a directive with a variable from server context to hide email for non-admins, which is the correct schema visibility control method.Step 3: Understand why other options fail
Removing the field removes it for all users; @deprecated only marks as outdated; returning null hides data but not schema field.Final Answer:
Use@hide(if: $isNotAdmin)on the email field and pass$isNotAdminfrom server context -> Option CQuick Check:
Use conditional directives with server variables for visibility [OK]
- Removing fields instead of hiding conditionally
- Using @deprecated to hide fields
- Returning null but not hiding schema field
Ready to prove it?
Pass the test and this pattern moves unknown
