Skip to content

Instantly share code, notes, and snippets.

@mbrochh
Last active September 24, 2023 10:45

Revisions

  1. mbrochh revised this gist Jun 24, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion 02_schema.py
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@
    class ProductType(DjangoObjectType):
    class Meta:
    model = models.Product
    interfaces = (CustomNode, )


    # Now we create a corresponding PaginatedType for that object type:
  2. mbrochh revised this gist Sep 21, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 01_utils.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    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)
  3. mbrochh revised this gist Sep 21, 2017. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  4. mbrochh created this gist Sep 21, 2017.
    18 changes: 18 additions & 0 deletions frontend.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // In your frontend, you just query your endpoint and request all the fields from the PaginatedType:

    const gql = `
    {
    products(page: 1) {
    page
    pages
    has_next
    has_prev
    objects {
    id
    name
    slug
    whatever
    }
    }
    }
    `
    29 changes: 29 additions & 0 deletions schema.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    from theartling.utils import get_paginator

    from . import models


    # Let's assume you have some ObjectType for one of your models:
    class ProductType(DjangoObjectType):
    class Meta:
    model = models.Product
    interfaces = (CustomNode, )


    # Now we create a corresponding PaginatedType for that object type:
    class ProductPaginatedType(graphene.ObjectType):
    page = graphene.Int()
    pages = graphene.Int()
    has_next = graphene.Boolean()
    has_prev = graphene.Boolean()
    objects = graphene.List(ProductType)


    class Query(object):
    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)
    19 changes: 19 additions & 0 deletions utils.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator


    def get_paginator(qs, page_size, page, paginated_type, **kwargs):
    p = Paginator(qs, page_size)
    try:
    page_obj = p.page(page)
    except PageNotAnInteger:
    page_obj = p.page(1)
    except EmptyPage:
    page_obj = p.page(p.num_pages)
    return paginated_type(
    page=page_obj.number,
    pages=p.num_pages,
    has_next=page_obj.has_next(),
    has_prev=page_obj.has_previous(),
    objects=page_obj.object_list,
    **kwargs
    )