twn39 / code

:memo: 代码笔记,通过 issue 的方式记录日常遇到的问题和学习笔记
13 stars 1 forks source link

Django graphql 基于 graphene 的分页 #421

Open twn39 opened 1 year ago

twn39 commented 1 year ago

graphene 默认提供 relay 基于游标的分页,但是这种分页不适合页面跳转的情况,所以需要自定义分页逻辑:

import graphene
from graphene import relay, ObjectType, Int, List, Field
from graphene_django import DjangoObjectType
from .models import Product, Order
from django.contrib.auth.models import User
from graphene_django.debug import DjangoDebug

class ProductType(DjangoObjectType):
    class Meta:
        model = Product
        fields = ('id', 'name', 'description', 'created_at', 'updated_at')
        interfaces = (relay.Node, )
        filter_fields = {
            'name': ['exact', 'icontains', 'istartswith']
        }

class OrderType(DjangoObjectType):
    class Meta:
        model = Order
        fields = ('id', 'product', 'count', 'user', 'created_at', 'updated_at')

class UserType(DjangoObjectType):
    class Meta:
        model = User
        fields = ('id', 'username', 'email')

class ProductResultType(ObjectType):
    total_count = Int()
    page = Int()
    limit = Int()
    items = List(ProductType)

class OrderResultType(ObjectType):
    total_count = Int()
    page = Int()
    limit = Int()
    items = List(OrderType)

class Query(graphene.ObjectType):
    product = relay.Node.Field(ProductType)
    products = Field(ProductResultType, page=Int(required=True), limit=Int())
    orders = Field(OrderResultType, page=Int(required=True), limit=Int())
    debug = graphene.Field(DjangoDebug, name='_debug')

    def resolve_products(self, info, page, limit) -> ProductResultType:
        data = Product.objects.all()
        result = ProductResultType(total_count=100, page=1, limit=20, items=data)
        return result

    def resolve_orders(self, info, page, limit) -> OrderResultType:
        data = Order.objects.select_related("product", "user").all()
        result = OrderResultType(total_count=100, page=1, limit=20, items=data)
        return result

schema = graphene.Schema(query=Query)