Skip to content

Commit

Permalink
fix: Make pageInfo work well.
Browse files Browse the repository at this point in the history
  • Loading branch information
abawchen committed May 2, 2018
1 parent f7229dc commit c94f9a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
6 changes: 6 additions & 0 deletions examples/flask_mongoengine/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
default_query = '''
{
allEmployees {
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
},
edges {
node {
id,
Expand Down
12 changes: 8 additions & 4 deletions graphene_mongo/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ def fields(self):
def get_query(cls, model, info, **args):

if not callable(getattr(model, 'objects', None)):
return []
return [], 0

objs = model.objects()
length = objs.count()
if args:
reference_fields = get_model_reference_fields(model)
reference_args = {}
Expand Down Expand Up @@ -139,13 +140,15 @@ def get_query(cls, model, info, **args):
_before = int(from_global_id(before)[-1])
objs = objs[:_before]

length = objs.count()

if first is not None:
objs = objs[:first]
if last is not None:
# https://github.com/graphql-python/graphene-mongo/issues/20
objs = objs[-(last+1):]

return objs
return objs, length

# noqa
@classmethod
Expand All @@ -159,8 +162,9 @@ def merge_querysets(cls, default_queryset, queryset):
def connection_resolver(cls, resolver, connection, model, root, info, **args):
iterable = resolver(root, info, **args)
if not iterable:
iterable = cls.get_query(model, info, **args)
_len = len(iterable)
iterable, _len = cls.get_query(model, info, **args)
else:
_len = len(iterable)
connection = connection_from_list_slice(
iterable,
args,
Expand Down
22 changes: 16 additions & 6 deletions graphene_mongo/tests/test_relay_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ class Query(graphene.ObjectType):
query = '''
query EditorQuery {
editors(first: 2) {
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
},
edges {
cursor,
node {
Expand All @@ -418,15 +424,21 @@ class Query(graphene.ObjectType):
'''
expected = {
'editors': {
'pageInfo': {
'hasNextPage': True,
'hasPreviousPage': False,
'startCursor': 'YXJyYXljb25uZWN0aW9uOjA=',
'endCursor': 'YXJyYXljb25uZWN0aW9uOjE='
},
'edges': [
{
'cursor': 'xxx',
'cursor': 'YXJyYXljb25uZWN0aW9uOjA=',
'node': {
'firstName': 'Penny'
}
},
{
'cursor': 'xxx',
'cursor': 'YXJyYXljb25uZWN0aW9uOjE=',
'node': {
'firstName': 'Grant'
}
Expand All @@ -436,11 +448,9 @@ class Query(graphene.ObjectType):
}
schema = graphene.Schema(query=Query)
result = schema.execute(query)

assert not result.errors
assert all(item in get_nodes(result.data, 'editors')
for item in get_nodes(expected, 'editors'))

assert json.dumps(result.data, sort_keys=True) == json.dumps(
expected, sort_keys=True)

def test_should_after():
class Query(graphene.ObjectType):
Expand Down

0 comments on commit c94f9a5

Please sign in to comment.