|from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator|
|# First we create a little helper function, becase we will potentially have many PaginatedTypes|
|# and we will potentially want to turn many querysets into paginated results:|
|def get_paginator(qs, page_size, page, paginated_type, **kwargs):|
|p = Paginator(qs, page_size)|
|page_obj = p.page(page)|
|page_obj = p.page(1)|
|page_obj = p.page(p.num_pages)|
|from theartling.utils import get_paginator|
|from . import models|
|# Let's assume you have some ObjectType for one of your models:|
|model = models.Product|
|# Now we create a corresponding PaginatedType for that object type:|
|page = graphene.Int()|
|pages = graphene.Int()|
|has_next = graphene.Boolean()|
|has_prev = graphene.Boolean()|
|objects = graphene.List(ProductType)|
|products = graphene.Field(ProductPaginatedType, page=graphene.Int())|
|# Now, in your resolver functions, you just query your objects and turn the queryset into the PaginatedType using the helper function:|
|def resolve_products(self, info, page):|
|page_size = 10|
|qs = models.Product.objects.all()|
|return get_paginator(qs, page_size, page, ProductPaginatedType)|
@Zagrebelin forget about
(I have updated the gist and removed that line)
I did this just to remove redundancy and make it a little neat. I feel there's more that can be done. Please let me know when you figure something out. Thanks!
how to handle this solution when you have filters in a class level filterset_class in the Query.
Basically doing this you would have to do all filters by hand and then paginate them as I haven't figured it out how to fetch in resolve methods what the filters have filtered before.
Actually, I've got lots of filters in the all app with filterset_class, so it shouldn't be nice removing all of them to do it in the resolvers side.