Updated broken code for graphene_django 2.8

ociule 2020-03-05 11:44:56 +01:00
parent b1c4868938
commit 8af66e5680

@ -1,6 +1,6 @@
For this example, we're using `DjangoObjectType` and a hypothetical django model for Place. Neither of these is necessary, and it's being included only to present a complete (almost) example. Simply replace the model assignment, and use the appropriate `ObjectType` in place of `DjangoObjectType` in the initial type declaration (`class Place_Node_Type(DjangoObjectType)` herein) for your application, and pagination will be yours! For this example, we're using `DjangoObjectType` and a django model for Place. Neither of these is necessary, and it's being included only to present a complete (almost) example. Simply replace the model assignment, and use the appropriate `ObjectType` in place of `DjangoObjectType` in the initial type declaration (`class PlaceNodeType(DjangoObjectType)` herein) for your application, and pagination will be yours!
For this example, we're going to assume a basic understanding of what `startCursor` and `endCursor`, and really, `pagination` as a concept, actually **are**, and just quickly provide you with the basics of pagination-in-graphene's **how**. For this example, we're going to assume a basic understanding of how [Relay pagination](https://graphql.org/learn/pagination/#complete-connection-model) works, and just quickly provide you with the basics of graphene relay pagination's **how**.
In a nutshell, `pagination` is implemented via graphene relay's `Connection` and `ConnectionField` pairing. With these in place, you'll see `pageInfo` available within your queries. It looks something like this: In a nutshell, `pagination` is implemented via graphene relay's `Connection` and `ConnectionField` pairing. With these in place, you'll see `pageInfo` available within your queries. It looks something like this:
@ -23,34 +23,26 @@ In addition to a `cursor` element within `edges` that will show you the cursor i
} }
``` ```
The basic implementation below provides the elements for cursor-based pagination that you'll need to do your thing in your code. Hope this helps. The basic schema below provides relay cursor-based pagination, combined with graphene_django filter capability.
``` ```
from graphene import Connection, ConnectionField, Node, Int from graphene import relay
from graphene_django import DjangoObjectType from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from <wherever your models exist> import Place from <wherever your models exist> import Place
class Place_Node_Type(DjangoObjectType): class PlaceNodeType(DjangoObjectType):
class Meta: class Meta:
model = Place model = Place
interfaces = (Node, ) interfaces = (relay.Node, )
filter_fields = {"name": ["exact", "icontains", "istartswith"]}
class Place_Connection(Connection):
class Meta:
node = Place_Node_Type
count = Int()
def resolve_count(root, info):
return len(root.edges)
class Query(object): class Query(object):
places = ConnectionField(Place_Connection) places = DjangoFilterConnectionField(PlaceNodeType)
def resolve_places(root, info, **kwargs):
return Place.objects.all()
``` ```
The following query, (the pageInfo element is included for illustration, but isn't necessary for the query to resolve): The following query, (the pageInfo element is included for illustration, but isn't necessary for the query to resolve):
``` ```
{ {