Filtering based on deeply nested entries

Hi there :wave:

We’re building a Help Center based on GitHub - contentful/compose-starter-helpcenter-nextjs: A sample website frontend for Compose with Next.js

Our issue is creating a category page. Say /categories/billing which shows a list of all articles related to billing.

Our attempt at a query looks like this (get all Pages based on categoryId):

  const { items: helpArticlePages } = await client.getEntries<TypePage>({
    content_type: PageContentType,
    "fields.content.fields.subcategory.fields.category.sys.id": categoryId, // error: when searching on references you can not search on deeply nested entries
    "fields.content.sys.contentType.sys.id": PageContentTypes.HelpDeskArticle,
  });

Rough content model:
A Page holds a reference to a Help Article: { slug, title, content (reference to Help Article) }
A Help Article has a subcategory which belongs to a maincategory { subcategory: { maincategory } }

We get the error “When searching on references you can not search on deeply nested entries”. Do you have a suggestion for another approach to this query?
For now, we’ve ended up getting all help article pages with an include level of 3 and then filtering client-side but that seems like a real heavy query.