Files
main.tsmain
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
Press Run tests to start the app. Its log appears here.Graded endpoints
No staff headers: the decorator answers guest, and public queries work
StaffGuard refuses: Nest's ForbiddenException arrives as a FORBIDDEN error with its message, and the cat stays
A name is not enough: the guard checks the key
The guard read the header through GqlExecutionContext and let the mutation through
Guarded too: a query can be refused like a mutation
The decorator gave removeCat the staff member's name from the request
The vet's database is down; formatError keeps the connection string and the user name on the server and tells the client only that something failed
An HttpException keeps its message and status: formatError masks only the failures Nest did not describe