Describe the bug
When trying to get the total_count of a connection, I get only the number of results filtered by the pagination, not the number of objects in the connection.
Versions
graphql: 1.10.3
rails : 6.0.2.1
graphql-batch: 0.4.2
GraphQL schema
class Types::BaseConnection < GraphQL::Types::Relay::BaseConnection
field :total_count, Integer, null: false
def total_count
object.nodes&.count
end
end
class Types::BaseObject < GraphQL::Schema::Object
field_class Types::BaseField
connection_type_class BaseConnection
end
class Types::QueryType < Types::BaseObject
add_field(GraphQL::Types::Relay::NodeField)
field :users, UserType.connection_type, null: false
def users
User.all
end
end
class ApiSchema < GraphQL::Schema
query(Types::QueryType)
# Opt in to the new runtime (default in future graphql-ruby versions)
use GraphQL::Execution::Interpreter
use GraphQL::Analysis::AST
# Add built-in connections for pagination
use GraphQL::Pagination::Connections
end
GraphQL query
When querying with no pagination parameters, I get the correct amount of objects in totalCount, i.e. all the objects
query {
users {
totalCount
nodes {
id
}
}
}
{
"data": {
"users": {
"totalCount": 5,
"nodes": [
{
"id": "U2FpbG9yLTI="
},
{
"id": "U2FpbG9yLTM="
},
{
"id": "U2FpbG9yLTQ="
},
{
"id": "U2FpbG9yLTc="
},
{
"id": "U2FpbG9yLTEw"
}
]
}
}
}
When passing pagination parameters the totalCount doesn't reflect the entire connection, rather the current page
query {
users(first: 2) {
totalCount
nodes {
id
}
}
}
{
"data": {
"users": {
"totalCount": 2,
"nodes": [
{
"id": "U2FpbG9yLTI="
},
{
"id": "U2FpbG9yLTM="
}
]
}
}
}
Expected behavior
I expected total_count to return the total number of items in the connection, as pointed out in that wiki article
Actual behavior
It only returns the number of items in a page.
Thanks for opening this issue -- that doc is out-of-date, and I haven't written a better replacement yet 馃槚 . In the new pagination system in 1.10+, the methods of connection objects have changed.
items contains the "raw" collection object returned from application code (for example, a whole Array or an unpaginated ActiveRecord::Relation) nodes contains the paginated items. This value will be nodes { ... } (or edges { node { ... } }) in the GraphQL result So, you code could be updated like this:
def total_count
+ object.items&.count
- object.nodes&.count
end
Does that work for you?
Let's keep this open til I fix that doc.
Using object.items&.count works perfectly! 馃槂馃帀
Thanks for the quick reply 馃檹
I would find it very useful indeed to know more generally what are the available methods to call (be it in Types, Connection, Edge, etc.). For instance, knowing that you can access object or items is not very easy to find, as you say.
It would be great if that could be added to the guides, which I find very useful and readable compared to the _RubyDoc_ 馃檲
Most helpful comment
Thanks for opening this issue -- that doc is out-of-date, and I haven't written a better replacement yet 馃槚 . In the new pagination system in 1.10+, the methods of connection objects have changed.
itemscontains the "raw" collection object returned from application code (for example, a whole Array or an unpaginated ActiveRecord::Relation)nodescontains the paginated items. This value will benodes { ... }(oredges { node { ... } }) in the GraphQL resultSo, you code could be updated like this:
Does that work for you?
Let's keep this open til I fix that doc.