Closed kallekulp closed 4 months ago
Try the following instead:
nestjs
I think this shouldn't be closed. It seems like it's a bug. Even with the minimal setup this doesn't work. Meaning you cannot inject a dependency two layers down into the guard
@kallekulp, this is not a bug, this is a feature.
Is there an existing issue for this?
Current behavior
Unable to inject a service to a guard from one module down. I don't know if this is related to how guards are constructed but I have the following scenario:
I have AppModule, AModule and BModule.
AppModule is allowed to import AModule which defines AuthGuard (example). AuthGuard is then exported from AModule. AModule is allowed to import BModule which defines a service, that needs to be injected into the AuthGuard. AppModule is not allowed to import BModule.
The following structure is based on linter rules ( "@nx/enforce-module-boundaries":)
With the current setup it throws me the following Error:
`Error: Nest can't resolve dependencies of the AuthGuard (?). Please make sure that the argument BService at index [0] is available in the AppModule context.
Potential solutions:
If I inject the AuthGuard into the constructor of
AppController
it works.Is this approach even possible?
Minimum reproduction code
https://github.com/kallekulp/nest-js-usecase
Steps to reproduce
Expected behavior
I expect that the Guard constructor and dependency tree would work the same way as it works if I'd inject it the Guard/Service into the controller's constructor
Package
Other package
No response
NestJS version
10.3.8
Packages versions
Node.js version
18.17.0
In which operating systems have you tested?
Other
No response