Best approach for sorting by relationship count

For me I am trying to select blog post categories/tags with a limit of 5 and ordered by how many blog posts are assigned to them. I am doing this to show a list of categories that are most commonly used rather than showing a massive list.

To achieve this, since there isn’t a built in way yet, I query the categories and populate it’s blogPosts relation field but only select the id property in it’s fields (I don’t need any actual data from the blog posts in this instance). Then I sort the results in code by length of blogPosts reversed (desc order) and then select only 5 of them for return.

It’s not idea as the query is returning back an array of blog posts for each category (albeit only with an id field) and I’ve got to do some mapping code.

Really Strapi should allow for a special count property when filtering and sorting by relationship fields and then it’s a simple API request only.