<aside> ❗ Notion Tip: From this queries you can fetch all data at once or you can call by id and fetch one layer by one

</aside>

Generic

All At once

Here I’m giving an example where from parent category to subcategories parent category Beverage → (child categories) Electrolyte drinks → (child categories) soft drinks →(child categories) fruit drinks →(child categories) fruit juice → .

<aside> ❗ This is only for example we will change the connection later before app team start work

</aside>

Query

const query = groq`*[_type == "accordionMenu"]{
  ...,
  multipleCategories[]->{
      ...,
      multisubCategories[]->{
        ...,
      multisubCategories[]->{
        ...,
        multisubCategories[]->{
          ...,
          multisubCategories[]->,
          "products": *[_type == "products" && references(^._id)]
        },
        "products": *[_type == "products" && references(^._id)]
      },
      "products": *[_type == "products" && references(^._id)]
      },
      "products": *[_type == "products" && references(^._id)]
    
  },
  "products": *[_type == "products" && references(^._id)]
}`

Dynamically fetch data

Query

const query = groq`*[_type == "accordionMenu"]{
  ...,
   multipleCategories[]->,
  "products": *[_type == "products" && references(^._id)]
}`

// Fetching the subcategories dynamically by _id from multipleCategories
const subcategories = groq`*[_type == "category" && _id == "34122b80-4034-48b7-81b2-5d58f770bbee" ]{ 
  ...,
  multisubCategories[]->,
  "products": *[_type == "products" && subCategory._ref == "34122b80-4034-48b7-81b2-5d58f770bbee"] 
}

Here as many times you will provide the multisubCategories _id you will get the same results