Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRPC client interceptor does not get called #9079

Closed
3 of 15 tasks
ckfngod opened this issue Feb 1, 2022 · 8 comments
Closed
3 of 15 tasks

GRPC client interceptor does not get called #9079

ckfngod opened this issue Feb 1, 2022 · 8 comments
Labels
needs triage This issue has not been looked into

Comments

@ckfngod
Copy link

ckfngod commented Feb 1, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

GRPC client interceptor not called when passed through ClientsModule options but is working when passed through options.channelOptions:

This does not work:

ClientsModule.register([
  {
    name: 'HERO_PACKAGE',
    transport: Transport.GRPC,
    options: {
      package: 'hero',
      protoPath: join(__dirname, 'hero.proto'),
      interceptors: [
        (options, nextCall) => {
          console.log('CALLED INTERCEPTOR');
          return new InterceptingCall(nextCall(options));
        },
      ],
    },
  },
])

This is working:

ClientsModule.register([
  {
    name: 'HERO_PACKAGE',
    transport: Transport.GRPC,
    options: {
      package: 'hero',
      protoPath: join(__dirname, 'hero.proto'),
      channelOptions: {
        interceptors: [
          (options, nextCall) => {
            console.log('CALLED INTERCEPTOR');
            return new InterceptingCall(nextCall(options));
          },
        ],
      },
    },
  },
])

Minimum reproduction code

https://github.com/ckfngod/grpc-client-interceptor-issue

Steps to reproduce

  1. npm i
  2. npm run start
  3. curl localhost:3000

GRPC request is successful but there is no console output from application when interceptor should be logging.

Expected behavior

GRPC client interceptor should be called.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

8.2.6

Packages versions

[System Information]
OS Version : macOS Monterey
NodeJS Version : v16.13.2
NPM Version : 8.1.2

[Nest CLI]
Nest CLI Version : 8.2.0

[Nest Platform Information]
platform-express version : 8.2.6
microservices version : 8.2.6
schematics version : 8.0.5
testing version : 8.2.6
common version : 8.2.6
core version : 8.2.6
cli version : 8.2.0

Node.js version

16.13.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@ckfngod ckfngod added the needs triage This issue has not been looked into label Feb 1, 2022
@dacdodinh99
Copy link

I had the exact same issue. Does anyone know how to solve this?

@vietkute02
Copy link

This error happen because interceptors option not pass to grpc-js client

https://github.com/nestjs/nest/blob/master/packages/microservices/client/client-grpc.ts#L65

@tuananhlai
Copy link

It's so weird, we already have an PR to expose interceptors option for gRPC but it doesn't get passed to grpc-js client?

#8231

@kamilmysliwiec
Copy link
Member

Yeah, it turns out #8231 was an invalid PR that exposed interceptors option in the wrong place. You should pass interceptors as part of the channelOptions object, as in this example:

ClientsModule.register([
  {
    name: 'HERO_PACKAGE',
    transport: Transport.GRPC,
    options: {
      package: 'hero',
      protoPath: join(__dirname, 'hero.proto'),
      channelOptions: {
        interceptors: [
          (options, nextCall) => {
            console.log('CALLED INTERCEPTOR');
            return new InterceptingCall(nextCall(options));
          },
        ],
      },
    },
  },
])

#8231 will be reverted

@leonidasv
Copy link

leonidasv commented Mar 29, 2022

@kamilmysliwiec channelOptions need a proper typing in order to support this:

TS2322: Type '(options: any, nextCall: any) => any' is not assignable to type 'string'.

image

@nekooei
Copy link

nekooei commented Apr 6, 2022

@kamilmysliwiec channelOptions need a proper typing in order to support this:

TS2322: Type '(options: any, nextCall: any) => any' is not assignable to type 'string'.

image

Yes, I have same problem

@hyunjinjeong
Copy link

hyunjinjeong commented Apr 20, 2022

We can use it with type casting as a workaround.

const options = {
  ...
} as unknown as GrpcOptions

// or more specifically
const options = {
  options: {
    channelOptions: {
      interceptors: [SomeInterceptor],
    } as unknown as GrpcOptions['options']['channelOptions']
  }
}

We need a proper typing though...

@kamilmysliwiec
Copy link
Member

Let's track this here #9587

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

8 participants