0
0
GraphQLquery~20 mins

Context argument in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 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.