Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@Chopingas 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

This comment has been minimized.

Copy link

@holger-zeit holger-zeit commented Jun 19, 2017

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

@ivlevdenis

This comment has been minimized.

Copy link
Owner Author

@ivlevdenis ivlevdenis commented Jul 10, 2017

fixed/

@nuarhu

This comment has been minimized.

Copy link

@nuarhu 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

This comment has been minimized.

Copy link

@kendallroth kendallroth commented Jul 9, 2020

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