将 Typegoose 与 GraphQL 结合使用
Using Typegoose with GraphQL
我正在尝试将 typegoose 与 graphql 一起使用。我在另一个 object 中有一个嵌套的 object 列表。我定义了 类 如下
@ObjectType()
export class ChildClass {
@prop()
@Field({ nullable: true })
someField: string;
@prop()
@Field({ nullable: true })
anotherField: string;
}
@ObjectType()
@modelOptions()
export class ParentClass extends Typegoose {
@prop()
@Field(() => String)
someField: string;
@prop()
@Field(() => [ChildClass], { nullable: 'itemsAndList' })
children: ChildClass[];
}
当我调用以下查询时
query {
parentClass {
someField
children {
someField
anotherField
}
}
}
对于 children 的 someField
和 anotherField
,它总是给我 null
。但是,如果我使用 mongoose 而不是 typegoose,它就可以正常工作。 MongoDb 在使用 typegoose 时返回正常。它向 graphql 的转换很不稳定。
我在这里错过了什么?
经过一番修修补补,终于明白了为什么会这样。将模型更新为
@prop( { type: () => [ChildClass] } )
@Field(() => [ChildClass], { nullable: 'itemsAndList' })
children: ChildClass[];
施展魔法。根据 documentation
我正在尝试将 typegoose 与 graphql 一起使用。我在另一个 object 中有一个嵌套的 object 列表。我定义了 类 如下
@ObjectType()
export class ChildClass {
@prop()
@Field({ nullable: true })
someField: string;
@prop()
@Field({ nullable: true })
anotherField: string;
}
@ObjectType()
@modelOptions()
export class ParentClass extends Typegoose {
@prop()
@Field(() => String)
someField: string;
@prop()
@Field(() => [ChildClass], { nullable: 'itemsAndList' })
children: ChildClass[];
}
当我调用以下查询时
query {
parentClass {
someField
children {
someField
anotherField
}
}
}
对于 children 的 someField
和 anotherField
,它总是给我 null
。但是,如果我使用 mongoose 而不是 typegoose,它就可以正常工作。 MongoDb 在使用 typegoose 时返回正常。它向 graphql 的转换很不稳定。
我在这里错过了什么?
经过一番修修补补,终于明白了为什么会这样。将模型更新为
@prop( { type: () => [ChildClass] } )
@Field(() => [ChildClass], { nullable: 'itemsAndList' })
children: ChildClass[];
施展魔法。根据 documentation