Skip to content

Commit

Permalink
fix: ensure page info cursors match the first and last edge cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldcurtis committed Dec 29, 2020
1 parent 5ad8948 commit 90faeef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion 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 Down
25 changes: 19 additions & 6 deletions test/lib/absinthe/relay/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +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: "custom_cursor"}}]
end

test "it will override the default cursor", %{record: record} do
{:ok, %{edges: [%{cursor: cursor} | _]}} = Connection.from_list([record], %{first: 1})
assert cursor == "custom_cursor"
[
records: [
{%{name: "Dan"}, %{role: "contributor", cursor: "first_custom_cursor"}},
{%{name: "Bob"}, %{role: "contributor", cursor: "end_custom_cursor"}}
]
]
end

test "it will override the default cursor", %{records: records} do
assert(
{:ok,
%{
edges: [%{cursor: "first_custom_cursor"}, %{cursor: "end_custom_cursor"}],
page_info: %{
start_cursor: "first_custom_cursor",
end_cursor: "end_custom_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 90faeef

Please sign in to comment.