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.
suppliers
property also needs @Field
decorator so you can add
@Field(() => [suppliers])