Bird
Raised Fist0
GraphQLquery~20 mins

Context argument in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Context Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the purpose of the context argument in GraphQL resolvers?

In GraphQL, resolvers receive a context argument. What is its main purpose?

ATo provide shared objects like authentication info or database connections to all resolvers during a request
BTo specify the query operation type such as query or mutation
CTo define the schema types and fields for the GraphQL API
DTo store the results of previous resolver calls for caching
Attempts:
2 left
💡 Hint

Think about what information needs to be accessible to all resolvers during a single request.

❓ query_result
intermediate
2:00remaining
What will be the output of this resolver using context?

Given the following resolver code snippet, what will getUserName return?

const resolvers = {
  Query: {
    getUserName: (parent, args, context) => {
      return context.user.name;
    }
  }
};

const context = { user: { id: 1, name: 'Alice' } };

// Query: { getUserName }
Aundefined
B"user"
Cnull
D"Alice"
Attempts:
2 left
💡 Hint

Look at what context.user.name contains.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this resolver using context

Which option contains a syntax error in accessing the context argument in a resolver?

const resolvers = {
  Query: {
    getUserEmail: (parent, args, context) => {
      return context.user.email;
    }
  }
};
AgetUserEmail: (parent, args) => { return context.user.email; }
BgetUserEmail: (parent, args, context) => { return context.user.email; }
CgetUserEmail: (parent, args, context) => { return context.user.email }
D} ;liame.resu.txetnoc nruter { ;tg&= )txetnoc ,sgra ,tnerap( :liamEresUteg
Attempts:
2 left
💡 Hint

Check if the context argument is declared properly.

❓ optimization
advanced
2:00remaining
How to optimize database access using context in GraphQL resolvers?

You want to avoid opening a new database connection in every resolver. How can you use the context argument to optimize this?

APass database connection as an argument in every query instead of context
BOpen a new database connection inside each resolver ignoring context
CCreate a single database connection in the context object and reuse it in all resolvers during the request
DStore database connection info in global variables outside context
Attempts:
2 left
💡 Hint

Think about sharing resources efficiently during a request.

🔧 Debug
expert
3:00remaining
Why does this resolver fail to access user info from context?

Consider this resolver:

const resolvers = {
  Query: {
    currentUser: (parent, args, context) => {
      return context.userInfo;
    }
  }
};

// Context setup:
const context = { user: { id: 5, name: 'Bob' } };

// Query: { currentUser }

Why does currentUser return null or undefined?

AThe resolver function signature is missing the <code>context</code> argument
BThe resolver tries to access <code>context.userInfo</code> but the context only has <code>user</code> property
CThe query is malformed and does not call <code>currentUser</code>
DThe context object is not passed to the GraphQL server
Attempts:
2 left
💡 Hint

Compare the property names used in the resolver and the context object.

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

  1. 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.
  2. Step 2: Compare with other options

    Defining schema types, specifying operation types, or formatting responses are handled elsewhere, not by context.
  3. Final Answer:

    To share common data like authentication or database access across all resolvers during a request -> Option D
  4. 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

  1. Step 1: Recall resolver function signature

    GraphQL resolvers receive arguments in the order: parent, args, context.
  2. Step 2: Match the correct order

    Only resolver(parent, args, context) { ... } matches this order correctly.
  3. Final Answer:

    resolver(parent, args, context) { ... } -> Option A
  4. Quick Check:

    Resolver args order = parent, args, context [OK]
Hint: Remember resolver args order: parent, args, context [OK]
Common Mistakes:
  • Swapping context and args positions
  • Omitting the parent argument
  • Using only two arguments instead of three
3. Given this resolver code snippet:
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' }?
medium
A. null
B. { id: 5, name: 'Alice' }
C. undefined
D. Error: getUserById is not a function

Solution

  1. Step 1: Understand the resolver call

    The resolver calls context.db.getUserById with args.id which is 5.
  2. Step 2: Check the return value from context

    Given context.db.getUserById(5) returns { id: 5, name: 'Alice' }, the resolver returns this object.
  3. Final Answer:

    { id: 5, name: 'Alice' } -> Option B
  4. Quick Check:

    Resolver returns context.db.getUserById(args.id) result [OK]
Hint: Resolver returns what context function returns for given args [OK]
Common Mistakes:
  • Assuming resolver returns null or undefined
  • Confusing parent argument with context
  • Expecting an error without checking context structure
4. Consider this resolver code:
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?
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

  1. Step 1: Analyze the error message

    The error says getUser is not a function on context, meaning context.getUser is undefined or not callable.
  2. 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.
  3. Final Answer:

    The context object does not have a getUser method -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Create context once per request including user info, then pass it to all resolvers -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard