从Nest JS GraphQL Apollo播放器中的解析器返回数组对象

huangapple go评论85阅读模式
英文:

Return array object from resolver in nest js graphql Apollo play ground

问题

I need to return array of objects in the nest js graphql resolver. I have a mongodb schema which have multiple object array in the schema.

Supplier field is an object array which keep the relationship with Supplier schema with supplier_id.

I created (mutation) ingredient records successfully in my mongodb.

But when I am querying this record. My apollo server playground not allowing me to get the suppliers. I and successfully run the query to import ingredient_name and _id. My return type is ResultIngredientDto.

My graphql play ground only show

Can some one advice me what is the change I have to do to get the supplier object array. I have printed my query output it printing all the details. Thank you in advance.

英文:

I need to return array of objects in the nest js graphql resolver. I have a mongodb schema which have multiple object array in the schema.

  1. @Schema()
  2. @ObjectType()
  3. export class Ingredient{
  4. @Field(() => ID)
  5. _id: string;
  6. @Prop()
  7. @Field({})
  8. ingredient_name: string;
  9. @Prop({type: mongoose.Types.Array})
  10. suppliers: [IngredientSupplier]
  11. }

Supplier field is an object array which keep the relationship with Supplier schema with supplier_id.

  1. @Schema()
  2. export class IngredientSupplier {
  3. @Field(() => ID)
  4. _id: string;
  5. @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Supplier' })
  6. @Field()
  7. supplier_id: string;
  8. @Prop({ required: true })
  9. @Field({})
  10. supplier_name: string
  11. @Prop({ nullable: true, default:0 })
  12. @Field({})
  13. unit_size: number;
  14. @Prop({ nullable: true, default: "" } )
  15. @Field({})
  16. unit_type: string;
  17. }

I created (mutation) ingredient records successfully in my mongodb.

  1. {
  2. _id: new ObjectId("640746a5959b153fd91be321"),
  3. ingredient_name: 'test ingredient',
  4. suppliers: [
  5. {
  6. supplier_id: new ObjectId("63f8618d7f193739fac93d51"),
  7. supplier_name: 'test',
  8. unit_size: 112,
  9. unit_type: 'KG',
  10. _id: new ObjectId("640746a5959b153fd91be322")
  11. },
  12. {
  13. supplier_id: new ObjectId("63f880447f193739fac93d5d"),
  14. supplier_name: 'test11',
  15. unit_size: 114,
  16. unit_type: 'KG',
  17. _id: new ObjectId("640746a5959b153fd91be323")
  18. }
  19. ],

But when I am querying this record. My apollo server playground not allowing me to get the suppliers. I and successfully run the query to import *** ingredient_name**** and *** _id ****. My return type is ResultIngredientDto.

  1. @ObjectType()
  2. export class ResultIngredientDto {
  3. @Field(()=>ID)
  4. _id: string
  5. @Field()
  6. ingredient_name: string
  7. suppliers: suppliers[]
  8. }
  9. @ObjectType()
  10. export class suppliers {
  11. @Field(()=>ID)
  12. _id: string
  13. @Field()
  14. supplier_id: string
  15. @Field({})
  16. supplier_name: string
  17. @Field()
  18. unit_size: number
  19. @Field({})
  20. unit_type: string
  21. }

My graphql play ground only show

  1. type ResultIngredientDto {
  2. _id: ID!
  3. ingredient_name: String!
  4. }

Can some one advice me what is the change I have to do to get the supplier object array. I have printed my query output it printing all the details. Thank you in advance.

答案1

得分: 1

suppliers 属性也需要 @Field 装饰器,这样你就可以添加

  1. @Field(() => [suppliers])
英文:

suppliers property also needs @Field decorator so you can add

  1. @Field(() => [suppliers])

huangapple
  • 本文由 发表于 2023年3月8日 19:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定