I am trying to add an auth guard to a GraphQL resolver but it doesn't seem to be working as expected.
I have a simple guard that should reject all requests:
@Injectable()
export class AuthGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
return false;
}
}
and have applied it in a resolver:
@Resolver()
export class RecipeResolver {
@Query(() => [Recipe])
@UseGuards(AuthGuard)
public recipes(): Recipe[] {
return this.recipeData;
}
}
This does not work in the resolver and the canActivate
is never fired. This does work in an HTTP Controller.
Changing @UseGuards(AuthGuard)
to @UseGuards(new AuthGuard())
works but will not go through the dependency injection process which is not ideal.
What am I doing wrong?
Edit:
original app.module.ts
:
@Module({
imports: [
GraphQLModule.forRoot({
autoSchemaFile: 'shcema.gql',
playground: true,
}),
RecipeResolver,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
RecipeResolver
in theAppModule
'simports
array? It should be in theproviders
- Jay McDonielimports
toproviders
fixed the issue. Thank you! - bsthedev