Files
What you'll learn
- Declare what a client may send with @InputType() classes, and take them as a mutation's argument
- Write @Mutation() resolvers that change state and return what changed
- Validate input types with the global ValidationPipe and class-validator, and read a refusal as a GraphQL error
- Derive an update input with PartialType from @nestjs/graphql, which keeps the fields' GraphQL types and their validation
Press Run tests to start the app. Its log appears here.Graded endpoints
createCat takes an input object and returns the new cat, with the fields the mutation selects
The global ValidationPipe checks the input class and refuses it; the error keeps Nest's code and the list of constraints that failed
GraphQL itself refuses an input without breed, before the pipe or the resolver: its type is String!
UpdateCatInput has every field optional: only the nickname changes, through variables
PartialType kept the create input's validation: a negative age is refused on update too
The service's NotFoundException becomes a GraphQL error carrying the 404, while the HTTP response stays 200
removeCat answers a Boolean
Tom removed, Luna renamed, Oscar added; the refused mutations changed nothing
PartialType made every field of UpdateCatInput optional while keeping its type: age is still an Int