Skip to main content

gRPC

When using a hybrid nestjs app the interceptors won't run on your grpc service out of the box. To fix this use the inheritAppConfig option when connecting your microservice.

src/main.ts
  app.connectMicroservice<MicroserviceOptions>(
{
transport: Transport.GRPC,
options: {
package: 'hero',
protoPath: join(__dirname, 'app/hero.proto'),
},
},
+ { inheritAppConfig: true },
);
tip

Don't forget to add the @Payload() to your data argument inside the controller. Otherwise nestjs fails to pass down your data correctly.

src/controllers/hero.controller.ts
@GrpcMethod('HeroesService', 'FindOne')
findOne(@Payload() data: HeroById, @I18n() i18n: I18nContext): Hero {
const items = [
{
id: 1,
name: i18n.t('test.set-up-password.heading', {
args: { username: 'John' },
}),
},
{ id: 2, name: 'Doe' },
];
return items.find(({ id }) => id === data.id);
}

After that use the following GrpcMetadataResolver resolver.

src/app.module.ts
  I18nModule.forRoot({
fallbackLanguage: 'en',
loaderOptions: {
path: path.join(__dirname, '/i18n/'),
watch: true,
},
resolvers: [
+ GrpcMetadataResolver
],
})