What if you could lock every door in your app with just one simple tag?
Why Directive-based authorization in GraphQL? - Purpose & Use Cases
Imagine you have a big app where many users can see or change data. You try to check who can do what by writing checks everywhere in your code. It's like putting locks on every door manually.
This manual way is slow and confusing. You might forget a check, or make mistakes that let someone see or change things they shouldn't. It's hard to keep track and fix later.
Directive-based authorization lets you add simple tags (directives) in your GraphQL schema to say who can do what. It keeps all rules in one place, easy to read and update, so your app stays safe and clean.
if (user.role === 'admin') { return data; } else { throw new Error('Not allowed'); }
type Query { secretData: String @auth(role: "admin") }You can control access clearly and safely, making your app easier to build and protect without messy code everywhere.
A social media app uses directive-based authorization to let only friends see private posts, by adding directives to the post fields instead of checking in every resolver.
Manual checks are hard to manage and error-prone.
Directives keep authorization rules clear and centralized.
This makes apps safer and easier to maintain.
Practice
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 AQuick Check:
Authorization controls access = B [OK]
- Confusing authorization with performance optimization
- Thinking directives format data output
- Assuming directives create database structures
@auth requiring a role ADMIN on a GraphQL field?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 BQuick Check:
Directive args use parentheses and quotes = D [OK]
- Using square brackets instead of parentheses
- Omitting quotes around string values
- Using equals sign without quotes
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 }?Solution
Step 1: Analyze field-level directives
TheuserProfilefield requires role USER, so the user can access it. ThesecretNotefield requires ADMIN role, which the user does not have.Step 2: Determine accessible fields
The user can seeidandemailbut notsecretNotebecause of the stricter ADMIN directive on that field.Final Answer:
The query returns id and email, but secretNote is blocked -> Option DQuick Check:
Field directives restrict access individually = C [OK]
- Assuming all fields inherit parent directive permissions
- Expecting full data without role ADMIN
- Thinking query fails instead of partial data
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?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@authdirective in the schema withdirective @auth(role: String) on FIELD_DEFINITIONcorrectly defines the@authdirective 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 AQuick Check:
Custom directives must be declared in schema = A [OK]
- Removing directives without replacing authorization
- Using undefined directive names
- Changing field names unrelated to directive errors
EDITOR or ADMIN can update posts. Which directive usage correctly applies this logic on the updatePost mutation?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 CQuick Check:
Use array argument for multiple roles = A [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
