Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/graphql/relay/connection/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if Code.ensure_loaded?(Ecto) do
nil -> false
_ ->
first_limit = first + 1
has_more_records_query = from things in query, limit: ^first_limit
has_more_records_query = remove_select(from things in query, limit: ^first_limit)
has_more_records_query = from things in has_more_records_query, select: count(things.id)
repo.one(has_more_records_query) > first
end
Expand All @@ -39,7 +39,7 @@ if Code.ensure_loaded?(Ecto) do
nil -> false
_ ->
last_limit = last + 1
has_prev_records_query = from things in query, limit: ^last_limit
has_prev_records_query = remove_select(from things in query, limit: ^last_limit)
has_prev_records_query = from things in has_prev_records_query, select: count(things.id)
repo.one(has_prev_records_query) > last
end
Expand Down Expand Up @@ -114,8 +114,15 @@ if Code.ensure_loaded?(Ecto) do
end

def connection_count(repo, query) do
query = remove_select(query)
count_query = from things in query, select: count(things.id)
repo.one(count_query)
end

# Remove select if it exists so that we avoid `only one select
# expression is allowed in query` Ecto exception
defp remove_select(query) do
%{ query | select: nil }
end
end
end
6 changes: 6 additions & 0 deletions test/graphql/relay/connection/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ defmodule GraphQL.Relay.Connection.EctoTest do
Enum.at(letters, 4)
end

test "querying for counts does not raise exception if select already exists" do
query = letters_query
|> select([l], %{id: l.id, letter: l.letter})
assert(Connection.Ecto.resolve(query, %{repo: Repo}))
end

test "basic slicing: returns all elements without filters" do
expected = %{
edges: [
Expand Down