Giter Club home page Giter Club logo

graphene-django-pagination's Introduction

graphene-django-pagination

This package adds offset-based pagination to Graphene-Django without using Graphene Relay.

Installation

Using Pipenv:

$ pipenv install https://github.com/instruct-br/graphene-django-pagination/archive/master.zip

Documentation

Fields:

DjangoPaginationConnectionField

    1. It allows paginate the query using offset-based method and returns the totalCount field that indicates the total query results.
    1. Also, it is possible to order list using the pattern input,enum just sendind ordering field.

Example

1 - Model (models.py)

from django.db import models


class Customer(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    name = models.CharField(max_length=100)

2 - Type (types.py)

from graphene_django.types import DjangoObjectType
from .models import Customer


class CustomerType(DjangoObjectType):
    class Meta:
        model = Customer
        filter_fields = {
            "name": ["istartswith", "exact"]
        }

3 - Schema (schema.py)

from graphene_django_pagination import DjangoPaginationConnectionField
from graphene import ObjectType
from .types import CustomerType
from .models import Customer


class Query(ObjectType):
    customers = DjangoPaginationConnectionField(CustomerType)

    def resolve_customers(self, info, **kwargs):
        return Customer.objects.all()

4 - Queries

4.1 - Query without limit, offset and filter
query customers {
  customers {
    totalCount
    results {
      id
      name
    }
  }
}
{
    "data": {
        "customers": {
            "totalCount": 6,
            "results": [
                {
                    "id": 1,
                    "name": "Figo"
                },
                {
                    "id": 2,
                    "name": "Edson Arantes do Nascimento"
                }
                {
                    "id": 3,
                    "name": "Lionel Messi"
                }
                {
                    "id": 4,
                    "name": "Ibrahimović"
                }
                {
                    "id": 5,
                    "name": "Paul Pogba"
                }
                {
                    "id": 6,
                    "name": "Eden Hazard"
                }
            ]
        }
    }
}
4.2 - Query with only limit and offset
query customers {
  customers(limit: 3, offset: 0) {
    totalCount
    results {
      id
      name
    }
  }
}
{
  "data": {
    "customers": {
      "totalCount": 6,
      "results": [
        {
          "id": 1,
          "name": "Figo"
        },
        {
          "id": 2,
          "name": "Edson Arantes do Nascimento"
        },
        {
          "id": 3,
          "name": "Lionel Messi"
        }
      ]
    }
  }
}
4.3 - Query with limit, offset and filter
query customers {
  customers(limit: 3, offset: 0, nickname_Istartswith: "E") {
    totalCount
    results {
      id
      name
    }
  }
}
{
  "data": {
    "customers": {
      "totalCount": 2,
      "results": [
        {
          "id": 2,
          "name": "Edson Arantes do Nascimento"
        },
        {
          "id": 6,
          "name": "Eden Hazard"
        }
      ]
    }
  }
}
4.4 - Query with ordering
query customers {
  customers(ordering: "name,asc") {
    totalCount
    results {
      id
      name
    }
  }
}
{
  "data": {
    "customers": {
        "totalCount": 6,
        "results": [
          {
            "id": 6,
            "name": "Eden Hazard"
          },
          {
            "id": 2,
            "name": "Edson Arantes do Nascimento"
          },
          {
            "id": 1,
            "name": "Figo"
          },
          {
            "id": 4,
            "name": "Ibrahimović"
          },
          {
            "id": 3,
            "name": "Lionel Messi"
          }
          {
            "id": 5,
            "name": "Paul Pogba"
          }
        ]
    }
  }
}
4.5 - Query with ordering, limit and offset
query customers {
  customers(ordering: "id,desc", limit: 3, offset: 0) {
    totalCount
    results {
      id
      name
    }
  }
}
{
  "data": {
    "customers": {
      "totalCount": 6,
      "results": [
        {
          "id": 6,
          "name": "Eden Hazard"
        },
        {
          "id": 5,
          "name": "Paul Pogba"
        },
        {
          "id": 4,
          "name": "Ibrahimović"
        }
      ]
    }
  }
}

graphene-django-pagination's People

Contributors

arthurzeras avatar moluwole avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.