我在NestJS中使用代码优先方法来创建GraphQL模式。我有以下定义。 Person可以有一个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
}
服务器启动时,出现以下错误消息。
ReferenceError:初始化之前无法访问'Dog'
我做错了什么? Dog是否需要由解析器中的@ ResolveField()解析?就我而言,它只是Person中的一个嵌套对象。狗不是来自其他服务电话。
基本上,我想代表以下结构。
interface Person {
name: string;
dog?: {
breed: string;
name: string
}
}
我想您需要做的就是先声明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;
}