What if you could share data safely without worrying about who sees what?
Why Field-level permissions in GraphQL? - Purpose & Use Cases
Imagine you have a big spreadsheet with sensitive info like salaries and personal contacts mixed with general data. You want to share it with your team, but only let some people see certain columns.
Manually hiding or sharing parts of the spreadsheet is slow and risky. You might forget to hide a column or accidentally share sensitive info. It's hard to keep track and update permissions as people join or leave.
Field-level permissions let you set rules so only the right people see the right data fields automatically. This keeps sensitive info safe without extra work every time you share or update data.
if(user.role == 'admin') { show salary; } else { hide salary; }
type User @auth(rules: [{ allow: owner }]) { salary: Int @auth(rules: [{ allow: groups, groups: ["Admin"] }]) }It enables secure, automatic control over who can see each piece of data, making sharing safe and simple.
A company's HR system shows employee names to all staff but only lets managers see salaries and personal details, all controlled automatically by field-level permissions.
Manual data sharing risks exposing sensitive info.
Field-level permissions automate who sees what data.
This keeps data safe and sharing easy.
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
