Context and guards — GraphQL · NestJS

Protect the schema like any Nest API: the HTTP request that Nest's Apollo driver puts in every resolver's context, a guard and a parameter decorator that read it through GqlExecutionContext, Nest's exceptions as GraphQL errors, and formatError keeping an internal failure's details on the server.

What you will learn

Read the theory for Context and guards

All GraphQL lessons

All NestJS courses

loading types…

What you'll learn

  • Find the HTTP request in the GraphQL context, where Nest's Apollo driver puts it as req, and extend the context with the context option
  • Turn Nest's ExecutionContext into a GqlExecutionContext in guards and custom decorators
  • Guard single queries and mutations with @UseGuards, and read the refusal as a FORBIDDEN error
  • Shape what errors tell clients with formatError, so that an internal failure's message stays on the server