Issue with Dependency Injection with Hooks
coupster74 opened this issue · comments
Coupster commented
try as I might, I have not been able to get dependency injection working for anything with my hook class.. Here is the class:
@Injectable()
export class ActivityLogAccessHook implements BeforeFindOneHook {
constructor(
@InjectRepository(ActivityLog)
private readonly activityLogRepository: Repository<ActivityLog>,
//private readonly emailService: EmailService,
// @InjectRepository(UserContext)
// private readonly userContextRepository: Repository<UserContext>,
//@CurrentUser() private userContext: UserContext,
) {}
async run(instance: FindOneArgsType, context: any): Promise<FindOneArgsType> {
const entityName = context.req.body.query
.replace(/\s\s+/g, ' ')
.split(' ')[1]
.split('(')[0];
const entityRefId = instance.id.toString() ?? '';
console.log(entityName);
console.log(entityRefId);
// const results = await this.activityLogService.createOne({
// type: ActivityLogType.ACCESS,
// referenceEntity: entityName,
// referenceId: entityRefId,
// });
// console.log(results);
return instance;
}
}
and as you can see, I want to log access to the entity, which has this defined:
@BeforeFindOne(ActivityLogAccessHook)
the module is configured as follows:
Module({
imports: [
NestjsQueryGraphQLModule.forFeature({
// import the NestjsQueryTypeOrmModule to register the entity with typeorm
// and provide a QueryService
imports: [
NestjsQueryTypeOrmModule.forFeature([Article]),
NestjsQueryTypeOrmModule.forFeature([ActivityLog]),
ActivityLogAccessHook,
ActivityLogModule,
],
// describe the resolvers you want to expose
assemblers: [ArticleAssembler],
resolvers: [
{
DTOClass: Article,
EntityClass: Article,
enableAggregate: true,
enableSubscriptions: true,
enableTotalCount: true,
},
],
}),
],
})
export class ArticleModule {}
and try as I might, I always get the following:
[Nest] 13964 - 2023-02-07, 3:47:55 p.m. ERROR [ExceptionHandler] Nest can't resolve dependencies of the ActivityLogAccessHook (?). Please make sure that the argument ActivityLogRepository at index [0] is available in the ActivityLogAccessHook context.
Potential solutions:
- If ActivityLogRepository is a provider, is it part of the current ActivityLogAccessHook?
- If ActivityLogRepository is exported from a separate @Module, is that module imported within ActivityLogAccessHook?
@Module({
imports: [ /* the Module containing ActivityLogRepository */ ]
})
and I've had the same issue whether I try to import a repository, query service, or any other injectable service. Any help would be appreciated.