0
0
GraphQLquery~10 mins

Context setup in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Context setup
Start GraphQL Request
Initialize Context Object
Attach Context to Request
Resolver Accesses Context
Resolver Uses Context Data
Return Response with Context Data
This flow shows how a context object is created and passed to GraphQL resolvers during a request, allowing shared data access.
Execution Sample
GraphQL
const context = ({ req }) => {
  return { user: req.user };
};

// Resolver example
resolve(parent, args, context) {
  return context.user.id;
}
This code sets up a context function that extracts user info from the request and makes it available to resolvers.
Execution Table
StepActionInputContext StateResolver AccessOutput
1Receive request{ user: { id: 42 } }{}N/AN/A
2Initialize contextreq.user = { id: 42 }{ user: { id: 42 } }N/AN/A
3Pass context to resolvercontext = { user: { id: 42 } }{ user: { id: 42 } }context.user.id42
4Resolver returns dataN/A{ user: { id: 42 } }context.user.id42
5Send response42{ user: { id: 42 } }N/A{ data: 42 }
💡 Request completes after resolver returns user id from context.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
context{}{ user: { id: 42 } }{ user: { id: 42 } }{ user: { id: 42 } }
resolverOutputN/AN/A4242
Key Moments - 2 Insights
Why does the resolver receive the context object?
The resolver receives the context so it can access shared data like the authenticated user, as shown in step 3 of the execution_table.
What happens if the context is not initialized properly?
If context is empty or missing user data, resolvers cannot access needed info, causing errors or missing data, as seen if context stayed {} after step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of context after step 2?
A{ user: { id: 42 } }
B{}
Cundefined
Dnull
💡 Hint
Check the 'Context State' column at step 2 in the execution_table.
At which step does the resolver access the user id from context?
AStep 1
BStep 3
CStep 5
DStep 2
💡 Hint
Look at the 'Resolver Access' column in the execution_table.
If the request had no user info, what would context likely be after step 2?
A{ user: { id: 42 } }
B{ user: null }
C{}
Dundefined
💡 Hint
Context is initialized from request data; missing user means empty context as shown in variable_tracker.
Concept Snapshot
Context setup in GraphQL:
- Context is an object created per request
- It holds shared data like authenticated user
- Passed automatically to all resolvers
- Enables resolvers to access request-specific info
- Setup via a function returning context object
Full Transcript
In GraphQL, context setup means creating an object that holds shared data for each request, like the logged-in user. When a request comes in, the server runs a context function that extracts needed info from the request and returns it as the context object. This context is then passed to every resolver function. Resolvers use this context to access data like user ID or permissions. The execution flow starts with receiving the request, initializing context, passing it to resolvers, and finally returning the response. If context is missing or empty, resolvers cannot access necessary data, which can cause errors. This visual trace shows step-by-step how context is created and used during a GraphQL request.