简体   繁体   中英

Handling nested input-types with type-graphql and typegoose

I'm using typegoose with type-graphql and when I try to use nested @InputType() , the nested object gets converted into mongoose.Types.ObjectId() . How do I handle nested InputTypes

This is my code ( GrandChild is not a mongoose document, it's a simple object-type)

@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;
}

Example input:

{
  "name": "Parent A",
  "child": {
    "name": "Child A",
    "grandChild": {
      "name": "GrandChild A"
    }
  }
}

parent query:

{
  parent {
    name
    child {
      name
      grandChild {
        name
      }
    }
  }
}

When I run the parent query, I get the following output

Cannot read property "Child.grandChild" of undefined.

I used mongo bash to get the parent document and this is what I got:

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
}

How do fix this?

this question was also asked on discord, which came to the conclusion that the provided output was not right, and after finding the the actual data was:

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

then the issue was clear: class GrandChild didnt have any @prop on properties
(this was known from the beginning, but what was investigated was why it was grandChild: ObjectId("some-object-id-here") instead of grandChild: { _id: ObjectId("some-object-id-here") } )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM