【问题标题】:NestJS: Problem with using ResolveProperty. Got " UnhandledPromiseRejectionWarning: TypeError:"NestJS:使用 ResolveProperty 的问题。得到“ UnhandledPromiseRejectionWarning: TypeError:”
【发布时间】:2020-06-08 15:06:45
【问题描述】:

我的父模型(相机)包含子模型(传送带)的 id,如下所示:

@ObjectType('camera')

export class CameraModel {
  @Field(() => ID)
  _id: String;

  @Field()
  name: String;

  @Field(() => ConveyorModel)
  conveyor: ConveyorModel;
}

因此,为了获得“传送器”的详细信息,我需要在 camera.resolver.ts 中使用 @ResolveProperty 装饰器(注意:仅显示相关方法)

import { CameraModel } from './models/camera.model';
import { ConveyorService } from "../conveyor/conveyor.service";
import { ConveyorModel } from "../conveyor/models/conveyor.model";

@Injectable()
export class CameraResolver {
  constructor(    
    private conveyorService: ConveyorService, 
    private cameraService: CameraService,
    ) {}

  @ResolveProperty('conveyor', type => ConveyorModel)  
  async getConveryor(@Parent() CameraModel) {
    const { cid } = CameraModel;
    return this.conveyorService.findOne(cid);     
  }
}

运行服务器后出现以下错误。主要来自 graphql 模式生成器。

(node:30636) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getObjectType' of undefined

...

(node:30636) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:30636) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

但是,如果我注释掉 @ResolveProperty 块,一切都很好。但我不能只获取传送带的详细信息(仅获取 ID)。

我在这里缺少什么?

【问题讨论】:

    标签: graphql code-first nestjs


    【解决方案1】:

    基于我在https://github.com/nestjs/graphql/issues/158 发现的相关问题

    已修复。

    不要使用@Resolver('YourModel') 的字符串基础装饰器。只需使用 @Resolver(of => YourModel);

    这部分NestJS的文档确实不够用。

    【讨论】:

    • 从文档中看起来,@Resolver(of => type) 用于code-first 方法,@Resolver('string') 用于schema-first 方法。
    猜你喜欢
    • 2021-09-26
    • 2021-05-31
    • 2020-06-26
    • 2021-08-07
    • 2019-09-23
    • 2018-05-20
    • 2020-12-27
    • 1970-01-01
    • 2022-10-14
    相关资源
    最近更新 更多