我尝试在与Nestjs一起使用的TypeGraphQL中使用解析器类继承,但不知道如何将PostService
注入BaseResolver
import { ClassType } from 'type-graphql';
import { Query, Resolver, Args } from '@nestjs/graphql';
import { UseGuards } from '@nestjs/common';
import { PostService } from './post.service';
import { GqlAuthGuard } from 'src/account/auth/auth.guard';
export const createPostBaseResolver = <T extends ClassType>(
suffix: string,
objectTypeCls: T,
) => {
@Resolver(() => objectTypeCls, { isAbstract: true })
abstract class PostBaseResolver {
constructor(private readonly postService: PostService) {}
@Query(() => objectTypeCls, { name: `${suffix}` })
@UseGuards(GqlAuthGuard)
async getPostById(@Args('id') id: number) {
// here postService is undefined
return await this.postService.get(id);
}
}
return PostBaseResolver as any;
};