Thuta Learning
GraphQL
AdvancedWeb Developmentbeginner

Resolvers Deep Dive and Context

What you'll walk away with

  • Explain the core ideas behind Resolvers Deep Dive and Context
  • Run the sample GraphQL query or code and verify its output
  • Apply the technique correctly to the Tutorial Platform and production scenarios

Build the mental model

Every resolver function can accept four arguments: `parent` (the resolved value of the parent field), `args` (the field's arguments), `context` (a shared object for the whole request — the db connection, the current user), and `info` (execution metadata, rarely used directly). `context` is built once per request inside Apollo Server's `context` function and passed to every resolver, giving you one place to manage database connection pooling and the current authenticated user.

Connect it to a real scenario

The Tutorial Platform's `Context` interface holds `db` (a typed database client) and `userId` (a string decoded from a token, or null). The `Query.tutorial` resolver calls `context.db.tutorial.findUnique(...)`, so every resolver reuses the same database-access pattern consistently.

Try the working example

typescript
interface Context {
  db: Database;
  userId: string | null;
}

const resolvers = {
  Query: {
    tutorial: (
      _parent: unknown,
      args: { id: string },
      context: Context,
    ) => context.db.tutorial.findUnique({ where: { id: args.id } }),
  },
};

const server = new ApolloServer<Context>({ typeDefs, resolvers });
await startStandaloneServer(server, {
  context: async ({ req }) => ({
    db,
    userId: getUserIdFromToken(req.headers.authorization),
  }),
});
You should see
You can distinguish the four resolver arguments and write a typed context object.

5-minute try-it

Write how the `Tutorial.author` resolver would use the `parent` argument to read the tutorial's authorId.

One important caution

Opening a fresh database connection inside a resolver function on every call can open many connections per request — always reuse the shared connection or pool from context.

Apollo Server — ResolversGraphQL

Easy traps

  • Opening a fresh database connection inside a resolver function on every call can open many connections per request — always reuse the shared connection or pool from context.
  • Validate sample queries and mutations on a local or test server with recoverable data before applying them to production.

Exercise

Write how the `Tutorial.author` resolver would use the `parent` argument to read the tutorial's authorId.

You'll know it worked when: You can distinguish the four resolver arguments and write a typed context object.

Resolvers Deep Dive and Context | Thuta Learning