【问题标题】:Using Typegoose with GraphQL将 Typegoose 与 GraphQL 一起使用
【发布时间】:2021-02-09 02:05:45
【问题描述】:

我正在尝试将 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 很不稳定。

我在这里错过了什么?

【问题讨论】:

    标签: mongoose graphql nestjs typegoose


    【解决方案1】:

    经过一番折腾,终于明白为什么会发生这种情况了。将模型更新为像

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

    会变魔术。根据documentation

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 2021-09-03
      • 1970-01-01
      • 2021-08-14
      • 2016-09-24
      • 2016-09-19
      • 2021-03-24
      • 2020-10-21
      • 2019-04-29
      相关资源
      最近更新 更多