I am implementing an apollo server graphql schema. All my schema definition are modules in .graphql files. All my resolvers are modules in .js files.
I have the following type :
productSchema.graphql
type Product {
_id: Int
company: Company
productSellingPrice: [PriceHistoryLog]
productName: String
category: String
productDetails: [ProductDetail]
globalId: Int
isActive: Boolean
}
extend type Query {
ProductList: [Product]
}
productDetailSchema.graphql
type ProductDetail {
_id: Int
company: Company
root: Product
catalogItem: CatalogItem
product: Product
isPerishable: Boolean
quantity: Float
isActive: Boolean
}
extend type Query {
ProductDetailsList(productId: Int!): [ProductDetail]
}
What I want to do is, when querying for ProductList, to run a ProductDetailsList query and resolve the field in product from there.
As you can see ProductDetail also have nested fields so I can't just query the DB for that field in the Product resolver.
Any ideas? I am kind of lost.
Edit:
this is my resolver code:
Product: {
company: product => product.companyId,
category: async product => {
try {
let res = await SaleModel.findOne({ productName:
product.productName }) ;
return res.productCategory;
} catch (err) {
console.log(err);
return "Mo Category found";
}
}
},
Query: {
async ProductList(obj, args, { companyId }) {
return await ProductModel.find({
companyId,
isActive: true
}).populate("companyId");
}
},