Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/915
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Apr 22, 2024
1 parent ff978d4 commit aae6f6a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion deps.hex
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ geo = "~> 3.3"
# grumble = "~> 0.1.3"
zest = "~> 0.1"
dataloader = "~> 2.0.0"
absinthe_relay = "~> 1.5.2"
absinthe_relay = "~> 1.5.2"
62 changes: 37 additions & 25 deletions lib/graphql/pagination.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Bonfire.API.GraphQL.Pagination do
import Bonfire.Common.Config, only: [repo: 0]
import Untangle

def pagination_args_filter(args) do
# {pagination_args, filters} =
Expand All @@ -8,7 +9,7 @@ defmodule Bonfire.API.GraphQL.Pagination do
|> Keyword.split([:after, :before, :first, :last])
end

def connection_paginate(list, args, repo_fun \\ &Repo.all/1)
def connection_paginate(list, args, opts \\ [])

def connection_paginate(
%{
Expand All @@ -22,14 +23,15 @@ defmodule Bonfire.API.GraphQL.Pagination do
} = page_info
},
_args,
repo_fun
opts
) do
IO.inspect(page_info)
# IO.inspect(page_info)
# best option, since doesn't use offset
# Absinthe.Relay.Connection.from_slice(edges, end_cursor)
{:ok,
%{
edges: build_edges(edges, cursor_for_record_fun),
edges:
build_edges(edges, Keyword.put(opts, :cursor_for_record_fun, cursor_for_record_fun)),
page_info:
Map.merge(page_info, %{
# page_count == limit,
Expand All @@ -39,46 +41,46 @@ defmodule Bonfire.API.GraphQL.Pagination do
}}
end

def connection_paginate(%Ecto.Query{} = query, args, repo_fun) do
def connection_paginate(%Ecto.Query{} = query, args, opts) do
# simple limit + offset
Absinthe.Relay.Connection.from_query(query, repo_fun, args)
Absinthe.Relay.Connection.from_query(query, opts[:repo_fun] || (&Repo.all/1), args)
end

def connection_paginate(list, args, _repo_fun) when is_list(list) do
def connection_paginate(list, args, _opts) when is_list(list) do
# need to provide the full list
Absinthe.Relay.Connection.from_list(
list,
args
)
end

defp build_cursors(items, cursor_for_record_fun \\ nil)
defp build_cursors(items, opts \\ [])
defp build_cursors([], _), do: {[], nil, nil}

defp build_cursors(items, cursor_for_record_fun) do
edges = build_edges(items, cursor_for_record_fun)
defp build_cursors(items, opts) do
edges = build_edges(items, opts)
first = edges |> List.first() |> get_in([:cursor])
last = edges |> List.last() |> get_in([:cursor])
{edges, first, last}
end

defp build_edges(items, cursor_for_record_fun \\ nil)
defp build_edges(items, opts \\ [])
defp build_edges([], _), do: []

defp build_edges([item | items], cursor_for_record_fun) do
edge = build_edge(item, cursor_for_record_fun)
{edges, _} = do_build_cursors(items, [edge], edge[:cursor], cursor_for_record_fun)
defp build_edges([item | items], opts) do
edge = build_edge(item, opts)
{edges, _} = do_build_cursors(items, [edge], edge[:cursor], opts)
edges
end

defp do_build_cursors([], edges, last, _), do: {Enum.reverse(edges), last}

defp do_build_cursors([item | rest], edges, _last, cursor_for_record_fun) do
edge = build_edge(item, cursor_for_record_fun)
do_build_cursors(rest, [edge | edges], edge[:cursor], cursor_for_record_fun)
defp do_build_cursors([item | rest], edges, _last, opts) do
edge = build_edge(item, opts)
do_build_cursors(rest, [edge | edges], edge[:cursor], opts)
end

defp build_edge({item, args}, cursor_for_record_fun) do
defp build_edge({item, args}, opts) do
args
|> Enum.flat_map(fn
{key, _} when key in [:node] ->
Expand All @@ -88,17 +90,27 @@ defmodule Bonfire.API.GraphQL.Pagination do
{key, val} ->
[{key, val}]
end)
|> Enum.into(build_edge(item, cursor_for_record_fun))
|> Enum.into(build_edge(item, opts))
end

defp build_edge(item, cursor_for_record_fun) do
defp build_edge(item, opts) do
opts
|> IO.inspect(label: "opts")

cursor_for_record_fun = opts[:cursor_for_record_fun] || (&Enums.id/1)

item =
if item_fun = opts[:item_prepare_fun] do
item_fun.(item)
|> IO.inspect(label: "item1")
else
item
|> IO.inspect(label: "item2")
end

%{
node: item,
cursor:
if(is_function(cursor_for_record_fun, 1),
do: cursor_for_record_fun.(item),
else: Paginator.cursor_for_record(item, [:id])
)
cursor: cursor_for_record_fun.(item)
}
end

Expand Down

0 comments on commit aae6f6a

Please sign in to comment.