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

Request and transient-scoped providers can't be used in combination with \"get()\" method. Please, use \"resolve()\" instead."

Open Dr-Phone opened this issue 4 years ago • 2 comments

Hi, Thanks for the library. I experimented with it and got stuck the following error.

CityLoader is marked as a scoped provider. Request and transient-scoped providers can't be used in combination with \"get()\" method. Please, use \"resolve()\" instead."

Any help is appreciated.

Here's how my CityLoader looks like -

import { Injectable } from '@nestjs/common';
import { OrderedNestDataLoader } from 'nestjs-graphql-dataloader';
import { ProfileService } from './profile.service';
import { Profile } from './entities/profile.entity';

@Injectable()
export class CityLoader extends OrderedNestDataLoader<number, City> {
  constructor(
    private readonly profileService: ProfileService) {
    super()
  }

  protected getOptions = () => ({
    query: (keys: Array<number>) => this.profileService.findCityByIds(keys),
  });
}

And this is how I'm trying to resolve it.

import DataLoader from 'dataloader';
import { Loader } from 'nestjs-graphql-dataloader';
import { CityLoader } from './profile.dataloader';

@ResolveField(returns => CityType)
    async city(
        @Parent() parent: Profile,
        @Loader(CityLoader) cityLoader: DataLoader<number, City>
    ) {
        // return this.profileService.getCity(parent.cityId);
        return cityLoader.load(parent.cityId);
    }

Dr-Phone avatar Aug 11 '20 14:08 Dr-Phone

For anybody else facing the same issue -

replace the following in the library src file - index.ts

await this.moduleRef.resolve<NestDataLoader<any, any>>(
                    type,
                    ctx[NEST_LOADER_CONTEXT_KEY].contextId,
                    { strict: false }
                  )

with the following

this.moduleRef.get<NestDataLoader<any, any>>(
                    type,
                    ctx[NEST_LOADER_CONTEXT_KEY].contextId,
                  )

And it will work.

Dr-Phone avatar Aug 18 '20 06:08 Dr-Phone

Hi @Dr-Phone, sorry for not getting back to you on this. I will take a look at this as I previously switched this from get to resolve and it was my understanding that this should work in all scenarios.

I am rather snowed under at the moment but I will do my best to look at this in the next 48 hours.

TreeMan360 avatar Aug 18 '20 06:08 TreeMan360