Skip to content

Instantly share code, notes, and snippets.

@gottfrois
Last active April 13, 2020 10:01
Show Gist options
  • Save gottfrois/3bbabf9c77192e1c7944cbba0d8f3bec to your computer and use it in GitHub Desktop.
Save gottfrois/3bbabf9c77192e1c7944cbba0d8f3bec 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
init = fn -> request(query, args) end
next = fn -> nil end
stop = fn -> nil end
Stream.resource(init, next, stop)
end
defp request(query, args) do
query
|> HttpClient.post(%{variables: args})
|> case do
{:ok,
%{"data" => %{"records" => %{"edges" => edges, "pageInfo" => page}}}} ->
{Enum.map(edges, & &1["node"]), page, query, args}
{:error, _reason} ->
{[], nil, query, args}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment