nestjs-dataloader icon indicating copy to clipboard operation
nestjs-dataloader copied to clipboard

Usage in @FieldResolver()

Open vinaybedre opened this issue 2 years ago • 1 comments

Hi,

Is it possible to use nestjs-dataloader with something like below?

@Resolver(() => AccountDto)
export class AccountFieldResolver {
  constructor(private queryBus: QueryBus) {}

  @ResolveField(() => [AccountDto], { name: 'account' })
  async resolveCompanyAbsences(
    @Args('input') { id }: AccountKey,
    @Loader(CompanyEmployeeAbsenceLoader) accountLoader: DataLoader<AccountKey, Account>,
  ) {
     return accountLoader.load({ id });
  }

Seems that the interceptor is not being initialized and it fails with error You should provide interceptor ${DataLoaderInterceptor.name} globally with ${APP_INTERCEPTOR}

vinaybedre avatar Jan 30 '23 09:01 vinaybedre

Hey @vinaybedre I was just passing through and saw your question.

What you're trying to do is possible – you're just missing one thing: globally registering your loader.

In your top level app module, make sure to provide CompanyEmployeeAbsenceLoader:

@Module({
  providers: [
    AccountFieldResolver, // <-- you're probably providing this somewhere, already
    CompanyEmployeeAbsenceLoader, // <-- make sure to provide your loader
    {
      provide: APP_INTERCEPTOR,
      useClass: DataLoaderInterceptor,
    }, // <-- as well as the library's DataloaderInterceptor
  ],

})
export class AppModule { }

dhritzkiv avatar Feb 14 '23 21:02 dhritzkiv