What if your data could decide who gets to see it, all by itself?
Why Schema visibility control in GraphQL? - Purpose & Use Cases
Imagine you have a big library of books, but you want to show only some shelves to certain visitors. Without a clear way to hide or show shelves, you have to manually cover or remove books every time someone visits.
Manually hiding parts of your data schema is slow and confusing. You might forget to hide sensitive info or accidentally expose too much. It's like trying to remember which books to cover for each visitor, leading to mistakes and wasted time.
Schema visibility control lets you set clear rules on what parts of your data are visible to whom. It automatically hides or shows fields based on these rules, so you don't have to do it by hand every time.
# Manually remove fields before sending response if user.isAdmin: return fullSchema else: return removeSensitiveFields(fullSchema)
type Query {
publicData: Data @visibleTo(role: "user")
adminData: Data @visibleTo(role: "admin")
}It makes your API smarter and safer by automatically controlling who sees what, without extra work.
A social media app shows basic profile info to everyone but only shows email and phone number to the profile owner, all controlled by schema visibility rules.
Manual hiding of schema parts is error-prone and slow.
Schema visibility control automates who can see what in your data.
This keeps data safe and your API easier to manage.
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
