Abdessalem Letaief
Abdessalem Letaief

Reputation: 155

how to create NestJs grpc client

am trying to create a NestJs gRPC client on docker env, but i always get this error when compiling

[Nest] 1076   - 2020-05-19 23:59:34   [ClientProxy] The invalid gRPC package (package not found)
Error: The invalid gRPC package (package not found)
    at ClientGrpcProxy.createClients (/app/node_modules/@nestjs/microservices/client/client-grpc.js:188:45)
    at new ClientGrpcProxy (/app/node_modules/@nestjs/microservices/client/client-grpc.js:26:33)
    at Function.create (/app/node_modules/@nestjs/microservices/client/client-proxy-factory.js:22:24)
    at clients.map.item (/app/node_modules/@nestjs/microservices/module/clients.module.js:11:51)
    at Array.map (<anonymous>)
    at Function.register (/app/node_modules/@nestjs/microservices/module/clients.module.js:9:41)
    at Object.<anonymous> (/app/dist/src/modules/orders/orders.module.js:31:43)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
/app/node_modules/@nestjs/microservices/client/client-grpc.js:190
                throw invalidPackageError;
                ^

Error: The invalid gRPC package (package not found)
    at ClientGrpcProxy.createClients (/app/node_modules/@nestjs/microservices/client/client-grpc.js:188:45)
    at new ClientGrpcProxy (/app/node_modules/@nestjs/microservices/client/client-grpc.js:26:33)
    at Function.create (/app/node_modules/@nestjs/microservices/client/client-proxy-factory.js:22:24)
    at clients.map.item (/app/node_modules/@nestjs/microservices/module/clients.module.js:11:51)
    at Array.map (<anonymous>)
    at Function.register (/app/node_modules/@nestjs/microservices/module/clients.module.js:9:41)
    at Object.<anonymous> (/app/dist/src/modules/orders/orders.module.js:31:43)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)

i have already tried reinstalling the packages (gRPC, @grpc/proto-loader and @nestjs/microservices),

I get this problem when trying with both methods provided in docs

First Method with @Client decorator

    @Client({
        transport: Transport.GRPC,
        options: {
          package: 'app',
          protoPath: join(__dirname, '../../../../../../src/modules/orders/proto/app.proto'),
        },
    })

    private client : ClientGrpc

    private grpcService: IGrpcService;

    onModuleInit(){
       this.grpcService= this.client.getService<IGrpcService>('AppController')
    }

Second Method using ClientModule in appmodule.ts

ClientsModule.register([{
            name:'TEST',
            transport: Transport.GRPC,
            options: {
              package: 'app',
              protoPath: join(__dirname, '../../../../src/modules/orders/proto/app.proto'),
            },
          }])

Proto file

syntax = "proto3";

package role;

service RoleService {
    rpc CheckPermission (StringMessage) returns (BooleanPayload) {}
    rpc AddPolicy (StringMessage) returns (BooleanPayload) {}
}

message StringMessage {
    repeated string params = 1;
}

message Role {
    string id = 1;
    string codeName = 2;
    string label = 3;
    string createdAt = 4;
    string updatedAt = 5;
}

message BooleanPayload {
    bool success = 1;
}

Upvotes: 2

Views: 13530

Answers (2)

Chuong Tran
Chuong Tran

Reputation: 3441

Follow question at here. Found a sample

Also, see more at a document gRPC client

Proto file:

syntax = "proto3";

package hero;

service HeroService {
  rpc FindOne (HeroById) returns (Hero) {}
}

message HeroById {
  int32 id = 1;
}

message Hero {
  int32 id = 1;
  string name = 2;
}

Controller:

import { Controller, OnModuleInit, Get, Param } from '@nestjs/common';
import {
  GrpcMethod,
  ClientGrpc,
  Client,
  Transport,
} from '@nestjs/microservices';
import { HeroById } from './interfaces/hero-by-id.interface';
import { Hero } from './interfaces/hero.interface';
import { Observable } from 'rxjs';
import { join } from 'path';
import { grpcClientOptions } from '../grpc-hero.options';

interface HeroService {
  findOne(data: { id: number }): Observable<any>;
}

@Controller('hero')
export class HeroController implements OnModuleInit {
  @Client(grpcClientOptions) private readonly client: ClientGrpc;
  private heroService: HeroService;

  onModuleInit() {
    this.heroService = this.client.getService<HeroService>('HeroService');
  }

  @Get(':id')
  call(@Param() params): Observable<any> {
    return this.heroService.findOne({ id: +params.id });
  }
}

Upvotes: 4

Abdessalem Letaief
Abdessalem Letaief

Reputation: 155

I have fixed it, the problem was with my proto file Package and service, i've changed it to the name of the controller in using it in.

Upvotes: 4

Related Questions