Skip to content

Instantly share code, notes, and snippets.

@gottfrois
Last active April 13, 2020 10:01
Show Gist options
  • Save gottfrois/a10d266615b9a7fb71090c362b6a0dfb to your computer and use it in GitHub Desktop.
Save gottfrois/a10d266615b9a7fb71090c362b6a0dfb to your computer and use it in GitHub Desktop.
Code snippet in Stream Paginated GraphQL API in Elixir medium blog post
defmodule MyApp.StreamPaginator do
def stream(query, args \\ %{first: 100}) do
# ...
next = &next/1
# ...
end
# ...
defp next({nil, _query, _args} = acc), do: {:halt, acc}
defp next({%{"hasNextPage" => false}, _query, _args} = acc), do: {:halt, acc}
defp next({%{"endCursor" => cursor}, query, args}) do
query
|> request(%{first: args[:first], after: cursor})
|> next()
end
defp next({nodes, page, query, args}) do
{nodes, {page, query, args}}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment