Skip to content

Configuration for microservice in hybrid application #4261

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

Closed
2Kable opened this issue Mar 10, 2020 · 11 comments
Closed

Configuration for microservice in hybrid application #4261

2Kable opened this issue Mar 10, 2020 · 11 comments

Comments

@2Kable
Copy link
Contributor

2Kable commented Mar 10, 2020

Feature Request

related to: #4112

Is your feature request related to a problem? Please describe.

We are running NestJS container that run a HTTP/GraphQL api, but also process GRPC, Queuing requests, in the same nestjs instance

I would like to run grpc alongside the graphql/http instance, and being able to intercept all request to log them, whether they are from GRPC or HTTP, using app.useGlobalInterceptors

const app = await NestFactory.create(AppModule, { logger })
app.useGlobalInterceptors(LoggerInterceptor)
app.connectMicroservice({
  transport: Transport.GRPC,
  options: {}
})

Describe the solution you'd like

We fixed it by patching nest source code and passing the NestApplication config in the NestMicroservice constructor

- const applicationConfig = new ApplicationConfig();
- const instance = new NestMicroservice(this.container, options, applicationConfig);
+ const instance = new NestMicroservice(this.container, options, this.config);

But this would lead to a huge breaking change for current application that wants a separate configuration.

I would suggest an optional argument/property to pass to the connectMicroservice function to use or provide an existing ApplicationConfig instance

Another solution could be adding a method to attach a NestMicroservice instance to the NestApplication without relying on connectMicroservice

  public attachMicroservice(instance: NestMicroservice): INestMicroservice {
    instance.registerListeners();
    instance.setIsInitialized(true);
    instance.setIsInitHookCalled(true);
    this.microservices.push(instance);
    return instance;
  }

What is the motivation / use case for changing the behavior?

I would expect that hybrid application can share the same ApplicationConfig.
Not being able to set global guard/interceptors for nest-grpc running in an hybrid application, greatly reduce the experience of it.

@2Kable 2Kable added needs triage This issue has not been looked into type: enhancement 🐺 labels Mar 10, 2020
@jmcdo29
Copy link
Member

jmcdo29 commented Mar 13, 2020

Are you able to globally bind interceptors and guards using custom providers, e.g.

{
  provide: APP_INTERCEPTOR,
  useClass: InterceptorClass
}

@2Kable
Copy link
Contributor Author

2Kable commented Mar 13, 2020

That's how i'm currently doing it. But the APP_INTERCEPTOR is set using useGlobalInterceptors at NestFactory.createApplicationContext time

NestFactory.createApplicationContext -> NestFactory.initialize -> Scanner.applyApplicationProviders -> Scanner.getApplyProvidersMap -> it then adds the global interceptor to the ApplicationConfig

but the ApplicationConfig instance is not used when running connectMicroservice(), it create a new one

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 13, 2020

Oh, that's super interesting. I haven't had a chance to play around with Hybrid Applications much, so that's good to know.

@kamilmysliwiec kamilmysliwiec added scope: microservices and removed needs triage This issue has not been looked into labels Mar 13, 2020
@kamilmysliwiec
Copy link
Member

Maybe we can add an additional argument to the connectMicroservice() method(?). What about something like this:

const transportOptions = {
  transport: Transport.GRPC,
  options: {}
};
 
app.connectMicroservice(transportOptions, { inheritAppConfig: true });

@2Kable
Copy link
Contributor Author

2Kable commented Mar 13, 2020

Looks good to me.
I can start working on the PR if the specs are a go

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 17, 2020

Out of curiosity, would this also affect apps with WebSockets? I'm working on a logging library and using the custom provider for globally binding interceptors doesn't seem to bind for gateways. Only HTTP calls.

@2Kable
Copy link
Contributor Author

2Kable commented Mar 18, 2020

I not familiar with WS implementation, but after a quick look I don't think this is the same issue

@Jekiwijaya
Copy link
Contributor

Hi, i'm also facing this same issue, my global interceptor is ignored in microservices,

I see the pull request has been made, is there anything I can help to make it merge? Thank you

@2Kable
Copy link
Contributor Author

2Kable commented Apr 28, 2020

Hi,
PR is ready, but I'm struggling with the test coverage. As far as I know it's just because I added lines, but can't actually covers them
So feel free to contribute/help

@kamilmysliwiec
Copy link
Member

@2Kable I'll take care of this, no worries! Thanks

@kamilmysliwiec
Copy link
Member

Added in 7.1.0. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants