Complete the code to define a GraphQL field with restricted access using a directive.
type User { id: ID!, name: String!, email: String! @[1] }The @auth directive is commonly used to enforce field-level permissions in GraphQL schemas.
Complete the code to specify a role-based access control directive on a GraphQL field.
type Post { content: String! @auth(role: "[1]") }The admin role is often used to restrict access to sensitive fields to administrators only.
Fix the error in the directive syntax to correctly restrict access to the 'salary' field.
type Employee { salary: Float! @auth(role: [1]) }The role value must be a string literal enclosed in double quotes inside the directive.
Fill both blanks to define a GraphQL query with field-level permission and a resolver function.
type Query { user: User @auth(role: "[1]") }
const resolvers = { Query: { user: () => [2] } }The query is restricted to the admin role, and the resolver calls getUser() to fetch user data.
Fill all three blanks to create a GraphQL schema with field-level permissions and a resolver that checks user role.
type User {
id: ID!,
email: String! @auth(role: "[1]")
}
const resolvers = {
User: {
email: (parent, args, context) => {
if (context.user.role === "[2]") {
return parent.[3];
}
return null;
}
}
};The email field is restricted to the admin role. The resolver checks if the user's role is 'admin' and returns the email; otherwise, it returns null.