0
0
GraphQLquery~20 mins

Context setup in GraphQL - Practice Problems & Coding Challenges

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

In GraphQL, what is the main role of the context object during query execution?

AIt caches query results to improve performance automatically.
BIt defines the schema types and fields for the GraphQL server.
CIt stores shared data like authentication info accessible to all resolvers during a request.
DIt handles client-side state management for UI components.
Attempts:
2 left
💡 Hint

Think about what data resolvers need to share during a single request.

query_result
intermediate
2:00remaining
What is the output when accessing context in a resolver?

Given this resolver snippet, what will be the value of userId?

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

// Context setup:
const context = { userId: 42 };
Anull
B42
CThrows an error
Dundefined
Attempts:
2 left
💡 Hint

Look at what the context object contains and what the resolver returns.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in context setup

Which option contains a syntax error in setting up the GraphQL context function?

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ???
});
A{ user: getUser() }
B() => { return { user: getUser() }; }
Ccontext() { return { user: getUser() }; }
Dasync () => { const user = await getUser(); return { user }; }
Attempts:
2 left
💡 Hint

The context option expects a function, not a plain object.

optimization
advanced
2:00remaining
Best practice for expensive context data

You want to add a database connection to the context for each request. Which approach is best to avoid performance issues?

ACreate a new database connection inside the context function for every request.
BReuse a single global database connection shared across all requests in the context.
CCreate a new database connection inside each resolver instead of context.
DCreate a new database connection once and store it in a global variable, then use it in context.
Attempts:
2 left
💡 Hint

Think about connection overhead and sharing resources safely.

🔧 Debug
expert
2:00remaining
Why does context data not appear in resolvers?

You set up context as context: () => ({ user: null }). But in resolvers, context.user is always undefined. What is the cause?

AThe resolver signature is missing the context parameter.
BThe context function is not called because it is missing parentheses in the server setup.
CThe context function is asynchronous but not awaited, causing <code>undefined</code> values.
DThe context function returns an object with <code>user</code> set to null, so <code>undefined</code> is impossible.
Attempts:
2 left
💡 Hint

Check how the resolver accesses context.