Web service APIs sometimes use pagination, where parameters to the Web service call indicate what page to retrieve. These can be roughly divided into two types:
Ones where the parameters to request a page are independent of any given page response (e.g., "give me page #3, with a page size of 10")
Ones where the parameters to request a page are dependent on some previous page response (e.g., "give me the next 10 items after the item with an identifier of
foo
)
This SO answer covers the first scenario nicely, where the Web service just needs a page number and all we need to determine from any given page's response is whether or not we are done.
This SO answer covers the second scenario, but it relies upon recursion, and so for large data sets we will die with a StackOverflowError
.
A Relay-compatible GraphQL-powered Web service (e.g., GitHub's API) will make heavy use of the second scenario, as Relay's specification for pagination requires you to provide a "cursor" from a previous response to get the next items after that cursor position. So, I am trying to figure out a non-recursive approach for this, that still wraps everything up into a single master Observable
, the way those two answers do.