Context-based authentication is a method where access is granted based on additional information like user location, device, or time, not just username and password.
Click to reveal answer
beginner
Why use context-based authentication in GraphQL APIs?
It helps secure GraphQL APIs by checking extra details about the request, like user role or device, before allowing access to data.
Click to reveal answer
intermediate
How is context passed in GraphQL for authentication?
Context is passed as an object to each resolver, carrying user info and request details to decide if access should be allowed.
Click to reveal answer
intermediate
What role does the resolver play in context-based authentication?
Resolvers check the context data to verify if the user has permission to access or modify the requested data.
Click to reveal answer
beginner
Give an example of a context property used in authentication.
A common context property is 'user', which holds the logged-in user's ID and roles to check permissions.
Click to reveal answer
What does context-based authentication add to the login process?
AExtra checks like location or device
BOnly username and password
CNo authentication needed
DRandom password generation
✗ Incorrect
Context-based authentication adds extra checks such as location or device to improve security.
In GraphQL, where is the authentication context usually stored?
AIn the client UI
BIn the query string
CIn the database schema
DIn the resolver context object
✗ Incorrect
Authentication info is stored in the context object passed to resolvers.
Which of these is NOT typically part of context-based authentication?
AUser's favorite color
BDevice type
CTime of access
DUser location
✗ Incorrect
User's favorite color is unrelated to authentication context.
What happens if a resolver finds the user is not authorized in context-based authentication?
AIt ignores the check
BIt grants full access
CIt denies access to the data
DIt deletes the user
✗ Incorrect
Resolvers deny access if the user is not authorized.
Which GraphQL feature helps pass authentication info to resolvers?
AQuery variables
BContext object
CSchema definition
DFragments
✗ Incorrect
The context object carries authentication info to resolvers.
Explain how context-based authentication works in a GraphQL API.
Think about how extra information helps decide access.
You got /3 concepts.
Describe why context is important for authentication in GraphQL.
Consider what data resolvers need to check permissions.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of context-based authentication in GraphQL?
easy
A. To cache query results
B. To verify user identity using request information
C. To format the response data
D. To speed up query execution
Solution
Step 1: Understand context-based authentication
It uses information from the request, like tokens, to verify who the user is.
Step 2: Identify the main goal
The goal is to check user identity to allow or deny access to data.
Final Answer:
To verify user identity using request information -> Option B
Quick Check:
Context-based authentication = verify user identity [OK]
Hint: Context holds user info to check identity quickly [OK]
Common Mistakes:
Thinking it speeds up queries
Confusing with data formatting
Assuming it caches results
2. Which of the following is the correct way to add user info to the GraphQL context in a server setup?
easy
A. context: ({ req }) => ({ user: req.user })
B. context: (user) => ({ req: user })
C. context: () => ({ user: 'anonymous' })
D. context: ({ res }) => ({ user: res.user })
Solution
Step 1: Identify how context receives request info
Context function receives an object with request (req) and response (res).
Step 2: Correctly extract user from request
User info is usually attached to req, so context: ({ req }) => ({ user: req.user }) is correct.
Hint: Check user role in context to decide access [OK]
Common Mistakes:
Ignoring optional chaining and assuming error
Confusing role values
Assuming null return
4. Identify the error in this context setup code:
const server = new ApolloServer({
typeDefs,
resolvers,
context: (req) => {
const user = authenticate(req.headers.authorization);
return { user };
}
});
medium
A. The context function should destructure req as ({ req })
B. authenticate function is missing
C. context function should return user directly, not an object
D. Authorization header should be accessed as req.authorization
Solution
Step 1: Check context function parameter
ApolloServer passes an object with req property, so context should destructure it as ({ req }).
Step 2: Identify correct usage of authorization header
Authorization header is accessed via req.headers.authorization, so destructuring is needed.
Final Answer:
The context function should destructure req as ({ req }) -> Option A
Quick Check:
Context param = ({ req }) not (req) [OK]
Hint: Destructure req from context param: ({ req }) [OK]
Common Mistakes:
Using req directly without destructuring
Assuming authenticate is missing error
Returning user instead of object
5. You want to restrict a GraphQL query so only the user who owns the data or an admin can access it. Which context-based authentication check correctly enforces this?
resolve(parent, args, context) {
// args.id is the data owner ID
// context.user has id and role
???
}
hard
A. if (args.id === null) { return 'Allowed'; } else { return 'Denied'; }
B. if (context.user.id !== args.id && context.user.role !== 'admin') { return 'Allowed'; } else { return 'Denied'; }
C. if (context.user.role === 'admin') { return 'Denied'; } else { return 'Allowed'; }
D. if (context.user.id === args.id || context.user.role === 'admin') { return 'Allowed'; } else { return 'Denied'; }
Solution
Step 1: Understand access rules
Access is allowed if user owns data (user.id === args.id) or user is admin (role === 'admin').
Step 2: Check condition logic
if (context.user.id === args.id || context.user.role === 'admin') { return 'Allowed'; } else { return 'Denied'; } correctly uses OR (||) to allow access if either condition is true.
Final Answer:
if (context.user.id === args.id || context.user.role === 'admin') { return 'Allowed'; } else { return 'Denied'; } -> Option D
Quick Check:
Owner or admin -> allow access [OK]
Hint: Use OR to allow owner or admin access [OK]
Common Mistakes:
Using AND instead of OR
Denying admins access
Ignoring user ID check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.