简体   繁体   English

使用代码优先方法在 NestJS 中使用 GraphQL 模型出错

[英]Getting error with GraphQL model in NestJS, using code-first approach

I'm using the code-first approach in NestJS to create GraphQL schemas.我在 NestJS 中使用代码优先方法来创建 GraphQL 模式。 I have the following definition.我有以下定义。 Person can have a Dog .可以有

@ObjectType()
export class Person {
  @Field()
  name: string;
  @Field(type => Dog, { nullable: true })
  dog?: Dog;
}

@ObjectType()
export class Dog {
  @Field()
  breed: string
  @Field()
  name: string
}

When server starts up I get the following error message.当服务器启动时,我收到以下错误消息。

ReferenceError: Cannot access 'Dog' before initialization参考错误:初始化前无法访问“狗”

What am I doing wrong?我究竟做错了什么? Does Dog need to be resolved by @ResolveField() in the resolver? Dog 是否需要通过解析器中的@ResolveField()来解析? In my case it's just a nested object within Person.就我而言,它只是 Person 中的一个嵌套对象。 Dog doesn't come from another service call.狗不是来自另一个服务电话。

Essentially, I want to represent the following structure.本质上,我想表示以下结构。

interface Person {
  name: string;
  dog?: {
    breed: string;
    name: string
  }
}

I think all you need to do is declare Dog first:我认为您需要做的就是先声明Dog

@ObjectType()
export class Dog {
  @Field()
  breed: string;
  @Field()
  name: string;
}

@ObjectType()
export class Person {
  @Field()
  name: string;
  @Field(type => Dog, { nullable: true })
  dog?: Dog;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM