4

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");
    }
   },
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    While it's technically possible to execute another GraphQL query against the same schema inside a resolver, it shouldn't be necessary. Having nested object types shouldn't prevent you from calling the DB to fetch the appropriate resource inside each field's resolver. It's not really clear what the issue that you're grappling with is -- maybe including your resolver code along with some comments would help clarify the problem? – Daniel Rearden Apr 12 '19 at 11:41
  • thnks for the reply...i cant resolve it from the db because eac typr has a nested type. for example if i will run a query that gets all details and populate the relavant fields. each of them needs to be resolved as well....catalogItem under productDetail needs to resolve supplier that needs to resolce company – Assaf Yacobi Apr 12 '19 at 11:54

0 Answers0