I am currently getting the categories from woocommerce and I came upon this question. There are parent categories and child categories. Now I created 2 endpoints.
getParentCategories
and getCategoriesByParentId
to get the child categories.
I realized that I can also combine these two and make 1 endpoint out of it. Then you return the data like this:
[
{
"category_name": "Shoes",
"category_slug: "/shoes",
"child_categories":[
{
"category_name": "Sneakers",
"category_slug: "/sneakers",
}
]
}
]
What is best practice? Thanks!
2
Answers
I would suggest maintaining them separately for separation of concern. If you are handling them separately then if something goes wrong with one of the them the other one won’t be affected.
My thought process is to try and keep your requests as small as possible.
Crazy idea:
Why not create a custom endpoint using the REST Api and return a json which contains the parent and child categories?
The benefit to me is that you will only use 1 request to fetch all the data + you have control of what is in the JSON (as you’re the one who built the custom endpoint).