diff --git a/lib/graphql/relay/connection/ecto.ex b/lib/graphql/relay/connection/ecto.ex index 24187d4..ef6eb3b 100644 --- a/lib/graphql/relay/connection/ecto.ex +++ b/lib/graphql/relay/connection/ecto.ex @@ -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 @@ -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 @@ -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 diff --git a/test/graphql/relay/connection/ecto_test.exs b/test/graphql/relay/connection/ecto_test.exs index 1b51a36..6f51bcc 100644 --- a/test/graphql/relay/connection/ecto_test.exs +++ b/test/graphql/relay/connection/ecto_test.exs @@ -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: [