GraphQL context is used in real applications to pass authentication and other request-specific data to resolvers, enabling personalized and secure data access.
💼 Career
Understanding GraphQL context setup is essential for backend developers working with GraphQL APIs, especially in roles involving user authentication and authorization.
Progress0 / 4 steps
1
Define the GraphQL schema with a currentUser query
Create a GraphQL schema string called typeDefs that defines a Query type with a currentUser field returning a User type. Also define the User type with a name field of type String.
GraphQL
Hint
Define type User with a name field, and type Query with currentUser: User.
2
Create a context function that adds a user object
Create a function called context that takes a { req } parameter and returns an object with a user property set to { name: 'Alice' } to simulate an authenticated user.
GraphQL
Hint
Return an object with user set to { name: 'Alice' } inside the context function.
3
Write resolver for currentUser using context
Create a resolvers object with a Query field. Inside Query, write a currentUser resolver function that takes parent, args, and context parameters and returns context.user.
GraphQL
Hint
Return context.user from the currentUser resolver function.
4
Complete server setup with schema, resolvers, and context
Create an ApolloServer instance called server using typeDefs, resolvers, and context. Use new ApolloServer({ typeDefs, resolvers, context }).
GraphQL
Hint
Pass typeDefs, resolvers, and context to the ApolloServer constructor.
Practice
(1/5)
1. What is the main purpose of the context in a GraphQL server?
easy
A. To share data like user info or database clients across all resolvers
B. To define the GraphQL schema types
C. To write the query resolvers
D. To format the GraphQL query response
Solution
Step 1: Understand what context provides
Context is an object that runs once per request and is accessible in every resolver, allowing shared data like user info or database clients.
Step 2: Compare with other options
Defining schema types, writing resolvers, and formatting responses are separate tasks not handled by context.
Final Answer:
To share data like user info or database clients across all resolvers -> Option A
Quick Check:
Context shares data across resolvers [OK]
Hint: Context shares common data across resolvers per request [OK]
Common Mistakes:
Confusing context with schema definition
Thinking context formats responses
Assuming context writes resolver logic
2. Which of the following is the correct way to define a context function in Apollo Server?
easy
A. context = { user: getUser() }
B. context: () => ({ user: getUser() })
C. context: { user: getUser() }
D. context() { return user: getUser() }
Solution
Step 1: Recall Apollo Server context syntax
Context is defined as a function returning an object, e.g., context: () => ({ user: getUser() }).
Step 2: Identify syntax errors in other options
context = { user: getUser() } uses assignment instead of a function, C uses an object directly without a function, and D has invalid syntax missing parentheses and braces.
Final Answer:
context: () => ({ user: getUser() }) -> Option B
Quick Check:
Context is a function returning an object [OK]
Hint: Context must be a function returning an object [OK]
What will be the value of context.user.name inside a resolver?
medium
A. undefined
B. 5
C. null
D. 'Alice'
Solution
Step 1: Analyze the context object
The context function returns an object with a user property containing { id: 5, name: 'Alice' }.
Step 2: Access user.name in resolver
Accessing context.user.name will give the string 'Alice' as defined.
Final Answer:
'Alice' -> Option D
Quick Check:
context.user.name = 'Alice' [OK]
Hint: Context returns user object; access properties directly [OK]
Common Mistakes:
Confusing user id with name
Assuming context is undefined
Expecting null instead of string
4. You wrote this context function:
context: () => { user: getUser() }
Why does context.user return undefined in resolvers?
medium
A. Because user is not a valid property
B. Because getUser() is undefined
C. Because the function does not return the object explicitly
D. Because context must be an object, not a function
Solution
Step 1: Check function return behavior
The function uses curly braces but lacks a return statement, so it returns undefined by default.
Step 2: Understand impact on context
Since nothing is returned, context.user is undefined in resolvers.
Final Answer:
Because the function does not return the object explicitly -> Option C
Quick Check:
Missing return in function = undefined context [OK]
Hint: Use return or parentheses to return object in arrow function [OK]
Common Mistakes:
Omitting return in arrow function with braces
Assuming getUser() is undefined without checking
Thinking context must be an object, not function
5. You want to add a database client and user info to context for each request. Which is the best way to do this in Apollo Server?
hard
A. context: async () => { const db = await connectDB(); const user = await getUser(); return { db, user }; }
B. context: { db: connectDB(), user: getUser() }
C. context: () => { db: connectDB(); user: getUser(); }
D. context: () => ({ db: connectDB(), user: getUser() })
Solution
Step 1: Understand async context needs
Connecting to a database and fetching user info are asynchronous operations, so context must be async and await these calls.
Step 2: Evaluate options for async correctness
context: async () => { const db = await connectDB(); const user = await getUser(); return { db, user }; } correctly uses async function and awaits results before returning an object. context: { db: connectDB(), user: getUser() } is a plain object, not a function. context: () => { db: connectDB(); user: getUser(); } lacks return and awaits. context: () => ({ db: connectDB(), user: getUser() }) returns an object but does not await async calls.
Final Answer:
context: async () => { const db = await connectDB(); const user = await getUser(); return { db, user }; } -> Option A
Quick Check:
Async context with awaited data [OK]
Hint: Use async function and await for async data in context [OK]
Common Mistakes:
Not awaiting async calls in context
Using plain object instead of function
Missing return statement in function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.