0

我正在尝试将 typegoose 与 graphql 一起使用。我在另一个对象中有一个嵌套对象列表。我已经定义了类如下

@ObjectType()
export class ChildClass {
  @prop()
  @Field({ nullable: true })
  someField: string;

  @prop()
  @Field({ nullable: true })
  anotherField: string;
}

@ObjectType()
@modelOptions()
export class ParentClass extends Typegoose {
  @prop()
  @Field(() => String)
  someField: string;

  @prop()
  @Field(() => [ChildClass], { nullable: 'itemsAndList' })
  children: ChildClass[];
}

当我调用以下查询时

query {
  parentClass {
    someField
    children {
      someField
      anotherField
    }
  }
}

它总是给我null孩子的someFieldanotherField。但是,如果我使用猫鼬而不是 typegoose,它可以正常工作。MongoDb 在使用 typegoose 时返回正常。它转换为 graphql 很不稳定。

我在这里想念什么?

4

1 回答 1

1

经过一番折腾,终于明白为什么会这样了。将模型更新为

  @prop( { type: () => [ChildClass] } )
  @Field(() => [ChildClass], { nullable: 'itemsAndList' })
  children: ChildClass[];

会变魔术。根据文档

于 2020-10-27T01:18:04.863 回答