If you use a graph database, something like the GraphQL compiler project might be useful: https://github.com/kensho-technologies/graphql-compiler
Primer on how to use it: https://blog.kensho.com/compiled-graphql-as-a-database-query...
I work at Kensho and we use it with queries that are 10+ levels of nesting with no problems.