Skip to content

Commit

Permalink
Merge pull request #838 from rmosolgo/fix-relation-count
Browse files Browse the repository at this point in the history
fix(RelationConnection) correctly count when ActiveRecord has already added an alias
  • Loading branch information
Robert Mosolgo committed Jul 14, 2017
2 parents 0e9e879 + 210cc7a commit feb5ba4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/graphql/relay/relation_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ def relation_limit(relation)

# If a relation contains a `.group` clause, a `.count` will return a Hash.
def relation_count(relation)
count_or_hash = relation.count
count_or_hash = case relation
when ActiveRecord::Relation
relation.count(:all)
else # eg, Sequel::Dataset, don't mess up others
relation.count
end
count_or_hash.is_a?(Integer) ? count_or_hash : count_or_hash.length
end

Expand Down
8 changes: 8 additions & 0 deletions spec/graphql/relay/relation_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ def get_last_cursor(result)
end

describe "for an ActiveRecord::Relation" do
describe "#has_next_page" do
it "handles joined, aliased relations" do
relation = StarWars::Base.select("id AS crazy_id")
connection = GraphQL::Relay::RelationConnection.new(relation, { first: 1 })
assert connection.has_next_page
end
end

describe "#edge_nodes" do
it "returns the nodes for the current page" do
# Offset
Expand Down

0 comments on commit feb5ba4

Please sign in to comment.