英文:
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.
@Schema()
@ObjectType()
export class Ingredient{
@Field(() => ID)
_id: string;
@Prop()
@Field({})
ingredient_name: string;
@Prop({type: mongoose.Types.Array})
suppliers: [IngredientSupplier]
}
Supplier field is an object array which keep the relationship with Supplier
schema with supplier_id
.
@Schema()
export class IngredientSupplier {
@Field(() => ID)
_id: string;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Supplier' })
@Field()
supplier_id: string;
@Prop({ required: true })
@Field({})
supplier_name: string
@Prop({ nullable: true, default:0 })
@Field({})
unit_size: number;
@Prop({ nullable: true, default: "" } )
@Field({})
unit_type: string;
}
I created (mutation) ingredient records successfully in my mongodb.
{
_id: new ObjectId("640746a5959b153fd91be321"),
ingredient_name: 'test ingredient',
suppliers: [
{
supplier_id: new ObjectId("63f8618d7f193739fac93d51"),
supplier_name: 'test',
unit_size: 112,
unit_type: 'KG',
_id: new ObjectId("640746a5959b153fd91be322")
},
{
supplier_id: new ObjectId("63f880447f193739fac93d5d"),
supplier_name: 'test11',
unit_size: 114,
unit_type: 'KG',
_id: new ObjectId("640746a5959b153fd91be323")
}
],
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
.
@ObjectType()
export class ResultIngredientDto {
@Field(()=>ID)
_id: string
@Field()
ingredient_name: string
suppliers: suppliers[]
}
@ObjectType()
export class suppliers {
@Field(()=>ID)
_id: string
@Field()
supplier_id: string
@Field({})
supplier_name: string
@Field()
unit_size: number
@Field({})
unit_type: string
}
My graphql play ground only show
type ResultIngredientDto {
_id: ID!
ingredient_name: String!
}
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
装饰器,这样你就可以添加
@Field(() => [suppliers])
英文:
suppliers
property also needs @Field
decorator so you can add
@Field(() => [suppliers])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论