Luis
Luis

Reputation: 130

NestJS can't resolve dependency

I'm trying to setup a graphql nestjs clean architecture, I have my resolver call the service, service calls repository and repository calls orm methods. Am I missing a step?

But I get this error:

[Nest] 59764  - 03/10/2022, 16:36:13   ERROR [ExceptionHandler] Nest can't resolve dependencies of the AthleteRepository (?). Please make sure that the argument AthleteEntityRepository at index [0] is available in the GraphQLModule context.

Potential solutions:
- If AthleteEntityRepository is a provider, is it part of the current GraphQLModule?
- If AthleteEntityRepository is exported from a separate @Module, is that module imported within GraphQLModule?
  @Module({
    imports: [ /* the Module containing AthleteEntityRepository */ ]
  })

Error: Nest can't resolve dependencies of the AthleteRepository (?). Please make sure that the argument AthleteEntityRepository at index [0] is available in the GraphQLModule context.

This is my module:

@Module({
  imports: [
    InfraestructureModule,
    NestGraphqlModule.forRoot<MercuriusDriverConfig>({
      driver: MercuriusDriver,
      graphiql: true,
      autoSchemaFile: join(
        process.cwd(),
        'src/infraestructure/graphql/schema.gql',
      ),
      sortSchema: true,
    }),
  ],
  controllers: [AuthResolver, UserResolver],
  providers: [
    FirebaseService,
    AthleteEntity,
    AthleteRepository,
    AthleteService,
  ],
})
export class GraphQLModule {}

Adding repository by comment request, it's mostly just wrappers around the typerom Repository.

import { Controller } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AthleteEntity } from '../entity/athlete.entity';

@Controller('')
export class AthleteRepository {
  constructor(
    @InjectRepository(AthleteEntity)
    private athleteRepository: Repository<AthleteEntity>,
  ) {}

  async getAthleteByUserId(id: string): Promise<AthleteEntity> {
    return this.athleteRepository.findOne({ where: { id } });
  }

  async getAthletes(): Promise<AthleteEntity[]> {
    return this.athleteRepository.find();
  }
}

Upvotes: 0

Views: 278

Answers (2)

Shivam Patel
Shivam Patel

Reputation: 21

change AthleteRepository to an @Injectable instead of @Controller.

Upvotes: 0

Jay McDoniel
Jay McDoniel

Reputation: 70131

As you use @InjectRepository(AtheleteEntity) you need to have TypeOrmModule.forFeature([AtheleteEntity]) used in the imports of the GraphQlModule.

Side note: Is that supposed to be @Controller() or @Injectable()? It doesn't look like a standard HTTP or RPC controller to me

Upvotes: 1

Related Questions