This package adds offset-based pagination to Graphene-Django without using Graphene Relay.
Using Pipenv:
$ pipenv install https://github.com/instruct-br/graphene-django-pagination/archive/master.zip
Fields:
DjangoPaginationConnectionField
-
- It allows paginate the query using offset-based method and returns the
totalCount
field that indicates the total query results.
- It allows paginate the query using offset-based method and returns the
-
- Also, it is possible to order list using the pattern
input,enum
just sendindordering
field.
- Also, it is possible to order list using the pattern
from django.db import models
class Customer(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
name = models.CharField(max_length=100)
from graphene_django.types import DjangoObjectType
from .models import Customer
class CustomerType(DjangoObjectType):
class Meta:
model = Customer
filter_fields = {
"name": ["istartswith", "exact"]
}
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()
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"
}
]
}
}
}
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"
}
]
}
}
}
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"
}
]
}
}
}
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"
}
]
}
}
}
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ć"
}
]
}
}
}