The context argument helps share common data or tools across all parts of a GraphQL request. It makes it easy to access things like user info or database connections without repeating code.
When you want to check who is making a request to control access.
When you need to share a database connection with all resolvers.
When you want to pass user settings or preferences to many parts of your API.
When you want to log or track requests in one place.
When you want to share helper functions or services across resolvers.
Syntax
GraphQL
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => {
// create and return context objectreturn { user: getUserFromReq(req), db };
}
});
The context function runs once per request and returns an object.
Resolvers receive this context as their third argument.
Examples
Simple context returning a database connection.
GraphQL
context: () => ({ db: myDatabase })
Context that extracts user info from request headers.
GraphQL
context: ({ req }) => {
const user = authenticate(req.headers.authorization);
return { user };
}
If running this code causes TypeError: context.getUser is not a function, what is the most likely cause?
medium
A. The context object does not have a getUser method
B. The args.id is undefined
C. The parent argument is missing
D. The resolver function is missing the args parameter
Solution
Step 1: Analyze the error message
The error says getUser is not a function on context, meaning context.getUser is undefined or not callable.
Step 2: Check other options
Undefined args.id or missing parent won't cause this specific error. Missing args parameter would cause a different error.
Final Answer:
The context object does not have a getUser method -> Option A
Quick Check:
TypeError means missing method on context [OK]
Hint: TypeError means method missing on context object [OK]
Common Mistakes:
Blaming args.id for method not found error
Assuming parent argument causes this error
Ignoring that context must have the method
5. You want to add user authentication info to all resolvers using the context argument. Which approach correctly sets up the context for this purpose?
hard
A. Pass user info as an argument in every query instead of context
B. Define user info inside each resolver separately without using context
C. Create context once per request including user info, then pass it to all resolvers
D. Use global variables to store user info accessible by resolvers
Solution
Step 1: Understand context usage for shared data
Context is designed to be created once per request and shared with all resolvers, perfect for authentication info.
Step 2: Evaluate other options
Defining user info inside each resolver duplicates code; passing user info as query args is insecure and inconvenient; global variables are unsafe for concurrent requests.
Final Answer:
Create context once per request including user info, then pass it to all resolvers -> Option C
Quick Check:
Context = single shared object per request [OK]
Hint: Set context once per request with user info for all resolvers [OK]
Common Mistakes:
Duplicating user info in every resolver
Passing sensitive info as query arguments
Using global variables causing data leaks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.