I'm working on some code that takes search criteria from a Rest API and uses it to query a remote API to return results.  As an exercise, I wanted to try to separate all state changes to one place, as a language like Haskell would force you to (even though I'm programming in Java in this example).At first this was straight forward:  Separate the code that generates the query from the code that executes the query.  The code that executes the query can be ignorant of the query it gets.  But now I have a new requirement that works like this:  If someone searches for all things under a parent folder, then I need to run two queries.  One to find the path of that parent folder then another to find all things that start with that path.  I cannot use subqueries to do this in one query, the remote api does not allow it.I can't figure out how I could cleanly design this.  For this special case, the first query needs to get outside the pure code, be executed, then the result needs to be passed in to different pure code so the result can be executed as the second query.How would this be cleanly designed in a functional programming language?  Keep in mind, tomorrow I may get a new requirement that requires me to run 3 queries to get the final result.  
How would I isolate changes to mutable state if I need to run two queries to get the final result?
java;functional programming;immutability