From 2abd300a5e5af1fc32a1b116a319442578d611bf Mon Sep 17 00:00:00 2001 From: Zhang Yuzheng Date: Thu, 19 Mar 2020 22:55:25 +0800 Subject: [PATCH] feat: Support for objects.order_by Use it like this: class XXX(MongoengineObjectType): class Meta: model:SomeModel interfaces:(Node,) order_by: 'some order_by clause used by SomeeModel.object().order_by' That's all, try it out. Signed-off-by: Zhang Yuzheng --- graphene_mongo/fields.py | 6 +++++- graphene_mongo/tests/test_types.py | 9 +++++++++ graphene_mongo/types.py | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/graphene_mongo/fields.py b/graphene_mongo/fields.py index 7a3d844b..d4081558 100644 --- a/graphene_mongo/fields.py +++ b/graphene_mongo/fields.py @@ -50,6 +50,10 @@ def node_type(self): def model(self): return self.node_type._meta.model + @property + def order_by(self): + return self.node_type._meta.order_by + @property def registry(self): return getattr(self.node_type._meta, "registry", get_global_registry()) @@ -182,7 +186,7 @@ def get_queryset(self, model, info, **args): return queryset_or_filters else: args.update(queryset_or_filters) - return model.objects(**args) + return model.objects(**args).order_by(self.order_by) def default_resolver(self, _root, info, **args): args = args or {} diff --git a/graphene_mongo/tests/test_types.py b/graphene_mongo/tests/test_types.py index 2fddd4cd..9d3e4b35 100644 --- a/graphene_mongo/tests/test_types.py +++ b/graphene_mongo/tests/test_types.py @@ -129,6 +129,15 @@ class Meta: assert "headline" not in list(A._meta.fields.keys()) +@with_local_registry +def test_mongoengine_objecttype_order_by(): + class A(MongoengineObjectType): + class Meta: + model = Article + order_by = "some_order_by_statement" + assert "some_order_by_statement" not in list(A._meta.fields.keys()) + + @with_local_registry def test_passing_meta_when_subclassing_mongoengine_objecttype(): class TypeSubclassWithBadOptions(MongoengineObjectType): diff --git a/graphene_mongo/types.py b/graphene_mongo/types.py index a676dc1a..ad68a1c5 100644 --- a/graphene_mongo/types.py +++ b/graphene_mongo/types.py @@ -71,6 +71,7 @@ class MongoengineObjectTypeOptions(ObjectTypeOptions): registry = None # type: Registry connection = None filter_fields = () + order_by = None class MongoengineObjectType(ObjectType): @@ -89,6 +90,7 @@ def __init_subclass_with_meta__( connection_field_class=None, interfaces=(), _meta=None, + order_by=None, **options ): @@ -155,6 +157,7 @@ def __init_subclass_with_meta__( # Save them for later _meta.only_fields = only_fields _meta.exclude_fields = exclude_fields + _meta.order_by = order_by super(MongoengineObjectType, cls).__init_subclass_with_meta__( _meta=_meta, interfaces=interfaces, **options