Skip to content

Instantly share code, notes, and snippets.

@moroz
Created November 26, 2020 04:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moroz/ed91f2ba5900c46bfa9c525ae8017408 to your computer and use it in GitHub Desktop.
Save moroz/ed91f2ba5900c46bfa9c525ae8017408 to your computer and use it in GitHub Desktop.
Format a pagination struct into neat GraphQL response (scrivener_ecto, absinthe)
defmodule MyAppWeb.Api.Middleware.FormatPage do
@behaviour Absinthe.Middleware
def call(%{value: %Scrivener.Page{} = page} = res, _) do
%{
entries: data,
page_number: page_number,
page_size: page_size,
total_pages: total_pages,
total_entries: total_entries
} = page
new_value = %{
data: data,
cursor: %{
page: page_number,
page_size: page_size,
total_pages: total_pages,
total_entries: total_entries
}
}
%{res | value: new_value}
end
def call(res, _) do
res
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment