Hey! The problem is that I have an AuthGuard which has 2 dependencies: AuthService & UserService, so when I need to use it in any other module (say BookModule) I have to import both: AuthModule and UserModule. It is not such a big deal, but then a situation happens when I need to use BookService inside of UserModule, which causes Circular Dependency (BookModule -> UserModule -> BookModule). Are there any ways to resolve the issue?
#Nest Guards Circular Dependencies Issue
4 messages · Page 1 of 1 (latest)
Have you already tried forwardRef(() => ...)? (see here: https://docs.nestjs.com/fundamentals/circular-dependency)
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).
But that's still a circular dependency, which leads to too coupled code
Oh, you want to avoid circular dependencies at all? That's a completely different problem imo, cause it depends on how the functionality of your UserModule and BookModule can be decoupled into independent services. I also try to avoid circular dependencies as much as possible, but sometimes, they are just the easiest way to go, because all other options might only unnecessarily increase your overall system complexity. So, either move the functionality that is required in both modules into their own service and import them in both modules, or just leave the CD as it is with forwardRef 🤷♂️ . That'd be my take on it 😅