Skip to content

Commit e145be5

Browse files
committed
Merge pull request #16 from graphql-elixir/bugfix/ecto-order-by-clashing-with-counts
Bugfix/ecto order by clashing with counts
2 parents 0b5427b + ea69eab commit e145be5

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ It's important that you understand GraphQL first and then Relay second. Relay is
2424

2525
def deps do
2626
[
27-
{:graphql_relay, "~> 0.0.15"},
27+
{:graphql_relay, "~> 0.0.16"},
2828
{:plug_graphql, "~> 0.2"} # Most likely going to need this as well
2929
]
3030
end

lib/graphql/relay/connection/ecto.ex

Lines changed: 16 additions & 4 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 = remove_select(from things in query, limit: ^first_limit)
33+
has_more_records_query = make_query_countable(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 = remove_select(from things in query, limit: ^last_limit)
42+
has_prev_records_query = make_query_countable(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,15 +114,27 @@ if Code.ensure_loaded?(Ecto) do
114114
end
115115

116116
def connection_count(repo, query) do
117-
query = remove_select(query)
117+
query = make_query_countable(query)
118118
count_query = from things in query, select: count(things.id)
119119
repo.one(count_query)
120120
end
121121

122+
defp make_query_countable(query) do
123+
query
124+
|> remove_select
125+
|> remove_order
126+
end
127+
122128
# Remove select if it exists so that we avoid `only one select
123129
# expression is allowed in query` Ecto exception
124130
defp remove_select(query) do
125-
%{ query | select: nil }
131+
query |> Ecto.Query.exclude(:select)
132+
end
133+
134+
# Remove order by if it exists so that we avoid `field X in "order_by"
135+
# does not exist in the model source in query`
136+
defp remove_order(query) do
137+
query |> Ecto.Query.exclude(:order_by)
126138
end
127139
end
128140
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule GraphQL.Relay.Mixfile do
22
use Mix.Project
33

4-
@version "0.0.15"
4+
@version "0.0.16"
55
@description "Elixir implementation of Relay for GraphQL"
66
@repo_url "https://github.com/graphql-elixir/graphql_relay"
77

priv/repo/migrations/20160307040347_create_letter.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule EctoTest.Repo.Migrations.CreateLetter do
44
def change do
55
create table(:letters) do
66
add :letter, :string
7+
add :second_column, :string
78
timestamps
89
end
910
end

test/graphql/relay/connection/ecto_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule GraphQL.Relay.Connection.EctoTest do
1111

1212
schema "letters" do
1313
field :letter, :string
14+
field :second_column, :string
1415
timestamps
1516
end
1617
end
@@ -62,6 +63,7 @@ defmodule GraphQL.Relay.Connection.EctoTest do
6263
test "querying for counts does not raise exception if select already exists" do
6364
query = letters_query
6465
|> select([l], %{id: l.id, letter: l.letter})
66+
|> order_by([l], asc: l.second_column)
6567
assert(Connection.Ecto.resolve(query, %{repo: Repo}))
6668
end
6769

0 commit comments

Comments
 (0)