Skip to content

Commit 2d43719

Browse files
fix(sample): use clients module in the grpc sample
1 parent 8f04b6a commit 2d43719

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

sample/04-grpc/src/hero/hero.controller.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Controller, Get, OnModuleInit } from '@nestjs/common';
2-
import { Client, ClientGrpc, GrpcMethod } from '@nestjs/microservices';
1+
import { Controller, Get, Inject, OnModuleInit } from '@nestjs/common';
2+
import { ClientGrpc, GrpcMethod } from '@nestjs/microservices';
33
import { Observable } from 'rxjs';
4-
import { grpcClientOptions } from '../grpc-client.options';
54
import { HeroById } from './interfaces/hero-by-id.interface';
65
import { Hero } from './interfaces/hero.interface';
76

@@ -11,11 +10,10 @@ interface HeroService {
1110

1211
@Controller()
1312
export class HeroController implements OnModuleInit {
14-
@Client(grpcClientOptions)
15-
private readonly client: ClientGrpc;
16-
1713
private heroService: HeroService;
1814

15+
constructor(@Inject('HERO_PACKAGE') private readonly client: ClientGrpc) {}
16+
1917
onModuleInit() {
2018
this.heroService = this.client.getService<HeroService>('HeroService');
2119
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { Module } from '@nestjs/common';
2+
import { ClientsModule } from '@nestjs/microservices';
3+
import { grpcClientOptions } from '../grpc-client.options';
24
import { HeroController } from './hero.controller';
35

46
@Module({
7+
imports: [
8+
ClientsModule.register([
9+
{
10+
name: 'HERO_PACKAGE',
11+
...grpcClientOptions,
12+
},
13+
]),
14+
],
515
controllers: [HeroController],
616
})
717
export class HeroModule {}

0 commit comments

Comments
 (0)