簡體   English   中英

使用 type-graphql 和 typegoose 處理嵌套輸入類型

[英]Handling nested input-types with type-graphql and typegoose

我將typegoosetype-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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM