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)
@Chopingas
Copy link

Chopingas commented Jun 7, 2017

I'm getting this error when quering Error: connection_resolver() takes exactly 9 arguments (11 given)

query a{
objects(orderBy:"position"){edges{ node{id,title } }
}
}

@holger-zeit
Copy link

I get the same connection_resolver() error, do you have any fix or a hint how to fix it? Thanks :)

@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