Skip to content

Instantly share code, notes, and snippets.

@moroz
Created November 26, 2020 04:21
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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