Mongo hasNextPage counts all records
It looks like https://jira.mongodb.org/browse/MONGOID-2325 is now closed in Mongoid version 3.0.6 so https://github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/relay/mongo_relation_connection.rb#L20 could check Mongo version if it was so inclined. I patched it in my implementation and it seems to work locally. I didn't see a clear test of this in the specs and wasn't sure how you would like it tested if I implemented it.
Thanks for taking a look! I just tried removing that bit in #2648 , but I got a failure on CI. I haven't actually looked into the failure, but is my change in that PR the same as your local patch?
Yea, that should fix it. I assume the failure is due to older versions of Mongoid before they applied the patch 🤷♂️. Calling to_a loads and does a full count of the entire collection, in my case adding 20 seconds to each page being fetched.
I posted my findings over at #2648, I'm still seeing a different value for .count vs .to_a.count. Could you share your bundle show mongoid response? I wonder if I'm still on a bad version or something.
My Mongoid version is /Users/chadwilken/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/mongoid-7.0.5. I might be looking at this wrong at second glance. I think I followed the wrong path for the MongoRelationConnection. I'm not sure why calling to_a in this case executes an incredibly slow getMore query. I re-read the original issue in Jira and it sounds like maybe it is counting all documents and not just the documents that meet the criteria. I'll try to play with this some more on my end and see if I can find a better set of repro steps and verify my local "patch" is actually correct.
@rmosolgo
in Mongoid v4.x folllowing query will respect skip and offset
def relation_count(relation)
relation.all.count(true)
end
After v5 this one will work
def relation_count(relation)
relation.all.count(relation.options.slice(:limit, :skip))
end