Context argument in GraphQL - Time & Space Complexity
When using the context argument in GraphQL, it helps pass shared data to resolvers. We want to understand how this affects the time it takes to run queries.
Specifically, how does the use of context change the work done as the input grows?
Analyze the time complexity of the following GraphQL resolver using context.
const resolvers = {
Query: {
user: (parent, args, context) => {
return context.db.findUserById(args.id);
}
}
};
This resolver uses the context to access a database helper to find a user by ID.
Look for repeated work inside the resolver.
- Primary operation: Searching the database for a user by ID.
- How many times: Once per query execution.
As the number of users in the database grows, the search takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of users to check.
Time Complexity: O(n)
This means the time to find a user grows in a straight line as the database size grows.
[X] Wrong: "Using context makes the query run instantly no matter the data size."
[OK] Correct: Context just passes shared data; the actual search still depends on how big the data is.
Understanding how context affects query time helps you explain real-world GraphQL performance clearly and confidently.
What if the database used an index to find users by ID? How would the time complexity change?
Practice
context argument in GraphQL resolvers?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 DQuick Check:
Context = Shared data for resolvers [OK]
- Confusing context with schema definition
- Thinking context formats responses
- Assuming context defines operation types
context argument in a GraphQL resolver function?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 AQuick Check:
Resolver args order = parent, args, context [OK]
- Swapping context and args positions
- Omitting the parent argument
- Using only two arguments instead of three
const resolver = (parent, args, context) => {
return context.db.getUserById(args.id);
};What will
resolver(null, {id: 5}, context) return if context.db.getUserById(5) returns { id: 5, name: 'Alice' }?Solution
Step 1: Understand the resolver call
The resolver callscontext.db.getUserByIdwithargs.idwhich is 5.Step 2: Check the return value from context
Givencontext.db.getUserById(5)returns{ id: 5, name: 'Alice' }, the resolver returns this object.Final Answer:
{ id: 5, name: 'Alice' } -> Option BQuick Check:
Resolver returns context.db.getUserById(args.id) result [OK]
- Assuming resolver returns null or undefined
- Confusing parent argument with context
- Expecting an error without checking context structure
const resolver = (parent, args, context) => {
return context.getUser(args.id);
};If running this code causes
TypeError: context.getUser is not a function, what is the most likely cause?Solution
Step 1: Analyze the error message
The error saysgetUseris not a function oncontext, meaningcontext.getUseris undefined or not callable.Step 2: Check other options
Undefinedargs.idor 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 AQuick Check:
TypeError means missing method on context [OK]
- Blaming args.id for method not found error
- Assuming parent argument causes this error
- Ignoring that context must have the method
context argument. Which approach correctly sets up the context for this purpose?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 CQuick Check:
Context = single shared object per request [OK]
- Duplicating user info in every resolver
- Passing sensitive info as query arguments
- Using global variables causing data leaks
Ready to prove it?
Pass the test and this pattern moves unknown
