Created
November 26, 2020 04:21
-
-
Save moroz/ed91f2ba5900c46bfa9c525ae8017408 to your computer and use it in GitHub Desktop.
Format a pagination struct into neat GraphQL response (scrivener_ecto, absinthe)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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