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

TransactionalConnection is not available in AppModule context #1160

Closed
Draykee opened this issue Oct 17, 2021 · 2 comments
Closed

TransactionalConnection is not available in AppModule context #1160

Draykee opened this issue Oct 17, 2021 · 2 comments
Assignees
Labels
type: bug 🐛 Something isn't working

Comments

@Draykee
Copy link
Contributor

Draykee commented Oct 17, 2021

Describe the bug
TransactionalConnection can't be injected into middleware.

UnknownDependenciesException [Error]: Nest can't resolve dependencies of the ShopApiMiddleware (ConfigService, SessionService, ?). Please make sure that the argument TransactionalConnection at index [2] is available in the AppModule context.


Potential solutions:
- If TransactionalConnection is a provider, is it part of the current AppModule?
- If TransactionalConnection is exported from a separate @Module, is that module imported within AppModule?
  @Module({
    imports: [ /* the Module containing TransactionalConnection */ ]
  })

To Reproduce
Steps to reproduce the behavior:

  1. Create a class implementing NestMiddleware and inject TransactionalConnection in the contructor
  2. Register the middleware and start the server

Expected behavior
TransactionalConnection should be available

Environment (please complete the following information):

  • @vendure/core version: 1.3.0

Additional context
A comment on that topic in slack

I think I know why this happens. It is due to an internal refractor where I put TransactionalConnection in its own module which is not being directly imported by AppModule.

@Draykee Draykee added the type: bug 🐛 Something isn't working label Oct 17, 2021
@michaelbromley
Copy link
Member

michaelbromley commented Oct 19, 2021

  1. Register the middleware and start the server

How exactly are you registering the middleware? Are you adding it to the apiOptions.middleware array, or something else?

I've tried to reproduce this using this middleware:

class MyMiddleware implements NestMiddleware {
    constructor(
        private connection: TransactionalConnection,
    ) {}
    use(req: any, res: any, next: any) {
        console.log('Request...');
        next();
    }
}

both by adding it to the middleware config array:

export const devConfig: VendureConfig = {
    apiOptions: {
        middleware: [{ handler: MyMiddleware, route: '/shop-api' }],
    },
}

and by registering it in a plugin:

@VendurePlugin({
    imports: [PluginCommonModule],
    providers: [],
})
class MyPlugin implements NestModule {
    configure(consumer: MiddlewareConsumer): any {
        consumer.apply(MyMiddleware).forRoutes('shop-api');
    }
}

export const devConfig: VendureConfig = {
    plugins: [MyPlugin],
}

and in both cases I cannot reproduce the error message you report. Could you provide a minimal reproduction?

@michaelbromley
Copy link
Member

Update: I missed the @Injectable() decorator off my middleware, now I can reproduce 👍

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

2 participants