It is a way to control access to parts of a GraphQL schema by using special annotations called directives that check user permissions before resolving fields.
Click to reveal answer
beginner
How do you apply a directive for authorization on a GraphQL field?
You add the directive name with any needed arguments directly beside the field in the schema, for example: email: String @auth(role: "ADMIN").
Click to reveal answer
intermediate
What role does the resolver play in directive-based authorization?
The resolver checks the directive's rules during query execution and either allows the data to be returned or denies access if the user lacks permission.
Click to reveal answer
intermediate
Why use directive-based authorization instead of checking permissions inside resolvers?
Directives keep authorization logic separate and reusable, making the schema cleaner and easier to maintain.
Click to reveal answer
beginner
Name a common directive name used for authorization in GraphQL schemas.
A common directive name is @auth, which often takes arguments like user roles or permissions to check.
Click to reveal answer
What does a directive in GraphQL do for authorization?
AFormats the output data
BChecks user permissions before returning data
CDefines the database schema
DCaches query results
✗ Incorrect
Directives for authorization check if a user has permission to access certain data before the data is returned.
Where do you place an authorization directive in a GraphQL schema?
AOn the field or type that needs protection
BOnly in the query operation
CIn the database configuration
DInside the client application code
✗ Incorrect
Authorization directives are placed on schema fields or types to control access at the data level.
What happens if a user does not meet the directive's authorization rules?
AThe server crashes
BThe query runs normally
CThe field returns an error or null
DThe data is modified
✗ Incorrect
If authorization fails, the server usually returns an error or null for that field to prevent unauthorized access.
Which of these is a benefit of using directive-based authorization?
AAutomatically encrypts data
BIncreases query execution time
CRemoves the need for authentication
DCentralizes access control logic
✗ Incorrect
Directive-based authorization centralizes and simplifies access control in the schema.
What argument might an @auth directive take?
Arole
Bcolor
Csize
Dformat
✗ Incorrect
The @auth directive often takes a 'role' argument to specify which user roles can access the field.
Explain how directive-based authorization works in a GraphQL schema.
Think about how special tags in the schema control who can see data.
You got /4 concepts.
Describe the advantages of using directive-based authorization compared to putting authorization checks inside resolvers.
Consider how keeping code organized helps when your app grows.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using directive-based authorization in GraphQL?
easy
A. To control who can access certain data or operations
B. To speed up query execution
C. To format the output data
D. To create new database tables
Solution
Step 1: Understand directive-based authorization
Directive-based authorization adds rules to GraphQL fields or types to restrict access based on user roles or permissions.
Step 2: Identify the main goal
The main goal is to control access to data or operations, ensuring only authorized users can see or change certain information.
Final Answer:
To control who can access certain data or operations -> Option A
Quick Check:
Authorization controls access = B [OK]
Hint: Authorization directives control access permissions [OK]
Common Mistakes:
Confusing authorization with performance optimization
Thinking directives format data output
Assuming directives create database structures
2. Which of the following is the correct syntax to apply a directive named @auth requiring a role ADMIN on a GraphQL field?
easy
A. type Query { secretData: String @auth(role: ADMIN) }
B. type Query { secretData: String @auth(role: "ADMIN") }
C. type Query { secretData: String @auth(role=ADMIN) }
D. type Query { secretData: String @auth[role=ADMIN] }
Solution
Step 1: Recall directive syntax in GraphQL
Directives use parentheses with named arguments and string values in quotes, e.g., @directiveName(argName: "value").
Step 2: Match the correct syntax
type Query { secretData: String @auth(role: "ADMIN") } correctly uses @auth(role: "ADMIN") with quotes around the string role value.
Final Answer:
type Query { secretData: String @auth(role: "ADMIN") } -> Option B
Quick Check:
Directive args use parentheses and quotes = D [OK]
Hint: Directives use parentheses and quoted strings for arguments [OK]
Common Mistakes:
Using square brackets instead of parentheses
Omitting quotes around string values
Using equals sign without quotes
3. Given the schema snippet:
type Query {
userProfile: User @auth(role: "USER")
}
type User {
id: ID!
email: String
secretNote: String @auth(role: "ADMIN")
}
What happens when a user with role USER queries userProfile { id email secretNote }?
medium
A. The query returns only id
B. The query returns all fields including secretNote
C. The query fails with an authorization error
D. The query returns id and email, but secretNote is blocked
Solution
Step 1: Analyze field-level directives
The userProfile field requires role USER, so the user can access it. The secretNote field requires ADMIN role, which the user does not have.
Step 2: Determine accessible fields
The user can see id and email but not secretNote because of the stricter ADMIN directive on that field.
Final Answer:
The query returns id and email, but secretNote is blocked -> Option D
Quick Check:
Field directives restrict access individually = C [OK]
Hint: Field directives override parent access; check each field's role [OK]
Common Mistakes:
Assuming all fields inherit parent directive permissions
Expecting full data without role ADMIN
Thinking query fails instead of partial data
4. Consider this schema snippet:
type Query {
adminData: String @auth(role: "ADMIN")
}
Which of the following fixes the error if the directive @auth is not recognized by the GraphQL server?
medium
A. Define the @auth directive in the schema with directive @auth(role: String) on FIELD_DEFINITION
B. Remove the @auth directive from the schema
C. Change @auth to @authorize without defining it
D. Rename the field adminData to adminInfo
Solution
Step 1: Understand directive registration
GraphQL requires custom directives to be declared in the schema before use, specifying their name, arguments, and locations.
Step 2: Apply correct fix
Define the @auth directive in the schema with directive @auth(role: String) on FIELD_DEFINITION correctly defines the @auth directive with a role argument and usage on field definitions, enabling the server to recognize it.
Final Answer:
Define the @auth directive in the schema with directive @auth(role: String) on FIELD_DEFINITION -> Option A
Quick Check:
Custom directives must be declared in schema = A [OK]
Hint: Declare custom directives in schema before use [OK]
Common Mistakes:
Removing directives without replacing authorization
Using undefined directive names
Changing field names unrelated to directive errors
5. You want to restrict access so that only users with role EDITOR or ADMIN can update posts. Which directive usage correctly applies this logic on the updatePost mutation?
hard
A. type Mutation { updatePost(id: ID!, content: String!): Post @auth(role: "EDITOR") @auth(role: "ADMIN") }
B. type Mutation { updatePost(id: ID!, content: String!): Post @auth(roles: ["EDITOR", "ADMIN"]) }
C. type Mutation { updatePost(id: ID!, content: String!): Post @auth(role_in: ["EDITOR", "ADMIN"]) }
D. type Mutation { updatePost(id: ID!, content: String!): Post @auth(role: "EDITOR" OR "ADMIN") }
Solution
Step 1: Understand role list syntax in directives
To allow multiple roles, the directive argument should accept a list (array) of roles, often named role_in or similar, depending on implementation.
Step 2: Evaluate options for correct syntax
type Mutation { updatePost(id: ID!, content: String!): Post @auth(role_in: ["EDITOR", "ADMIN"]) } uses @auth(role_in: ["EDITOR", "ADMIN"]), which correctly passes an array of roles to check for authorization.
Final Answer:
type Mutation { updatePost(id: ID!, content: String!): Post @auth(role_in: ["EDITOR", "ADMIN"]) } -> Option C
Quick Check:
Use array argument for multiple roles = A [OK]
Hint: Use array argument to list multiple allowed roles [OK]
Common Mistakes:
Stacking multiple @auth directives instead of one with list
Using invalid OR syntax inside directive argument
Using wrong argument name like roles instead of role_in
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.