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

Cannot define global Nest providers in plugins #837

Closed
michaelbromley opened this issue Apr 22, 2021 · 0 comments
Closed

Cannot define global Nest providers in plugins #837

michaelbromley opened this issue Apr 22, 2021 · 0 comments
Assignees
Labels
type: bug 🐛 Something isn't working

Comments

@michaelbromley
Copy link
Member

michaelbromley commented Apr 22, 2021

Describe the bug
In Nest, you can define global providers such as interceptors like this:

@Module({
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor,
    },
  ],
})
export class AppModule {}

Moreover, this should be possible from any module:

you can set up an interceptor directly from any module
source

Since Vendure plugins are a superset of Nest modules, it should also be possible to define these global providers from plugins. However, currently they do not work due to this part of the plugin-handling code:

const nestModuleMetadata = pick(pluginMetadata, Object.values(MODULE_METADATA) as any);
// Automatically add any of the Plugin's "providers" to the "exports" array. This is done
// because when a plugin defines GraphQL resolvers, these resolvers are used to dynamically
// created a new Module in the ApiModule, and if those resolvers depend on any providers,
// the must be exported. See the function {@link createDynamicGraphQlModulesForPlugins}
// for the implementation.
nestModuleMetadata.exports = [
...(nestModuleMetadata.exports || []),
...(nestModuleMetadata.providers || []),
];
Module(nestModuleMetadata)(target);

Attempting to define global providers will result in an error like:

[run:worker] error 22/04/21, 14:18 - [ExceptionHandler] Nest cannot export a provider/module that is not a part of the currently process
ed module (PluginWithGlobalProviders). Please verify whether the exported APP_INTERCEPTOR is available in this particular context.
[run:worker]
[run:worker] Possible Solutions:
[run:worker] - Is APP_INTERCEPTOR part of the relevant providers/imports within PluginWithGlobalProviders?

Expected behavior
Global providers should work as described in the Nest docs.

Environment (please complete the following information):

  • @vendure/core version: 1.0.0-beta.8
  • Nodejs version: any
  • Database (mysql/postgres etc): any

Additional context
The fix is probably to inspect the providers array and filter out the following tokens: APP_INTERCEPTOR, APP_PIPE, APP_GUARD, APP_FILTER

@michaelbromley michaelbromley added the type: bug 🐛 Something isn't working label Apr 22, 2021
@michaelbromley michaelbromley self-assigned this Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant