Given this GraphQL schema snippet with a directive @auth(role: "ADMIN") on the secretData field, what will be the result of the query below if the user role is USER?
query {
secretData
}type Query {
secretData: String @auth(role: "ADMIN")
}Think about what happens when a user without the required role tries to access a protected field.
The @auth directive restricts access to the secretData field to users with the ADMIN role. Since the user has the USER role, the server returns an authorization error.
Choose the correct description of how directive-based authorization works in GraphQL schemas.
Consider what role directives play in controlling access to data.
Directive-based authorization uses directives to specify access control rules on schema fields. The server checks these rules when resolving queries to allow or deny access.
Which option shows the correct way to define a custom @auth directive that accepts a role argument of type String?
directive @auth(role: String) on FIELD_DEFINITIONRemember the syntax for defining arguments in directives.
The correct syntax uses a colon : to specify the argument type without quotes. Option A misses the colon, C incorrectly quotes the type, and D uses a non-null type which is allowed but not shown in the original.
You want to allow access to a field for users with roles ADMIN or MODERATOR. Which directive usage is the most efficient and correct?
Think about how to pass multiple values as an argument in GraphQL directives.
Passing an array of roles as role: ["ADMIN", "MODERATOR"] is the correct way to specify multiple allowed roles in one directive. Option B repeats the directive which is invalid, C uses a wrong argument name, and D passes a string instead of an array.
Given this schema snippet, unauthorized users can still access privateInfo. What is the likely cause?
directive @auth(role: String) on FIELD_DEFINITION
type Query {
privateInfo: String @auth(role: "ADMIN")
}Defining a directive in the schema is not enough to enforce authorization.
GraphQL directives require server-side implementation to enforce rules. Simply declaring the directive in the schema does not block access unless the server checks the directive during query execution.