GraphQL
Serve the cats API as a GraphQL schema, code first: object types and resolvers, arguments and field resolvers, mutations with validated inputs, enums, scalars, interfaces and unions, guards on the GraphQL context, field middleware and Apollo plugins, subscriptions, and tests that query the schema.
- lessons
- 9
- level
- advanced
- time
- 4.5 hours
The schema
Types and queries generated from decorated classes, fields resolved on demand, and mutations with validated inputs.
- 1Your first query
Serve the cats as a GraphQL schema: the Apollo driver generating the schema from decorated classes, an object type whose fields say exactly what they are, and a resolver with two queries.
Read the theory - 2Resolvers and arguments
Let the graph connect: a cat's owner and an owner's cats resolved only when a query asks for them, and the cats query taking a filter and paging through a class of arguments with defaults.
Read the theory - 3Mutations
Change the cats through the schema: input types that describe what a client may send, validated by class-validator, a partial update derived from the create input, and Nest's exceptions as GraphQL errors.
Read the theory
Types
Values with their real types, and fields that can hold more than one kind of object.
- 4Enums and scalars
Give values their real types: a registered enum the schema lists and checks, dates that travel as ISO strings and arrive as Date objects, and a custom Microchip scalar that validates what clients send and formats what they get.
Read the theory - 5Interfaces and unions
Fields that hold more than one kind of object: an Animal interface that cats and dogs implement, with a field resolved once for both, a search that answers a union of cats, dogs and owners, and the resolveType functions that tell plain objects apart.
Read the theory
Around the resolvers
The request behind a query, who may run what, and code that runs around fields and operations.
- 6Context and guards
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.
Read the theory - 7Field middleware and plugins
Code that runs around a single field and around a whole request: a field middleware that masks a microchip for anyone who is not staff, and an Apollo plugin, a Nest provider with its dependencies, that names every operation in a response header and a log.
Read the theory
Beyond queries
Events pushed to subscribers, and tests that exercise the schema.
- 8Subscriptions
Push changes to clients as they happen: a PubSub provider, mutations that publish, a subscription that filters what each subscriber receives and one that turns a payload into a sentence, graded by specs that subscribe through the schema.
Read the theory - 9Testing a GraphQL API
Write the end-to-end tests of the cats schema: a fresh application per test, queries and mutations posted to /graphql with supertest, answers compared as data and errors as codes, until four bugs planted in the code are all caught.
Read the theory