0
0
GraphQLquery~10 mins

Context argument in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Context argument
Client sends GraphQL query
GraphQL Server receives query
Server creates Context object
Resolver functions called with Context
Resolvers use Context for shared data
Resolvers return data
Server sends response to Client
The context argument is created once per request and passed to all resolvers, allowing them to share data like authentication info or database connections.
Execution Sample
GraphQL
const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ req }) => ({ user: req.user })
});
This code creates a GraphQL server that passes the user info from the request into the context for all resolvers.
Execution Table
StepActionContext CreatedResolver CalledContext UsedResult
1Client sends queryNoNoNoWaiting for server
2Server receives queryNoNoNoPreparing to create context
3Create context from requestYes: { user: req.user }NoNoContext ready
4Call resolver AYesYesAccess context.userResolver A returns data with user info
5Call resolver BYesYesAccess context.userResolver B returns data with user info
6Send response to clientYesNoYesClient receives data
💡 All resolvers called with the same context object; response sent after all resolvers finish.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
contextundefined{ user: req.user }{ user: req.user }{ user: req.user }{ user: req.user }
resolverResultAundefinedundefineddata with user infodata with user infodata with user info
resolverResultBundefinedundefinedundefineddata with user infodata with user info
Key Moments - 2 Insights
Why is the context created only once per request and not per resolver?
Because the context is shared across all resolvers for the same request to provide consistent data like authentication info, as shown in execution_table steps 3 to 5.
How do resolvers access the context argument?
Resolvers receive the context as a third argument and can access its properties directly, as seen in execution_table steps 4 and 5 where context.user is used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the context object created?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Check the 'Context Created' column in execution_table rows.
According to variable_tracker, what is the value of 'context' after Step 5?
Aundefined
B{ user: req.user }
Cnull
D{}
💡 Hint
Look at the 'context' row and the 'After Step 5' column in variable_tracker.
If the context was created separately for each resolver, how would the execution_table change?
AContext Created would be 'No' at all steps
BContext Created would be 'Yes' only at Step 3
CContext Created would be 'Yes' at Steps 4 and 5 separately
DContext Created would be 'Yes' only at Step 6
💡 Hint
Think about when context is created and used in execution_table steps 3, 4, and 5.
Concept Snapshot
Context argument in GraphQL:
- Created once per request
- Passed to all resolvers
- Holds shared data (e.g., user info)
- Accessed as third argument in resolvers
- Enables consistent data access across resolvers
Full Transcript
In GraphQL, the context argument is an object created once for each client request. It is passed to every resolver function during that request. This allows resolvers to share common data like authentication details or database connections. The server creates the context after receiving the query and before calling any resolvers. Each resolver can then access the context to get needed information. After all resolvers finish, the server sends the response back to the client. This flow ensures consistent and secure data handling across the entire query execution.