Kind: Function
Source: packages/microservices/decorators/message-pattern.decorator.ts
Part of: Microservices
GrpcMethod() marks a controller method as a handler for a gRPC service RPC. It stores the service and method metadata used by the microservice transport to route incoming gRPC requests to the decorated method.
Signature
tsfunction GrpcMethod(service: string | undefined, method: string): MethodDecorator
Parameters
| Name | Type |
|---|---|
service | `string |
method | string |
Returns: MethodDecorator
Diagram
mermaidgraph LR Client[gRPC Client] --> Server[gRPC Microservice Server] Server --> Service[Proto Service and RPC] Service --> Decorator["@GrpcMethod('UsersService', 'FindOne')"] Decorator --> Handler[Controller Handler Method] Handler --> Response[gRPC Response]
Usage
tsimport { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
interface FindUserRequest {
id: string;
}
interface User {
id: string;
name: string;
}
@Controller()
export class UsersController {
@GrpcMethod('UsersService', 'FindOne')
findOne(request: FindUserRequest): User {
return {
id: request.id,
name: 'Ada Lovelace',
};
}
}
AI Coding Instructions
- Match the
serviceand RPC method names with the definitions in the loaded.protofile. - Decorate methods in a controller that is registered with the gRPC microservice module.
- Keep handler request and response shapes compatible with the generated protobuf contract.
- Use the explicit method argument when the TypeScript method name differs from the proto RPC name.
- Do not use this decorator for HTTP routes; use it only for handlers served by a gRPC transport.
Was this page helpful?