From 8af66e5680252873f66c022546555e9390f1f6ed Mon Sep 17 00:00:00 2001 From: ociule Date: Thu, 5 Mar 2020 11:44:56 +0100 Subject: [PATCH] Updated broken code for graphene_django 2.8 --- Relay-Pagination-example.md | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/Relay-Pagination-example.md b/Relay-Pagination-example.md index 7b14a02..e23900a 100644 --- a/Relay-Pagination-example.md +++ b/Relay-Pagination-example.md @@ -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: @@ -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_django import DjangoObjectType +from graphene import relay +from graphene_django.types import DjangoObjectType +from graphene_django.filter import DjangoFilterConnectionField + from import Place -class Place_Node_Type(DjangoObjectType): +class PlaceNodeType(DjangoObjectType): class Meta: model = Place - interfaces = (Node, ) - - -class Place_Connection(Connection): - class Meta: - node = Place_Node_Type - count = Int() - - def resolve_count(root, info): - return len(root.edges) + interfaces = (relay.Node, ) + filter_fields = {"name": ["exact", "icontains", "istartswith"]} class Query(object): - places = ConnectionField(Place_Connection) - - def resolve_places(root, info, **kwargs): - return Place.objects.all() + places = DjangoFilterConnectionField(PlaceNodeType) ``` + The following query, (the pageInfo element is included for illustration, but isn't necessary for the query to resolve): ``` {