Skip to content

Instantly share code, notes, and snippets.

@ivlevdenis
Last active September 14, 2023 17:58
Show Gist options
  • Save ivlevdenis/3fb0ede89650889cc9f62a77770e7156 to your computer and use it in GitHub Desktop.
Save ivlevdenis/3fb0ede89650889cc9f62a77770e7156 to your computer and use it in GitHub Desktop.
Django graphene orderBy
from graphene import relay, String, List
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.fields import DjangoConnectionField
from app.models import Model
class Object(DjangoObjectType):
class Meta:
model = Model
interfaces = (relay.Node,)
class OrderedDjangoFilterConnectionField(DjangoFilterConnectionField):
@classmethod
def connection_resolver(cls, resolver, connection, default_manager, max_limit,
enforce_first_or_last, filterset_class, filtering_args,
root, args, context, info):
filter_kwargs = {k: v for k, v in args.items() if k in filtering_args}
qs = default_manager.get_queryset()
qs = filterset_class(data=filter_kwargs, queryset=qs).qs
order = args.get('orderBy', None)
if order:
qs = qs.order_by(*order)
return super(DjangoFilterConnectionField, cls).connection_resolver(
resolver,
connection,
qs,
max_limit,
enforce_first_or_last,
root,
args,
context,
info
)
class Query(graphene.ObjectType):
'''
query{
objects(orderBy: ['field1', '-field2']){
...
}
}
'''
objects = OrderedDjangoFilterConnectionField(Object, orderBy=List(of_type=String))
schema = graphene.Schema(query=Query)
@ivlevdenis
Copy link
Author

fixed/

@nuarhu
Copy link

nuarhu commented Jun 21, 2018

with django-graphene==2.1rc1:

class OrderedDjangoFilterConnectionField(DjangoFilterConnectionField):
    @classmethod
    def connection_resolver(cls, resolver, connection, default_manager, max_limit,
                            enforce_first_or_last, filterset_class, filtering_args,
                            root, info, **args):
        filter_kwargs = {k: v for k, v in args.items() if k in filtering_args}
        qs = default_manager.get_queryset() if hasattr(default_manager, 'get_queryset') else default_manager
        qs = filterset_class(data=filter_kwargs, queryset=qs).qs
        order = args.get('orderBy', None)
        if order:
            qs = qs.order_by(*order)
        return super(DjangoFilterConnectionField, cls).connection_resolver(resolver, connection, qs, max_limit,
                                                                           enforce_first_or_last, root, info, **args)

Custom queries need to handle it similarly:

    def resolve_my_todos(self, info, **args):
        user = info.context.user
        if user.is_authenticated:
            qs = Todo.objects.filter(user=user)
            order = args.get('orderBy', None)
            if order:
                qs = qs.order_by(*order)
            return qs
        return Todo.objects.none()

@kendallroth
Copy link

For those looking for a more up-to-date solution, the StackOverflow answer here nicely details the approach (Graphene 2.11.1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment