Django graphql 基于 graphene 的分页
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论