I'm using the following code and would like to to have variable "count" (number of total results) in my response – but I have no idea on how to solve it (already did quite some research).
class EntryNode(DjangoNode):
class Meta:
model = Entry
class Query(ObjectType):
all_entries = DjangoFilterConnectionField(EntryNode)
class Meta:
abstract = True
Query
{allEntries(first:5) {
pageInfo{hasPreviousPage, hasNextPage},
edges{node{id,user{id,username},title,category{id,name}}}
}}
Results
I'd like to have an additional variable "count" (either within "allEntries" or even better as a part of "pageInfo")
{
"data": {
"allEntries": {
"pageInfo": {
"hasPreviousPage": false,
"hasNextPage": false,
"count": ???,
},
"count": ???,
"edges": [
{
"node": {
"id": "RW50cnlOb2RlOjUy",
...
}
}
]
}
}
}
Please Note: I'm not even sure this is related to graphene ... any hints are highly appreciated.
Hi @sehmaschine, this is how I approached the same issue in swapi-graphene.
Example swapi-graphene totalCount query.
{
myFavoriteFilm: film(id:"RmlsbToz") {
id
title
episodeId
characters(first:5) {
totalCount
edges {
node {
name
}
}
}
}
}
Implementation: https://github.com/graphql-python/swapi-graphene/blob/master/starwars/schema.py#L18-L22
And then use connection_type in your EntryNode:
class EntryNode(DjangoNode):
class Meta:
model = Entry
connection_type = Connection
Hope it helps!
[Reopen this issue if you run into some errors or have more questions in the process].
@syrusakbary thanks a lot – it works now. but I do have another minor issue: when I'm filtering the query (e.g. with something like name_Icontains) the counter returns the number of filtered items. do you have an idea how I'm able to get all items (independent of any filters applied)?
I would override total count on your connection to return Model.objects.count()
Btw, this doesn't work on the sqlalchemy side. I have a PR in there to fix it and make the total_count and iterable available like it is on the django side:
https://github.com/graphql-python/graphene-sqlalchemy/pull/36
I arrived here by Google first but ultimately found the total_count implementation at this thread easier to follow and use: https://github.com/graphql-python/graphene-django/issues/162
from graphene import Int
class ProductNode(DjangoObjectType):
class Meta:
model = Product
filter_fields = {
'name': ['exact', 'icontains', 'istartswith'],
}
interfaces = (relay.Node, )
@classmethod
def get_connection(cls):
class CountableConnection(relay.Connection):
total_count = Int()
class Meta:
name = '{}Connection'.format(cls._meta.name)
node = cls
@staticmethod # Redundant since Graphene kinda does this automatically for all resolve_ methods.
def resolve_total_count(root, args, context, info):
return root.length
return CountableConnection
@Vitiell0 This worked well for me. However I had to modify how the resolver was working since it was crashing my test server on cases >100K records while attempting to count the edges.
I ended up just using a SQLA statement filtered with the current_user and returned with .count().
Most helpful comment
Hi @sehmaschine, this is how I approached the same issue in swapi-graphene.
Example swapi-graphene totalCount query.
Implementation: https://github.com/graphql-python/swapi-graphene/blob/master/starwars/schema.py#L18-L22
And then use connection_type in your EntryNode:
Hope it helps!
[Reopen this issue if you run into some errors or have more questions in the process].