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. 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? In my case it's just a nested object within 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
}
}
解决方案
我认为您需要做的就是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;
}
推荐阅读
- javascript - jquery圈子进度| 更改圆圈进度的颜色
- python - 使用步骤高效创建 numpy N 数组
- c# - 如何将 Google 输入的数字字符串转换为整数作为存储到 MySql DB 中?
- android - 入队后更改 PeriodicWorkRequest 的重复间隔
- php - 如何在 laravel 5.4 中使用 post 提交表单
- angular - Ionic 2 - *ngFor over google maps 消失
- php - Flex appengine mongoDb
- javascript - 如果嵌套,CSS 将不起作用
- c# - 错误 Guid 应包含 32 位数字和 4 个破折号
- css - 拒绝应用样式,因为它的 MIME 类型('text/html')