What is a GraphQL context object and what does it typically contain?
Understand the GraphQL context: a per-request object passed to every resolver, holding the user, DataLoaders, DB clients, and request metadata.
Expected Interview Answer
The context is a shared object created per request and passed as the third argument to every resolver, giving all resolvers access to request-scoped data like the authenticated user, data loaders, and database connections.
It is built once per operation, usually in the server's context function from the incoming HTTP request. Because the same object flows into every resolver in the resolution tree, it is the standard place to put cross-cutting concerns: the current user derived from a token, DataLoader instances for batching, database or service clients, and request metadata like headers or a request ID. It should hold per-request state, not per-field state, and is not a place for business logic.
- Shares authenticated user across all resolvers
- Holds DataLoader instances so batching works per request
- Provides database and service clients without global imports
- Carries request metadata like headers and request ID
- Keeps resolvers decoupled from how the request was built
- Centralizes cross-cutting, request-scoped concerns
AI Mentor Explanation
Think of the shared team briefing folder handed to every fielder before a match: it holds the current match situation, the captain's plan, and who is on strike. Each fielder consults the same folder rather than radioing the dressing room. The context is that folder, prepared once per innings and available to every player making a decision on the field.
Step-by-Step Explanation
Step 1
Server builds context
A context function runs per request, reading the HTTP request (headers, cookies).
Step 2
Derive the user
Validate the auth token and attach the current user or session to the object.
Step 3
Attach shared clients
Add DataLoader instances, database clients, and service connections needed by resolvers.
Step 4
Pass into resolvers
GraphQL supplies the object as the third resolver argument (parent, args, context, info).
Step 5
Resolvers consume it
Resolvers read context.user or context.loaders instead of re-fetching or using globals.
What Interviewer Expects
- Knowing context is the third resolver argument
- That it is created once per request
- Common contents: user, DataLoaders, DB clients, request metadata
- That it is request-scoped, not global or per-field
- Its role in auth and batching
Common Mistakes
- Confusing context with resolver args or the parent object
- Putting business logic in the context function
- Sharing one context or DataLoader across multiple requests
- Forgetting context is where authentication data belongs
- Thinking context is per-field rather than per-request
Best Answer (HR Friendly)
“The context is a shared bundle of information created for each request and given to every resolver that handles it. It usually carries things like the logged-in user, database connections, and helpers for efficient data loading, so resolvers do not have to figure those out on their own.”
Code Example
const server = new ApolloServer({ typeDefs, resolvers });
await startStandaloneServer(server, {
context: async ({ req }) => {
const token = req.headers.authorization || '';
const user = await getUserFromToken(token);
return {
user,
db,
loaders: { userLoader: createUserLoader(db) },
};
},
});
const resolvers = {
Query: {
me: (parent, args, context) => {
if (!context.user) throw new Error('Not authenticated');
return context.user;
},
},
};Follow-up Questions
- Why are DataLoaders created per request inside context?
- How would you do authorization using the context?
- What is the difference between context and resolver arguments?
- Can context be modified partway through resolution, and should it be?
MCQ Practice
1. In a resolver signature (parent, args, context, info), what is context?
Context is built once per request and passed as the third argument to every resolver in that operation.
2. Which item is typically placed on the GraphQL context?
Context commonly holds request-scoped data such as the current user, DataLoader instances, and database clients.
Flash Cards
What is the GraphQL context? — A per-request object passed as the third argument to every resolver.
When is context created? — Once per request, typically by a server-side context function reading the HTTP request.
What commonly lives on context? — The authenticated user, DataLoaders, DB/service clients, and request metadata.
Why keep DataLoaders on context? — So batching and caching are scoped to a single request and not shared across users.