首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在GraphQL中使用Typegoose

在GraphQL中使用Typegoose
EN

Stack Overflow用户
提问于 2020-10-27 03:09:08
回答 1查看 130关注 0票数 0

我正在尝试使用typegoose和graphql。我有一个嵌套在另一个对象中的对象列表。我按如下方式定义了类

代码语言:javascript
复制
@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[];
}

当我调用以下查询时

代码语言:javascript
复制
query {
  parentClass {
    someField
    children {
      someField
      anotherField
    }
  }
}

它总是为孩子们的someFieldanotherField提供null。但是,如果我使用mongoose而不是typegoose,它可以正常工作。使用typegoose时,MongoDb返回正常。到graphql的转换变得不可靠了。

这里我漏掉了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-27 09:18:05

在修修补补之后,我终于明白了为什么会发生这种情况。将模型更新为类似于

代码语言:javascript
复制
  @prop( { type: () => [ChildClass] } )
  @Field(() => [ChildClass], { nullable: 'itemsAndList' })
  children: ChildClass[];

施展魔法。根据documentation

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64543517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档