A gRPC service in NestJS where the contract comes first — src/proto/chat.proto is the source of truth, and both the server and the client in this repo are generated against it.
src/proto/chat.proto the contract
src/grpc-server/ @GrpcMethod controllers implementing it
src/grpc-client/ a typed client consuming the same proto
src/auth/auth.guard.ts guard applied before a handler runs
src/chat.dto.ts class-validator DTOs
src/validation.exception-filter.ts turns validation failures into proper gRPC errors
test/grpc.e2e-spec.ts end-to-end over a real gRPC channel
- Server and client in one repo, on purpose. Consuming your own contract is the fastest way to find out it is wrong — the client is the first integration test.
- Validation at the boundary.
class-validatorDTOs plus a dedicated exception filter, because a gRPC service that returnsUNKNOWNon bad input is unusable for whoever calls it. - Auth as a guard, not a helper call.
auth.guard.tsruns before the handler, so no method can forget it. - E2E against a real channel (
test/grpc.e2e-spec.ts), not a mocked transport — proto mismatches only show up over the wire.
NestJS 9 · @nestjs/microservices · @grpc/grpc-js · @grpc/proto-loader · class-validator + class-transformer · RxJS · Jest
pnpm install
pnpm start:dev # watch mode
pnpm test # unit
pnpm test:e2e # gRPC end-to-endComplete as a reference: a working gRPC server and client, guarded and validated, with tests. Not a product — the domain is deliberately a chat stub so the plumbing stays visible.