【发布时间】:2021-02-09 14:46:50
【问题描述】:
我将typegoose 与type-graphql 一起使用,当我尝试使用嵌套@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
}
如何解决这个问题?
【问题讨论】:
标签: node.js typescript typegraphql typegoose