使用解析器类继承时如何注入服务

时间:2019-04-06 09:19:33

标签: nestjs type-graphql

我尝试在与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;
};

0 个答案:

没有答案