I had a need for something similar and ended up writing a quite complex (but working!) function.
In my case, it returns the whole hierarchy of the ‘parent’ field of my ‘category’ items.
The nice thing is that it uses a single query to get the full hierarchy of an item.
//How to use ?
const chainItems = await getRelationChain(strapi,itemId,'api::category.category','parent');
//will return the whole hierarchy of items (as a flat ordered array) from the 'parent' field, for categories.
//NB: getRelationChainIds() uses a single DB request to retrieve the chain of IDs.
See this file : STRAPI V5 : Returns the full hierarchical chain of entries for the specified relation field (manyToOne) · GitHub