Directive-based authorization in GraphQL - Time & Space Complexity
When using directive-based authorization in GraphQL, it's important to understand how the time to check permissions grows as the number of fields or queries increases.
We want to know how the authorization checks affect the overall execution time.
Analyze the time complexity of this GraphQL schema snippet using directive-based authorization.
directive @auth(role: String) on FIELD_DEFINITION
type Query {
user(id: ID!): User @auth(role: "ADMIN")
posts: [Post] @auth(role: "USER")
}
type User {
id: ID!
name: String
}
This snippet shows fields protected by an @auth directive that checks user roles before returning data.
Look for repeated checks or loops in the authorization process.
- Primary operation: Authorization check for each field with @auth directive.
- How many times: Once per field requested in the query that has the directive.
As the number of fields requested with @auth increases, the number of authorization checks grows linearly.
| Input Size (fields with @auth) | Approx. Operations (auth checks) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of authorization checks grows directly with the number of protected fields requested.
Time Complexity: O(n)
This means the time to check authorization grows in a straight line as more protected fields are requested.
[X] Wrong: "Authorization checks happen once per query, no matter how many fields are protected."
[OK] Correct: Each protected field triggers its own check, so more fields mean more checks and more time.
Understanding how authorization scales helps you design efficient APIs and explain your reasoning clearly in interviews.
What if the authorization directive cached results per user session? How would that change the time complexity?
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
