1

我正在使用typegoosewith 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
}

如何解决这个问题?

4

1 回答 1

0

这个问题也在discord上被问到,得出的结论是提供的输出不正确,在找到实际数据后是:

{
  name: "Child A",
  grandChild: {
    _id: ObjectId("some-object-id-here")
  }
}

那么问题就很清楚了:类GrandChild没有任何@prop属性
(这从一开始就知道,但调查的是为什么它是grandChild: ObjectId("some-object-id-here")而不是grandChild: { _id: ObjectId("some-object-id-here") }

于 2020-10-28T14:29:52.040 回答