From 75c2d1ce62d52e06f2eb588b51c1668d2906b217 Mon Sep 17 00:00:00 2001 From: AbawChen Date: Fri, 10 May 2019 16:40:42 +0800 Subject: [PATCH 1/8] style: Minor changes --- graphene_mongo/tests/test_relay_query.py | 66 +++++++++++++----------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/graphene_mongo/tests/test_relay_query.py b/graphene_mongo/tests/test_relay_query.py index cf6a8168..21da4c69 100644 --- a/graphene_mongo/tests/test_relay_query.py +++ b/graphene_mongo/tests/test_relay_query.py @@ -6,29 +6,23 @@ from graphene.relay import Node +from . import models +from . import nodes from .setup import fixtures, fixtures_dirname -from .models import Article, Reporter -from .nodes import (ArticleNode, - EditorNode, - PlayerNode, - ReporterNode, - ChildNode, - ParentWithRelationshipNode, - ProfessorVectorNode,) from ..fields import MongoengineConnectionField -def get_nodes(data, key): +def _get_nodes(data, key): return map(lambda edge: edge['node'], data[key]['edges']) def test_should_query_reporter(fixtures): class Query(graphene.ObjectType): - reporter = graphene.Field(ReporterNode) + reporter = graphene.Field(nodes.ReporterNode) def resolve_reporter(self, *args, **kwargs): - return Reporter.objects.first() + return models.Reporter.objects.first() query = ''' query ReporterQuery { @@ -131,7 +125,7 @@ def resolve_reporter(self, *args, **kwargs): def test_should_query_reporters_with_nested_document(fixtures): class Query(graphene.ObjectType): - reporters = MongoengineConnectionField(ReporterNode) + reporters = MongoengineConnectionField(nodes.ReporterNode) query = ''' query ReporterQuery { @@ -185,7 +179,7 @@ class Query(graphene.ObjectType): def test_should_query_all_editors(fixtures, fixtures_dirname): class Query(graphene.ObjectType): - editors = MongoengineConnectionField(EditorNode) + editors = MongoengineConnectionField(nodes.EditorNode) query = ''' query EditorQuery { @@ -259,11 +253,23 @@ class Query(graphene.ObjectType): assert result.data['editors'] == expected['editors'] +""" +def test_should_query_editors_with_dataloader(fixtures): + from promise import Promise + from promise.dataloader import DataLoader + + class EditorLoader(DataLoader): + queryset = models.Editor. + def batch_load_fn(self, keys): + return Promise.resolve([get_user(id=key) for key in keys]) +""" + + def test_should_filter_editors_by_id(fixtures): class Query(graphene.ObjectType): node = Node.Field() - all_editors = MongoengineConnectionField(EditorNode) + all_editors = MongoengineConnectionField(nodes.EditorNode) query = ''' query EditorQuery { @@ -302,7 +308,7 @@ def test_should_filter(fixtures): class Query(graphene.ObjectType): node = Node.Field() - articles = MongoengineConnectionField(ArticleNode) + articles = MongoengineConnectionField(nodes.ArticleNode) query = ''' query ArticlesQuery { @@ -344,7 +350,7 @@ def test_should_filter_by_reference_field(fixtures): class Query(graphene.ObjectType): node = Node.Field() - articles = MongoengineConnectionField(ArticleNode) + articles = MongoengineConnectionField(nodes.ArticleNode) query = ''' query ArticlesQuery { @@ -384,7 +390,7 @@ def test_should_filter_through_inheritance(fixtures): class Query(graphene.ObjectType): node = Node.Field() - children = MongoengineConnectionField(ChildNode) + children = MongoengineConnectionField(nodes.ChildNode) query = ''' query ChildrenQuery { @@ -427,7 +433,7 @@ class Query(graphene.ObjectType): def test_should_filter_by_list_contains(fixtures): # Notes: https://goo.gl/hMNRgs class Query(graphene.ObjectType): - reporters = MongoengineConnectionField(ReporterNode) + reporters = MongoengineConnectionField(nodes.ReporterNode) query = ''' query ReportersQuery { @@ -464,7 +470,7 @@ class Query(graphene.ObjectType): def test_should_filter_by_id(fixtures): # Notes: https://goo.gl/hMNRgs class Query(graphene.ObjectType): - reporter = Node.Field(ReporterNode) + reporter = Node.Field(nodes.ReporterNode) query = ''' query ReporterQuery { @@ -492,7 +498,7 @@ def test_should_first_n(fixtures): class Query(graphene.ObjectType): - editors = MongoengineConnectionField(EditorNode) + editors = MongoengineConnectionField(nodes.EditorNode) query = ''' query EditorQuery { @@ -540,14 +546,14 @@ class Query(graphene.ObjectType): 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 all(item in _get_nodes(result.data, 'editors') + for item in _get_nodes(expected, 'editors')) def test_should_after(fixtures): class Query(graphene.ObjectType): - players = MongoengineConnectionField(PlayerNode) + players = MongoengineConnectionField(nodes.PlayerNode) query = ''' query EditorQuery { @@ -595,7 +601,7 @@ class Query(graphene.ObjectType): def test_should_before(fixtures): class Query(graphene.ObjectType): - players = MongoengineConnectionField(PlayerNode) + players = MongoengineConnectionField(nodes.PlayerNode) query = ''' query EditorQuery { @@ -636,7 +642,7 @@ class Query(graphene.ObjectType): def test_should_last_n(fixtures): class Query(graphene.ObjectType): - players = MongoengineConnectionField(PlayerNode) + players = MongoengineConnectionField(nodes.PlayerNode) query = ''' query PlayerQuery { @@ -679,7 +685,7 @@ def test_should_self_reference(fixtures): class Query(graphene.ObjectType): - all_players = MongoengineConnectionField(PlayerNode) + all_players = MongoengineConnectionField(nodes.PlayerNode) query = ''' query PlayersQuery { @@ -790,7 +796,7 @@ def test_should_lazy_reference(fixtures): class Query(graphene.ObjectType): node = Node.Field() - parents = MongoengineConnectionField(ParentWithRelationshipNode) + parents = MongoengineConnectionField(nodes.ParentWithRelationshipNode) schema = graphene.Schema(query=Query) @@ -855,7 +861,7 @@ def test_should_query_with_embedded_document(fixtures): class Query(graphene.ObjectType): - all_professors = MongoengineConnectionField(ProfessorVectorNode) + all_professors = MongoengineConnectionField(nodes.ProfessorVectorNode) query = ''' query { @@ -896,7 +902,7 @@ def test_should_get_queryset_returns_dict_filters(fixtures): class Query(graphene.ObjectType): node = Node.Field() - articles = MongoengineConnectionField(ArticleNode, get_queryset=lambda *_, **__: {"headline": "World"}) + articles = MongoengineConnectionField(nodes.ArticleNode, get_queryset=lambda *_, **__: {"headline": "World"}) query = ''' query ArticlesQuery { @@ -941,7 +947,7 @@ def get_queryset(model, info, **args): class Query(graphene.ObjectType): node = Node.Field() - articles = MongoengineConnectionField(ArticleNode, get_queryset=get_queryset) + articles = MongoengineConnectionField(nodes.ArticleNode, get_queryset=get_queryset) query = ''' query ArticlesQuery { From f579bc12b2dcb0abd582d0c7143082e76b75a8ab Mon Sep 17 00:00:00 2001 From: AbawChen Date: Fri, 10 May 2019 17:03:05 +0800 Subject: [PATCH 2/8] style: Minor changes --- graphene_mongo/tests/test_query.py | 43 +++++++-------- graphene_mongo/tests/test_relay_query.py | 66 ++++++++++++++++++------ 2 files changed, 71 insertions(+), 38 deletions(-) diff --git a/graphene_mongo/tests/test_query.py b/graphene_mongo/tests/test_query.py index a48fcbf7..c89b501c 100644 --- a/graphene_mongo/tests/test_query.py +++ b/graphene_mongo/tests/test_query.py @@ -3,27 +3,23 @@ import json import graphene +from . import models +from . import types from .setup import fixtures, fixtures_dirname -from .models import ( - Child, Editor, Player, Reporter, ProfessorVector, Parent, CellTower -) -from .types import ( - ChildType, EditorType, PlayerType, ReporterType, ProfessorVectorType, ParentType, CellTowerType -) def test_should_query_editor(fixtures, fixtures_dirname): class Query(graphene.ObjectType): - editor = graphene.Field(EditorType) - editors = graphene.List(EditorType) + editor = graphene.Field(types.EditorType) + editors = graphene.List(types.EditorType) def resolve_editor(self, *args, **kwargs): - return Editor.objects.first() + return models.Editor.objects.first() def resolve_editors(self, *args, **kwargs): - return list(Editor.objects.all()) + return list(models.Editor.objects.all()) query = ''' query EditorQuery { @@ -91,10 +87,10 @@ def resolve_editors(self, *args, **kwargs): def test_should_query_reporter(fixtures): class Query(graphene.ObjectType): - reporter = graphene.Field(ReporterType) + reporter = graphene.Field(types.ReporterType) def resolve_reporter(self, *args, **kwargs): - return Reporter.objects.first() + return models.Reporter.objects.first() query = ''' query ReporterQuery { @@ -154,10 +150,10 @@ def test_should_custom_kwargs(fixtures): class Query(graphene.ObjectType): - editors = graphene.List(EditorType, first=graphene.Int()) + editors = graphene.List(types.EditorType, first=graphene.Int()) def resolve_editors(self, *args, **kwargs): - editors = Editor.objects() + editors = models.Editor.objects() if 'first' in kwargs: editors = editors[:kwargs['first']] return list(editors) @@ -192,10 +188,10 @@ def test_should_self_reference(fixtures): class Query(graphene.ObjectType): - all_players = graphene.List(PlayerType) + all_players = graphene.List(types.PlayerType) def resolve_all_players(self, *args, **kwargs): - return Player.objects.all() + return models.Player.objects.all() query = ''' query PlayersQuery { @@ -260,10 +256,10 @@ def resolve_all_players(self, *args, **kwargs): def test_should_query_with_embedded_document(fixtures): class Query(graphene.ObjectType): - professor_vector = graphene.Field(ProfessorVectorType, id=graphene.String()) + professor_vector = graphene.Field(types.ProfessorVectorType, id=graphene.String()) def resolve_professor_vector(self, info, id): - return ProfessorVector.objects(metadata__id=id).first() + return models.ProfessorVector.objects(metadata__id=id).first() query = """ query { @@ -284,7 +280,8 @@ def resolve_professor_vector(self, info, id): } } } - schema = graphene.Schema(query=Query, types=[ProfessorVectorType]) + schema = graphene.Schema( + query=Query, types=[types.ProfessorVectorType]) result = schema.execute(query) assert not result.errors assert result.data == expected @@ -294,10 +291,10 @@ def test_should_query_child(fixtures): class Query(graphene.ObjectType): - children = graphene.List(ChildType) + children = graphene.List(types.ChildType) def resolve_children(self, *args, **kwargs): - return list(Child.objects.all()) + return list(models.Child.objects.all()) query = ''' query Query { @@ -338,10 +335,10 @@ def test_should_query_cell_tower(fixtures): class Query(graphene.ObjectType): - cell_towers = graphene.List(CellTowerType) + cell_towers = graphene.List(types.CellTowerType) def resolve_cell_towers(self, *args, **kwargs): - return list(CellTower.objects.all()) + return list(models.CellTower.objects.all()) query = ''' query Query { diff --git a/graphene_mongo/tests/test_relay_query.py b/graphene_mongo/tests/test_relay_query.py index 21da4c69..ed87327b 100644 --- a/graphene_mongo/tests/test_relay_query.py +++ b/graphene_mongo/tests/test_relay_query.py @@ -22,6 +22,7 @@ class Query(graphene.ObjectType): reporter = graphene.Field(nodes.ReporterNode) def resolve_reporter(self, *args, **kwargs): + print('abc' * 10) return models.Reporter.objects.first() query = ''' @@ -183,20 +184,20 @@ class Query(graphene.ObjectType): query = ''' query EditorQuery { - editors { - edges { - node { - id, - firstName, - lastName, - avatar { - contentType, - length, - data + editors { + edges { + node { + id, + firstName, + lastName, + avatar { + contentType, + length, + data + } } } } - } } ''' @@ -253,16 +254,51 @@ class Query(graphene.ObjectType): assert result.data['editors'] == expected['editors'] -""" + def test_should_query_editors_with_dataloader(fixtures): from promise import Promise from promise.dataloader import DataLoader class EditorLoader(DataLoader): - queryset = models.Editor. + def batch_load_fn(self, keys): - return Promise.resolve([get_user(id=key) for key in keys]) -""" + print(keys) + queryset = models.Editor.objects(_id__in=keys) + return Promise.resolve( + [ + [e for e in queryset if e._id == _id] + for _id in keys + ] + ) + + editor_loader = EditorLoader() + + class Query(graphene.ObjectType): + editors = MongoengineConnectionField(nodes.EditorNode) + + """ + def resolve_editors(self, info, **args): + print(self.__dict__) + return None + """ + + query = ''' + query EditorPromiseQuery { + editors(first: 1) { + edges { + node { + id, + firstName, + lastName + } + } + } + } + ''' + + schema = graphene.Schema(query=Query) + result = schema.execute(query) + print(result.data) def test_should_filter_editors_by_id(fixtures): From bf7bb7d157014072adad59f20887782cac776d28 Mon Sep 17 00:00:00 2001 From: AbawChen Date: Fri, 10 May 2019 17:07:28 +0800 Subject: [PATCH 3/8] minor: Simplify test code --- graphene_mongo/tests/test_relay_query.py | 27 +++++++++++------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/graphene_mongo/tests/test_relay_query.py b/graphene_mongo/tests/test_relay_query.py index ed87327b..d605f282 100644 --- a/graphene_mongo/tests/test_relay_query.py +++ b/graphene_mongo/tests/test_relay_query.py @@ -22,7 +22,6 @@ class Query(graphene.ObjectType): reporter = graphene.Field(nodes.ReporterNode) def resolve_reporter(self, *args, **kwargs): - print('abc' * 10) return models.Reporter.objects.first() query = ''' @@ -120,7 +119,7 @@ def resolve_reporter(self, *args, **kwargs): schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors - assert result.data['reporter'] == expected['reporter'] + assert result.data == expected def test_should_query_reporters_with_nested_document(fixtures): @@ -174,7 +173,7 @@ class Query(graphene.ObjectType): schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors - assert result.data['reporters'] == expected['reporters'] + assert result.data == expected def test_should_query_all_editors(fixtures, fixtures_dirname): @@ -251,10 +250,10 @@ class Query(graphene.ObjectType): schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors - assert result.data['editors'] == expected['editors'] - + assert result.data == expected +""" def test_should_query_editors_with_dataloader(fixtures): from promise import Promise from promise.dataloader import DataLoader @@ -274,13 +273,13 @@ def batch_load_fn(self, keys): editor_loader = EditorLoader() class Query(graphene.ObjectType): - editors = MongoengineConnectionField(nodes.EditorNode) + # editors = MongoengineConnectionField(nodes.EditorNode) + editors = graphene.List(types.EditorType) - """ def resolve_editors(self, info, **args): print(self.__dict__) return None - """ + query = ''' query EditorPromiseQuery { @@ -299,17 +298,17 @@ def resolve_editors(self, info, **args): schema = graphene.Schema(query=Query) result = schema.execute(query) print(result.data) +""" def test_should_filter_editors_by_id(fixtures): class Query(graphene.ObjectType): - node = Node.Field() - all_editors = MongoengineConnectionField(nodes.EditorNode) + editors = MongoengineConnectionField(nodes.EditorNode) query = ''' query EditorQuery { - allEditors(id: "RWRpdG9yTm9kZToy") { + editors(id: "RWRpdG9yTm9kZToy") { edges { node { id, @@ -321,7 +320,7 @@ class Query(graphene.ObjectType): } ''' expected = { - 'allEditors': { + 'editors': { 'edges': [ { 'node': { @@ -337,13 +336,12 @@ class Query(graphene.ObjectType): schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors - assert dict(result.data['allEditors']) == expected['allEditors'] + assert result.data == expected def test_should_filter(fixtures): class Query(graphene.ObjectType): - node = Node.Field() articles = MongoengineConnectionField(nodes.ArticleNode) query = ''' @@ -385,7 +383,6 @@ class Query(graphene.ObjectType): def test_should_filter_by_reference_field(fixtures): class Query(graphene.ObjectType): - node = Node.Field() articles = MongoengineConnectionField(nodes.ArticleNode) query = ''' From d01e144349bbe49f216557925f310ac12b001159 Mon Sep 17 00:00:00 2001 From: AbawChen Date: Fri, 10 May 2019 17:09:28 +0800 Subject: [PATCH 4/8] minor: Simplify test code --- graphene_mongo/tests/test_relay_query.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/graphene_mongo/tests/test_relay_query.py b/graphene_mongo/tests/test_relay_query.py index d605f282..9e7a92e3 100644 --- a/graphene_mongo/tests/test_relay_query.py +++ b/graphene_mongo/tests/test_relay_query.py @@ -718,11 +718,11 @@ def test_should_self_reference(fixtures): class Query(graphene.ObjectType): - all_players = MongoengineConnectionField(nodes.PlayerNode) + players = MongoengineConnectionField(nodes.PlayerNode) query = ''' query PlayersQuery { - allPlayers { + players { edges { node { firstName, @@ -746,7 +746,7 @@ class Query(graphene.ObjectType): } ''' expected = { - 'allPlayers': { + 'players': { 'edges': [ { 'node': { @@ -894,11 +894,11 @@ def test_should_query_with_embedded_document(fixtures): class Query(graphene.ObjectType): - all_professors = MongoengineConnectionField(nodes.ProfessorVectorNode) + professors = MongoengineConnectionField(nodes.ProfessorVectorNode) query = ''' query { - allProfessors { + professors { edges { node { vec, @@ -911,7 +911,7 @@ class Query(graphene.ObjectType): } ''' expected = { - 'allProfessors': { + 'professors': { 'edges': [ { 'node': { @@ -928,7 +928,7 @@ class Query(graphene.ObjectType): schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors - assert dict(result.data['allProfessors']) == expected['allProfessors'] + assert result.data == expected def test_should_get_queryset_returns_dict_filters(fixtures): From d14a7321d9414bf21addb2335c04ab0c5b563928 Mon Sep 17 00:00:00 2001 From: AbawChen Date: Fri, 10 May 2019 17:17:49 +0800 Subject: [PATCH 5/8] test: Remove _get_nodes --- graphene_mongo/tests/test_relay_query.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/graphene_mongo/tests/test_relay_query.py b/graphene_mongo/tests/test_relay_query.py index 9e7a92e3..f0803045 100644 --- a/graphene_mongo/tests/test_relay_query.py +++ b/graphene_mongo/tests/test_relay_query.py @@ -12,10 +12,6 @@ from ..fields import MongoengineConnectionField -def _get_nodes(data, key): - return map(lambda edge: edge['node'], data[key]['edges']) - - def test_should_query_reporter(fixtures): class Query(graphene.ObjectType): @@ -555,13 +551,13 @@ class Query(graphene.ObjectType): 'editors': { 'edges': [ { - 'cursor': 'xxx', + 'cursor': 'YXJyYXljb25uZWN0aW9uOjA=', 'node': { 'firstName': 'Penny' } }, { - 'cursor': 'xxx', + 'cursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'node': { 'firstName': 'Grant' } @@ -570,8 +566,8 @@ class Query(graphene.ObjectType): 'pageInfo': { 'hasNextPage': True, 'hasPreviousPage': False, - 'startCursor': 'xxx', - 'endCursor': 'xxx' + 'startCursor': 'YXJyYXljb25uZWN0aW9uOjA=', + 'endCursor': 'YXJyYXljb25uZWN0aW9uOjE=' } } } @@ -579,8 +575,7 @@ class Query(graphene.ObjectType): 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 result.data == expected def test_should_after(fixtures): From 7460bc21bfb1610ad030da0e8f9d5e03844ffbcb Mon Sep 17 00:00:00 2001 From: AbawChen Date: Fri, 10 May 2019 17:50:39 +0800 Subject: [PATCH 6/8] wip --- graphene_mongo/tests/test_relay_query.py | 31 +++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/graphene_mongo/tests/test_relay_query.py b/graphene_mongo/tests/test_relay_query.py index f0803045..6ddc2c37 100644 --- a/graphene_mongo/tests/test_relay_query.py +++ b/graphene_mongo/tests/test_relay_query.py @@ -8,6 +8,7 @@ from . import models from . import nodes +from . import types from .setup import fixtures, fixtures_dirname from ..fields import MongoengineConnectionField @@ -249,7 +250,7 @@ class Query(graphene.ObjectType): assert result.data == expected -""" + def test_should_query_editors_with_dataloader(fixtures): from promise import Promise from promise.dataloader import DataLoader @@ -272,29 +273,41 @@ class Query(graphene.ObjectType): # editors = MongoengineConnectionField(nodes.EditorNode) editors = graphene.List(types.EditorType) - def resolve_editors(self, info, **args): - print(self.__dict__) + def resolve_editors(self, info, *args, **kwargs): + print('hell') + # print(self.__dict__) + print(self) + print(info) + print(args) + print(kwargs) return None query = ''' query EditorPromiseQuery { editors(first: 1) { + firstName + } + } + ''' + """ + query = ''' + query EditorPromiseQuery { + editors { edges { node { - id, - firstName, - lastName + firstName } } } } ''' - + """ schema = graphene.Schema(query=Query) result = schema.execute(query) - print(result.data) -""" + assert not result.errors + # print(result.errors) + print('ccccc' * 10, result.data) def test_should_filter_editors_by_id(fixtures): From fd4b99963bd3ccd6c9fa1319154f85b226d63e0d Mon Sep 17 00:00:00 2001 From: AbawChen Date: Tue, 14 May 2019 15:20:17 +0800 Subject: [PATCH 7/8] test: Make test_should_query_editors_with_dataloader pass --- graphene_mongo/fields.py | 33 ++++++--- graphene_mongo/tests/test_relay_query.py | 88 ++++++++++++++---------- setup.cfg | 1 - 3 files changed, 74 insertions(+), 48 deletions(-) diff --git a/graphene_mongo/fields.py b/graphene_mongo/fields.py index fcf20015..d10820ba 100644 --- a/graphene_mongo/fields.py +++ b/graphene_mongo/fields.py @@ -5,6 +5,7 @@ import graphene import mongoengine +from promise import Promise from graphene.relay import ConnectionField from graphene.types.argument import to_arguments from graphene.types.dynamic import Dynamic @@ -126,7 +127,6 @@ def fields(self): return self._type._meta.fields def get_queryset(self, model, info, **args): - if args: reference_fields = get_model_reference_fields(self.model) hydrated_references = {} @@ -157,36 +157,49 @@ def default_resolver(self, _root, info, **args): _id = args.pop('id', None) if _id is not None: - objs = [get_node_from_global_id(self.node_type, info, _id)] + iterables = [get_node_from_global_id(self.node_type, info, _id)] list_length = 1 elif callable(getattr(self.model, 'objects', None)): - objs = self.get_queryset(self.model, info, **args) - list_length = objs.count() + iterables = self.get_queryset(self.model, info, **args) + list_length = iterables.count() else: - objs = [] + iterables = [] list_length = 0 connection = connection_from_list_slice( - list_slice=objs, + list_slice=iterables, args=connection_args, list_length=list_length, connection_type=self.type, edge_type=self.type.Edge, pageinfo_type=graphene.PageInfo, ) - connection.iterable = objs + connection.iterable = iterables connection.list_length = list_length return connection - def chained_resolver(self, resolver, root, info, **args): - if not bool(args): + def chained_resolver(self, resolver, is_partial, root, info, **args): + if not bool(args) or not is_partial: # XXX: Filter nested args resolved = resolver(root, info, **args) if resolved is not None: return resolved return self.default_resolver(root, info, **args) + @classmethod + def connection_resolver(cls, resolver, connection_type, root, info, **args): + iterable = resolver(root, info, **args) + if isinstance(connection_type, graphene.NonNull): + connection_type = connection_type.of_type + + on_resolve = partial(cls.resolve_connection, connection_type, args) + if Promise.is_thenable(iterable): + return Promise.resolve(iterable).then(on_resolve) + + return on_resolve(iterable) + def get_resolver(self, parent_resolver): super_resolver = self.resolver or parent_resolver - resolver = partial(self.chained_resolver, super_resolver) + resolver = partial( + self.chained_resolver, super_resolver, isinstance(super_resolver, partial)) return partial(self.connection_resolver, resolver, self.type) diff --git a/graphene_mongo/tests/test_relay_query.py b/graphene_mongo/tests/test_relay_query.py index 6ddc2c37..0f913724 100644 --- a/graphene_mongo/tests/test_relay_query.py +++ b/graphene_mongo/tests/test_relay_query.py @@ -11,6 +11,7 @@ from . import types from .setup import fixtures, fixtures_dirname from ..fields import MongoengineConnectionField +from ..types import MongoengineObjectType def test_should_query_reporter(fixtures): @@ -250,64 +251,78 @@ class Query(graphene.ObjectType): assert result.data == expected - def test_should_query_editors_with_dataloader(fixtures): from promise import Promise from promise.dataloader import DataLoader - class EditorLoader(DataLoader): + class ArticleLoader(DataLoader): - def batch_load_fn(self, keys): - print(keys) - queryset = models.Editor.objects(_id__in=keys) - return Promise.resolve( - [ - [e for e in queryset if e._id == _id] - for _id in keys - ] - ) + def batch_load_fn(self, instances): + queryset = models.Article.objects(editor__in=instances) + return Promise.resolve([ + [a for a in queryset if a.editor.id == instance.id] + for instance in instances + ]) - editor_loader = EditorLoader() + article_loader = ArticleLoader() - class Query(graphene.ObjectType): - # editors = MongoengineConnectionField(nodes.EditorNode) - editors = graphene.List(types.EditorType) + class _EditorNode(MongoengineObjectType): + + class Meta: + model = models.Editor + interfaces = (graphene.Node,) - def resolve_editors(self, info, *args, **kwargs): - print('hell') - # print(self.__dict__) - print(self) - print(info) - print(args) - print(kwargs) - return None + articles = MongoengineConnectionField(nodes.ArticleNode) + + def resolve_articles(self, *args, **kwargs): + return article_loader.load(self) + class Query(graphene.ObjectType): + editors = MongoengineConnectionField(_EditorNode) query = ''' - query EditorPromiseQuery { + query EditorsConnectionPromiseQuery { editors(first: 1) { - firstName - } - } - ''' - """ - query = ''' - query EditorPromiseQuery { - editors { edges { node { - firstName + firstName, + articles(first: 1) { + edges { + node { + headline + } + } + } } } } } ''' - """ + + expected = { + 'editors': { + 'edges': [ + { + 'node': { + 'firstName': 'Penny', + 'articles': { + 'edges': [ + { + 'node': { + 'headline': 'Hello' + } + } + ] + } + } + } + ] + } + } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors - # print(result.errors) - print('ccccc' * 10, result.data) + assert result.data == expected def test_should_filter_editors_by_id(fixtures): @@ -337,7 +352,6 @@ class Query(graphene.ObjectType): 'firstName': 'Grant', 'lastName': 'Hill' } - } ] } diff --git a/setup.cfg b/setup.cfg index fef3f048..1a477837 100644 --- a/setup.cfg +++ b/setup.cfg @@ -19,5 +19,4 @@ known_first_party=graphene,graphene_mongo test=pytest [tool:pytest] -addopts=-vv python_files = graphene_mongo/tests/*.py From 9a354105a78d7944522eab2a81d0ffcfa542136b Mon Sep 17 00:00:00 2001 From: AbawChen Date: Tue, 14 May 2019 15:21:52 +0800 Subject: [PATCH 8/8] minor: Rename query --- graphene_mongo/tests/test_relay_query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene_mongo/tests/test_relay_query.py b/graphene_mongo/tests/test_relay_query.py index 0f913724..7e26f2fa 100644 --- a/graphene_mongo/tests/test_relay_query.py +++ b/graphene_mongo/tests/test_relay_query.py @@ -281,7 +281,7 @@ class Query(graphene.ObjectType): editors = MongoengineConnectionField(_EditorNode) query = ''' - query EditorsConnectionPromiseQuery { + query EditorPromiseConnectionQuery { editors(first: 1) { edges { node {