Context-based authentication helps check who you are before giving access to data. It keeps data safe by using information about the user or request.
When you want to allow only logged-in users to see their own data.
When you need to check user roles before allowing certain actions.
When you want to track who made a request for auditing.
When you want to customize data shown based on user location or device.
When you want to protect sensitive data from unauthorized access.
Syntax
GraphQL
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => {
// Extract user info from request headersconst user = getUserFromToken(req.headers.authorization);
return { user };
}
});
The context function runs for every request and can add user info.
You can use the context inside resolvers to check user identity or permissions.
Examples
Extracts user info from the request header token and adds it to context.
GraphQL
context: ({ req }) => {
const user = getUserFromToken(req.headers.authorization);
return { user };
}
Sets a fixed user in context for testing or development.
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.