我正在尝试使用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
}
}
}它总是为孩子们的someField和anotherField提供null。但是,如果我使用mongoose而不是typegoose,它可以正常工作。使用typegoose时,MongoDb返回正常。到graphql的转换变得不可靠了。
这里我漏掉了什么?
发布于 2020-10-27 09:18:05
在修修补补之后,我终于明白了为什么会发生这种情况。将模型更新为类似于
@prop( { type: () => [ChildClass] } )
@Field(() => [ChildClass], { nullable: 'itemsAndList' })
children: ChildClass[];施展魔法。根据documentation
https://stackoverflow.com/questions/64543517
复制相似问题