Skip to content

Instantly share code, notes, and snippets.

@smoak
Last active March 29, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoak/61f894fea1fac47b2e96349ca85a4c62 to your computer and use it in GitHub Desktop.
Save smoak/61f894fea1fac47b2e96349ca85a4c62 to your computer and use it in GitHub Desktop.
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
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