Hi guys,
Looking at GraphQL specification about pagination here, I see they have a totalCount field on the connection which graphene currently does not have
{
hero {
name
friendsConnection(first:2 after:"Y3Vyc29yMQ==") {
totalCount
edges {
node {
name
}
cursor
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
Will it be possible to add totalCount field to connections or to the pageInfo?
BTW, good job with the docs and version 1.0
At the moment, connections are implemented according to the Relay cursor spec - which doesn't mention totalCount. You can create your own connection class (and field) if you want to add it
thanks for your response @mjtamlyn,
can you please give me an example on how will I get the framework to use my own connection class?
Need to debug to make sure it works but should be something like
class MyConnection(Connection):
class Meta:
node = UserType
total_count = Int()
some_query = ConnectionField(MyConnection)
def resolve_some_query(self, *_):
edges = .. create edges from query ...
total_count = .. calculate total ...
return MyConnection
total_count=total_count,
edges=edges,
page_info=PageInfo(
start_cursor=start_cursor.urlsafe() if start_cursor else '',
end_cursor=end_cursor,
has_previous_page=has_previous_page,
has_next_page=ndb_iter.has_next()
)
)
Eran example should now work.
I added also a testcase to ensure resolvers can return connection instances: c7929234294a181993158b4439c24006e9ed1ebd
Thanks man. Nice work!
On Oct 1, 2016 1:54 PM, "Syrus Akbary" [email protected] wrote:
Eran example should now work.
I added also a testcase to ensure that resolvers can return connection
instances: c792923
https://github.com/graphql-python/graphene/commit/c7929234294a181993158b4439c24006e9ed1ebd—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/graphql-python/graphene/issues/307#issuecomment-250926732,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGqlaQN74U8TZLWPriIDS78jYeNB5yJaks5qvp5ogaJpZM4KLJJV
.
Is there a way to resolve total_count within the MyConnection class?
from graphene import ObjectType, Connection, Node, Int
from graphene_django import DjangoObjectType
from graphene_django.fields import DjangoConnectionField
from ..models import MyObject
class MyConnection(Connection):
class Meta:
abstract = True
total_count = Int()
def resolve_total_count(root, info, **kwargs):
return root.length
class MyObjectType(DjangoObjectType):
class Meta:
model = MyObject
interfaces = (Node, )
connection_class = MyConnection
class Query(ObjectType):
my_objects = DjangoConnectionField(MyObjectType)
In my case, this problem was solved by using this custom Connection.
import graphene
class MyConnection(graphene.relay.Connection):
class Meta:
abstract = True
total_count = graphene.Int()
def resolve_total_count(root, info):
return len(root.iterable) # And no, root.iterable.count() did not work for me.
What is great about this is that if I replace return len(root.iterable) with return 0, django-debug-toolbar tells me that I still have the same amount of queries in both cases. So, no N+1 problem. And, I can use the ConnectionField rather than DjangoConnectionField in my Query classes.
Most helpful comment
Is there a way to resolve total_count within the MyConnection class?