Skip to content

Commit

Permalink
Merge pull request #173 from ronaldcurtis/main
Browse files Browse the repository at this point in the history
fix: enable overriding the default cursor for an edge [#152] [#161]
  • Loading branch information
benwilson512 authored Dec 29, 2020
2 parents 5131035 + 05ab2cb commit 6b8a334
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
6 changes: 4 additions & 2 deletions lib/absinthe/relay/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,9 @@ defmodule Absinthe.Relay.Connection do
offset = offset || 0
first = offset_to_cursor(offset)
edge = build_edge(item, first)
{edges, last} = do_build_cursors(items, offset + 1, [edge], first)
{edges, _} = do_build_cursors(items, offset + 1, [edge], first)
first = edges |> List.first() |> get_in([:cursor])
last = edges |> List.last() |> get_in([:cursor])
{edges, first, last}
end

Expand All @@ -581,7 +583,7 @@ defmodule Absinthe.Relay.Connection do
defp build_edge({item, args}, cursor) do
args
|> Enum.flat_map(fn
{key, _} when key in [:cursor, :node] ->
{key, _} when key in [:node] ->
Logger.warn("Ignoring additional #{key} provided on edge (overriding is not allowed)")
[]

Expand Down
33 changes: 19 additions & 14 deletions test/lib/absinthe/relay/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -578,20 +578,25 @@ defmodule Absinthe.Relay.ConnectionTest do

describe "when provided with a cursor as an edge arg" do
setup do
[record: {%{name: "Dan"}, %{role: "contributor", cursor: :bad}}]
end

test "it will ignore the additional cursor", %{record: record} do
capture_log(fn ->
{:ok, %{edges: [%{cursor: cursor} | _]}} = Connection.from_list([record], %{first: 1})
assert cursor == "YXJyYXljb25uZWN0aW9uOjA="
end)
end

test "it will log a warning", %{record: record} do
assert capture_log(fn ->
Connection.from_list([record], %{first: 1})
end) =~ "Ignoring additional cursor provided on edge"
[
records: [
{%{name: "Dan"}, %{role: "contributor", cursor: "start_cursor"}},
{%{name: "Bob"}, %{role: "contributor", cursor: "end_cursor"}}
]
]
end

test "it will override the default cursor", %{records: records} do
assert(
{:ok,
%{
edges: [%{cursor: "start_cursor"}, %{cursor: "end_cursor"}],
page_info: %{
start_cursor: "start_cursor",
end_cursor: "end_cursor"
}
}} = Connection.from_list(records, %{first: 2})
)
end
end

Expand Down
1 change: 0 additions & 1 deletion test/lib/absinthe/relay/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ defmodule Absinthe.Relay.SchemaTest do
end
end


defmodule SchemaCustomIdType do
use Absinthe.Schema
use Absinthe.Relay.Schema, :classic
Expand Down

0 comments on commit 6b8a334

Please sign in to comment.