Skip to content

Commit 6f6883b

Browse files
author
Victor
committed
added before and after and maybe fixed last pagination issue.
1 parent 5aca93e commit 6f6883b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

graphene_mongo/fields.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
class MongoengineListField(Field):
1616

1717
def __init__(self, _type, *args, **kwargs):
18-
super(MongoengineListField, self).__init__(List(_type), *args, **kwargs)
18+
super(MongoengineListField, self).__init__(
19+
List(_type), *args, **kwargs)
1920

2021
@property
2122
def model(self):
@@ -42,8 +43,10 @@ def __init__(self, type, *args, **kwargs):
4243
def type(self):
4344
from .types import MongoengineObjectType
4445
_type = super(ConnectionField, self).type
45-
assert issubclass(_type, MongoengineObjectType), "MongoengineConnectionField only accepts MongoengineObjectType types"
46-
assert _type._meta.connection, "The type {} doesn't have a connection".format(_type.__name__)
46+
assert issubclass(
47+
_type, MongoengineObjectType), "MongoengineConnectionField only accepts MongoengineObjectType types"
48+
assert _type._meta.connection, "The type {} doesn't have a connection".format(
49+
_type.__name__)
4750
return _type._meta.connection
4851

4952
@property
@@ -68,10 +71,11 @@ def args(self, args):
6871
def default_filter_args(self):
6972
def is_filterable(kv):
7073
return hasattr(kv[1], '_type') \
71-
and callable(getattr(kv[1]._type, '_of_type', None))
74+
and callable(getattr(kv[1]._type, '_of_type', None))
7275

7376
return reduce(
74-
lambda r, kv: r.update({kv[0]: kv[1]._type._of_type()}) or r if is_filterable(kv) else r,
77+
lambda r, kv: r.update(
78+
{kv[0]: kv[1]._type._of_type()}) or r if is_filterable(kv) else r,
7579
self.fields.items(),
7680
{}
7781
)
@@ -96,17 +100,29 @@ def get_query(cls, model, info, **args):
96100
first = args.pop('first', None)
97101
last = args.pop('last', None)
98102
id = args.pop('id', None)
103+
before = args.pop('before', None)
104+
after = args.pop('after', None)
99105

100106
if id is not None:
101107
# https://github.com/graphql-python/graphene/issues/124
102108
args['pk'] = from_global_id(id)[-1]
103109

104110
objs = objs.filter(**args)
105111

112+
# https://github.com/graphql-python/graphene-mongo/issues/21
113+
if after is not None:
114+
_after = from_global_id(after)[-1]
115+
objs = objs[_after:]
116+
117+
if before is not None:
118+
_before = from_global_id(before)[-1]
119+
objs = objs[:_before]
120+
106121
if first is not None:
107122
objs = objs[:first]
108123
if last is not None:
109-
objs = objs[:-last]
124+
# fix for https://github.com/graphql-python/graphene-mongo/issues/20
125+
objs = objs[-last:]
110126

111127
return objs
112128

0 commit comments

Comments
 (0)