无法在左轮手枪中依赖注入服务

时间:2021-07-04 21:14:29

标签: graphql nestjs fastify

我无法在同一模块中使用带有解析器的服务。我已经看了这个至少一个小时,但不知道出了什么问题。

auth 服务在 auth 模块的 providers 数组中,所以 auth 解析器应该能够使用它吧?

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

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

@Module({
  imports: [
    MercuriusModule.forRoot({ autoSchemaFile: true, graphiql: 'playground' }),
    AppResolver,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
auth.module.ts

@Module({
  imports: [AuthResolver],
  providers: [AuthService],
})
export class AuthModule {}
auth.service.ts

@Injectable()
export class AuthService {}
auth.resolver.ts

@Resolver()
export class AuthResolver {
  constructor(private readonly authService: AuthService) {}

  @Mutation(() => Boolean)
  sendMagicLink(): boolean {
    return true;
  }

  @Mutation(() => Boolean)
  signIn(): boolean {
    return true;
  }

  @Mutation(() => Boolean)
  signOut(): boolean {
    return true;
  }

  @Mutation(() => Boolean)
  revokeRefreshToken(): boolean {
    return true;
  }
}

1 个答案:

答案 0 :(得分:0)

问题是解析器必须像这样位于提供者数组中。

@Module({
  providers: [AuthService, AuthResolver],
})
export class AuthModule {}