0
0
GraphQLquery~20 mins

Role-based access control in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Role-based Access Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Query Result: Fetching User Roles

Given the following GraphQL query to fetch user roles, what will be the output?

query { user(id: "123") { id name roles } }

Assume the user with id "123" has roles: ["admin", "editor"]

GraphQL
query { user(id: "123") { id name roles } }
A{"data":{"user":{"id":"123","name":"Alice","roles":["admin","editor"]}}}
B{"data":{"user":{"id":"123","name":"Alice","roles":["user"]}}}
C{"errors":[{"message":"User not found"}]}
D{"data":{"user":null}}
Attempts:
2 left
💡 Hint

Check the roles assigned to the user with id "123".

🧠 Conceptual
intermediate
1:30remaining
Understanding Role Hierarchies

In a role-based access control system, which statement best describes role hierarchies?

ARoles can inherit permissions from other roles, allowing hierarchical access control.
BRoles are independent and cannot inherit permissions from other roles.
CUsers can only have one role at a time, so hierarchies are unnecessary.
DRole hierarchies mean roles are assigned randomly without structure.
Attempts:
2 left
💡 Hint

Think about how permissions can be shared or extended between roles.

📝 Syntax
advanced
2:00remaining
GraphQL Schema for Role-based Access Control

Which of the following GraphQL schema snippets correctly defines a User type with roles as a list of strings?

GraphQL
type User { id: ID! name: String! roles: [String!]! }
Atype User { id: ID name: String roles: String }
Btype User { id: ID! name: String! roles: [String!]! }
Ctype User { id: ID! name: String! roles: String! }
Dtype User { id: ID! name: String! roles: [String] }
Attempts:
2 left
💡 Hint

Roles should be a non-null list of non-null strings.

optimization
advanced
2:30remaining
Optimizing Role Checks in GraphQL Resolvers

You want to optimize role checks in your GraphQL resolvers to avoid repeated database calls. Which approach is best?

AFetch roles only when an error occurs.
BFetch user roles separately in each resolver to ensure fresh data.
CIgnore roles and allow all access to simplify code.
DFetch user roles once per request and cache them in context for all resolvers.
Attempts:
2 left
💡 Hint

Think about efficiency and avoiding repeated work.

🔧 Debug
expert
3:00remaining
Debugging Role-based Access Control Error

Given this resolver snippet, what error will occur if the user object is null?

const resolver = (parent, args, context) => { if (!context.user.roles.includes('admin')) { throw new Error('Access denied'); } return 'Success'; }
ANo error, returns 'Success'
BReferenceError: roles is not defined
CTypeError: Cannot read property 'includes' of null
DError: Access denied
Attempts:
2 left
💡 Hint

Consider what happens if context.user is null.