Query to identify parent in Query on the M2O, O2M, M2M, M2A relation

Context: If we have an item from the collection A, which it is related to the collections N, then return all the collections N where this item is related until find the top parent.

In the middle there are many relationship that could be related through differents kind of relationship.

How can I do that?

Many of my relationship are made of M2A.

I found this as well https://discord.com/channels/725371605378924594/1019641731332976722/threads/1034741934415167498

I tried with this API in a descending order:
https://discord.com/channels/725371605378924594/1019641731332976722/threads/1034741934415167498