Django 中继 graphql 按创建日期排序

Django relay graphql order by created date

如何在 django graphene-relay 中按创建日期顺序查询模型

class CategoryNode(DjangoObjectType):
    class Meta:
        model = Category
        filter_fields = ['name', 'ingredients', 'created_at']
        interfaces = (relay.Node, )



class Query(graphene.ObjectType):
     category = relay.Node.Field(CategoryNode)
     all_categories = DjangoFilterConnectionField(CategoryNode)

我在阅读 Graphene-Django 文档后找到了解决方案 https://docs.graphene-python.org/projects/django/en/latest/filtering/

需要为模型创建过滤器集:

from django_filters import FilterSet, OrderingFilter

class CategoryFilter(FilterSet):
class Meta:
    model = Category
    fields = '__all__'

    order_by = OrderingFilter(
               fields=(('created_at'),))

class Query(graphene.ObjectType):
    category = relay.Node.Field(CategoryNode)
    all_categories = DjangoFilterConnectionField(CategoryNode, filterset_class=CategoryFilter)

def resolve_category(self, info, **kwargs):
    return CategoryFilter(kwargs).qs