Field-level permissions control access to specific fields in a GraphQL schema, allowing or denying users from seeing or modifying certain data fields.
Click to reveal answer
beginner
Why are field-level permissions important in GraphQL APIs?
They protect sensitive data by restricting access to certain fields based on user roles or conditions, ensuring users only see data they are allowed to access.
Click to reveal answer
intermediate
How can you implement field-level permissions in GraphQL?
By adding authorization logic in resolvers or using middleware that checks user roles before returning field data.
Click to reveal answer
intermediate
What happens if a user tries to access a field they don't have permission for?
The field can return null, an error, or be omitted from the response depending on the implementation, preventing unauthorized data exposure.
Click to reveal answer
intermediate
Give an example of a simple field-level permission check in a GraphQL resolver.
In a resolver, check the user's role before returning the field data, e.g., if (user.role !== 'admin') return null; else return sensitiveData;
Click to reveal answer
What does field-level permission control in GraphQL?
AGraphQL query syntax
BAccess to the entire API
CDatabase connection settings
DAccess to specific fields in the schema
✗ Incorrect
Field-level permissions control access to specific fields, not the entire API or other unrelated settings.
Where is field-level permission logic usually implemented in GraphQL?
AIn resolvers or middleware
BIn the database schema
CIn the client application
DIn the network protocol
✗ Incorrect
Resolvers or middleware are the places where access checks happen before returning data.
What is a common response when a user lacks permission for a field?
ACrash the server
BReturn full data anyway
CReturn null or an error
DIgnore the query
✗ Incorrect
Returning null or an error prevents unauthorized data exposure safely.
Why should field-level permissions be used instead of only query-level permissions?
ATo speed up queries
BTo protect sensitive fields within allowed queries
CTo reduce server load
DTo simplify client code
✗ Incorrect
Field-level permissions add finer control to protect sensitive data inside queries.
Which role might typically have access to all fields in a GraphQL API?
AAdmin
BGuest
CAnonymous
DUser
✗ Incorrect
Admins usually have full access, while other roles have limited permissions.
Explain what field-level permissions are and why they matter in GraphQL APIs.
Think about controlling access to parts of data, not just whole queries.
You got /3 concepts.
Describe how you would implement a simple field-level permission check in a GraphQL resolver.
Focus on the resolver function and conditional logic.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of field-level permissions in GraphQL?
easy
A. To cache query results
B. To control access to individual data fields
C. To define the schema structure
D. To speed up query execution
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 B
Quick Check:
Field-level permissions = control access [OK]
Hint: Focus on access control for each field, not overall schema [OK]
Common Mistakes:
Confusing permissions with schema design
Thinking it speeds up queries
Assuming it caches data
2. Which of the following is the correct way to apply a field-level permission directive in a GraphQL schema?
easy
A. type User { email: String @auth(role: "admin") }
B. type User { email: String auth(role: "admin") }
C. type User { email: String @auth[role="admin"] }
D. type User { email: String #auth(role: "admin") }
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 A
Quick Check:
Directive syntax = @name(args) [OK]
Hint: Remember directives start with @ and use parentheses [OK]
Common Mistakes:
Omitting the @ symbol
Using brackets [] instead of parentheses ()
Commenting out the directive
3. Given this GraphQL query and schema with field-level permissions:
type User { id: ID! name: String email: String @auth(role: "admin") }
Unauthorized fields are omitted, not null or error [OK]
Hint: Unauthorized fields are hidden, not null or error [OK]
Common Mistakes:
Expecting null instead of omitted field
Expecting an error response
Assuming all fields return regardless of role
4. You wrote this schema snippet:
type User { id: ID! name: String email: String @auth(role: admin) }
Why does this cause an error when applying field-level permissions?
medium
A. The role value admin should be a string in quotes
B. The @auth directive is not allowed on fields
C. ID! type cannot have directives
D. The email field must be nullable to use 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 A
Quick Check:
Directive args strings need quotes [OK]
Hint: Always put string arguments in quotes inside directives [OK]
Common Mistakes:
Forgetting quotes around string arguments
Thinking directives can't be on fields
Assuming non-null types disallow directives
5. You want to allow users to see their own email field but only admins to see others' emails. Which field-level permission setup achieves this?
hard
A. Use @auth(role: "admin") on email field only
B. Make email field public and filter in resolver
C. Use a custom directive checking if user is owner or admin
D. Remove email field from schema and add a separate query
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 C
Quick Check:
Conditional access needs custom logic [OK]
Hint: Use custom directives for complex permission logic [OK]
Common Mistakes:
Using simple role checks only
Exposing sensitive fields publicly
Overcomplicating schema with extra queries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.