Skip to content

Commit

Permalink
Merge pull request #141 from zhangyuz/master
Browse files Browse the repository at this point in the history
Support for objects.order_by
  • Loading branch information
abawchen authored Jun 30, 2020
2 parents 3fda219 + 2abd300 commit e7bef85
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion graphene_mongo/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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())
Expand Down Expand Up @@ -194,7 +198,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 {}
Expand Down
9 changes: 9 additions & 0 deletions graphene_mongo/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions graphene_mongo/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class MongoengineObjectTypeOptions(ObjectTypeOptions):
registry = None # type: Registry
connection = None
filter_fields = ()
order_by = None


class MongoengineObjectType(ObjectType):
Expand All @@ -89,6 +90,7 @@ def __init_subclass_with_meta__(
connection_field_class=None,
interfaces=(),
_meta=None,
order_by=None,
**options
):

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e7bef85

Please sign in to comment.