Skip to content

Commit 9d23745

Browse files
committed
Merge pull request #15 from graphql-elixir/bugfix/multiple-selects-on-connection
Don't bomb if the query passed into an ecto association already includes a select value.
2 parents fa7754f + cc87a65 commit 9d23745

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/graphql/relay/connection/ecto.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if Code.ensure_loaded?(Ecto) do
3030
nil -> false
3131
_ ->
3232
first_limit = first + 1
33-
has_more_records_query = from things in query, limit: ^first_limit
33+
has_more_records_query = remove_select(from things in query, limit: ^first_limit)
3434
has_more_records_query = from things in has_more_records_query, select: count(things.id)
3535
repo.one(has_more_records_query) > first
3636
end
@@ -39,7 +39,7 @@ if Code.ensure_loaded?(Ecto) do
3939
nil -> false
4040
_ ->
4141
last_limit = last + 1
42-
has_prev_records_query = from things in query, limit: ^last_limit
42+
has_prev_records_query = remove_select(from things in query, limit: ^last_limit)
4343
has_prev_records_query = from things in has_prev_records_query, select: count(things.id)
4444
repo.one(has_prev_records_query) > last
4545
end
@@ -114,8 +114,15 @@ if Code.ensure_loaded?(Ecto) do
114114
end
115115

116116
def connection_count(repo, query) do
117+
query = remove_select(query)
117118
count_query = from things in query, select: count(things.id)
118119
repo.one(count_query)
119120
end
121+
122+
# Remove select if it exists so that we avoid `only one select
123+
# expression is allowed in query` Ecto exception
124+
defp remove_select(query) do
125+
%{ query | select: nil }
126+
end
120127
end
121128
end

test/graphql/relay/connection/ecto_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ defmodule GraphQL.Relay.Connection.EctoTest do
5959
Enum.at(letters, 4)
6060
end
6161

62+
test "querying for counts does not raise exception if select already exists" do
63+
query = letters_query
64+
|> select([l], %{id: l.id, letter: l.letter})
65+
assert(Connection.Ecto.resolve(query, %{repo: Repo}))
66+
end
67+
6268
test "basic slicing: returns all elements without filters" do
6369
expected = %{
6470
edges: [

0 commit comments

Comments
 (0)