[英]Handling nested input-types with type-graphql and typegoose
我将typegoose
与type-graphql
一起使用,当我尝试使用嵌套的@InputType()
,嵌套的对象会转换为@InputType()
mongoose.Types.ObjectId()
。 我如何处理嵌套的 InputTypes
这是我的代码( GrandChild
不是猫鼬文档,它是一个简单的对象类型)
@ObjectType()
export class GrandChild {
@Field()
name: string;
}
@ObjectType()
export class Child {
@prop()
@Field()
name: string;
@prop({ type: () => GrandChild })
@Field(() => GrandChild)
grandChild: GrandChild;
}
@ObjectType()
export class Parent {
@prop()
@Field()
name: string;
@prop({ ref: Child })
@Field(() => Child)
child: Ref<Child>;
}
@InputType()
export class GrandChildInput {
@Field()
name: string;
}
@InputType()
export class ChildInput {
@Field()
name: string;
@Field(() => GrandChild)
grandChild: GrandChildInput;
}
@InputType()
export class Parent {
@Field()
name: string;
@Field(() => Child)
child: ChildInput;
}
示例输入:
{
"name": "Parent A",
"child": {
"name": "Child A",
"grandChild": {
"name": "GrandChild A"
}
}
}
parent
查询:
{
parent {
name
child {
name
grandChild {
name
}
}
}
}
当我运行parent
查询时,我得到以下输出
Cannot read property "Child.grandChild" of undefined.
我使用 mongo bash 来获取父文档,这就是我得到的:
db.parent.find():
{
name: "Parent A",
child: ObjectId("some-object-id-here")
}
db.child.find():
{
name: "Child A",
grandChild: {
_id: ObjectId("some-object-id-here")
} // <-- This is not supposed to be a document
}
如何解决这个问题?
这个问题也是在discord上被问到的,得出的结论是提供的输出不正确,在找到实际数据后是:
{
name: "Child A",
grandChild: {
_id: ObjectId("some-object-id-here")
}
}
那么问题GrandChild
清楚了: GrandChild
类在属性上没有任何@prop
(这从一开始就知道,但调查的是为什么是grandChild: ObjectId("some-object-id-here")
而不是grandChild: { _id: ObjectId("some-object-id-here") }
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.