(节点:18560)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性'typeFn'

时间:2020-05-03 17:31:49

标签: mongodb typescript mongoose graphql nestjs

我尝试使用@ResolveField()装饰器解析字段(块)时遇到此错误。

page.resolver.ts

import {
  Resolver,
  Query,
  Mutation,
  Args,
  ResolveField,
  Parent,
} from '@nestjs/graphql';
import { PageService } from './page.service';
import { PageType } from './type/page.type';
import { CreatePageInput } from './input/create-page.input';
import { BlockService } from '../block/block.service';
import { Page } from './page.interface';

@Resolver('Page')
export class PageResolver {
  constructor(
    private readonly pageService: PageService,
    private readonly blockService: BlockService,
  ) {}

  @Query(() => [PageType])
  pages() {
    return this.pageService.getAllPages();
  }

  @Query(() => [PageType])
  async page(@Args('id') id: string) {
    return this.pageService.getPage(id);
  }

  @Mutation(() => PageType)
  createPage(@Args('createPageInput') createPageInput: CreatePageInput) {
    return this.pageService.createPage(createPageInput);
  }

  @ResolveField()
  blocks(@Parent() page: Page) {
    return this.blockService.getManyBlocks(page.blockIds);
  }
}

page.interface.ts

import { Document } from 'mongoose';

export interface Page extends Document {
  readonly id: string;
  readonly name: string;
  readonly createdAt: Date;
  readonly updatedAt: Date;
  readonly createdBy: string;
  readonly updatedBy: string;
  readonly blockIds: string[];
}

1 个答案:

答案 0 :(得分:6)

() => PageType装饰器中使用函数@Resolver()后即可解决。

@Resolver(() => PageType)