Last active
March 29, 2018 21:09
-
-
Save smoak/61f894fea1fac47b2e96349ca85a4c62 to your computer and use it in GitHub Desktop.
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 Blog do | |
def fetch(limit, offset \\ 0) do | |
IO.inspect(limit: limit, offset: offset) | |
posts = 1..1000 |> Enum.map(fn i -> %{body: "body #{i}"} end) | |
%{ | |
total_count: length(posts), | |
posts: posts |> Enum.slice(offset || 0, limit) | |
} | |
end | |
end |
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 MySchema do | |
use Absinthe.Schema | |
use Absinthe.Relay.Schema, :modern | |
object :post do | |
field :body, non_null(:string) | |
end | |
connection node_type: :post do | |
@desc "The total number of posts." | |
field(:total_count, non_null(:integer)) | |
edge do | |
end | |
end | |
object :blog do | |
connection field :posts, node_type: :post, paginate: :forward do | |
resolve(fn pagination_args, %{source: _blog} -> | |
{:ok, :forward, limit} = Absinthe.Relay.Connection.limit(pagination_args) | |
offset = Absinthe.Relay.Connection.offset(pagination_args) | |
%{posts: posts, total_count: total_count} = Blog.fetch(limit, offset) | |
{:ok, conn} = posts | |
|> Absinthe.Relay.Connection.from_slice(offset) | |
{:ok, conn |> Map.merge(%{total_count: total_count})} | |
end) | |
end | |
end | |
query do | |
field :blog, non_null(:blog) do | |
resolve(fn _, _, _ -> {:ok, %{}} end) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment