我正在使用typegoose
with type-graphql
,当我尝试使用 nested@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
}
如何解决这个问题?