The context argument is an object passed to every resolver in a GraphQL server. It holds shared data like authentication info, database connections, or user details that resolvers can access.
Click to reveal answer
beginner
Why do we use the context argument instead of passing data directly to resolvers?
Using context avoids repeating the same data in every resolver call. It provides a single place to store shared info, making code cleaner and easier to maintain.
Click to reveal answer
intermediate
How is the context argument typically created in a GraphQL server?
The context is usually created once per request. It can include user authentication info, database clients, or other services needed by resolvers during that request.
Click to reveal answer
beginner
Give an example of data you might store in the context argument.
You might store the current user's ID, their roles or permissions, a database connection object, or a logger instance in the context.
Click to reveal answer
intermediate
Can the context argument change during a single GraphQL request?
No, the context is created once per request and remains the same for all resolvers during that request to ensure consistent data access.
Click to reveal answer
What is the main purpose of the context argument in GraphQL?
ATo store query results
BTo define the GraphQL schema
CTo format the response data
DTo share data like authentication info across all resolvers
✗ Incorrect
The context argument shares common data such as authentication info with all resolvers during a request.
When is the context argument created in a GraphQL server?
AOnce per GraphQL request
BOnce per server startup
COnce per resolver call
DOnce per schema definition
✗ Incorrect
The context is created once for each request to provide consistent data to all resolvers handling that request.
Which of the following is NOT typically stored in the context argument?
AUser authentication info
BDatabase connection
CGraphQL query string
DLogger instance
✗ Incorrect
The GraphQL query string is usually passed separately, not stored in context.
How do resolvers access the context argument?
AAs the third argument
BAs the first argument
CAs the second argument
DAs the fourth argument
✗ Incorrect
Resolvers receive (parent, args, context, info) where context is the third argument.
Why is it better to use context instead of global variables for shared data?
AGlobal variables are recommended for security
BContext is request-specific and avoids conflicts between users
CContext is harder to use
DGlobal variables are faster
✗ Incorrect
Context is created per request, so it keeps data isolated per user and avoids conflicts.
Explain what the context argument is in GraphQL and why it is useful.
Think about how multiple resolvers can access the same info without repeating it.
You got /3 concepts.
Describe how you would use the context argument to handle user authentication in a GraphQL server.
Consider how to get user data from a request and make it available to all resolvers.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the context argument in GraphQL resolvers?
easy
A. To format the response data before sending it to the client
B. To define the GraphQL schema types
C. To specify the query operation type (query, mutation, subscription)
D. To share common data like authentication or database access across all resolvers during a request
Solution
Step 1: Understand the role of context in GraphQL
The context argument is designed to hold data shared by all resolvers during a single request, such as authentication info or database connections.
Step 2: Compare with other options
Defining schema types, specifying operation types, or formatting responses are handled elsewhere, not by context.
Final Answer:
To share common data like authentication or database access across all resolvers during a request -> Option D
Quick Check:
Context = Shared data for resolvers [OK]
Hint: Context holds shared info for all resolvers in a request [OK]
Common Mistakes:
Confusing context with schema definition
Thinking context formats responses
Assuming context defines operation types
2. Which of the following is the correct way to access the context argument in a GraphQL resolver function?
easy
A. resolver(parent, args, context) { ... }
B. resolver(context, args, parent) { ... }
C. resolver(args, parent, context) { ... }
D. resolver(args, context) { ... }
Solution
Step 1: Recall resolver function signature
GraphQL resolvers receive arguments in the order: parent, args, context.
Step 2: Match the correct order
Only resolver(parent, args, context) { ... } matches this order correctly.
Final Answer:
resolver(parent, args, context) { ... } -> Option A
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.