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

Issue with Dependency Injection with Hooks #1563

Open
coupster74 opened this issue Feb 7, 2023 · 0 comments
Open

Issue with Dependency Injection with Hooks #1563

coupster74 opened this issue Feb 7, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@coupster74
Copy link

coupster74 commented Feb 7, 2023

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.

@coupster74 coupster74 added the bug Something isn't working label Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant