Skip to content

Commit fd4b999

Browse files
committed
test: Make test_should_query_editors_with_dataloader pass
1 parent 7460bc2 commit fd4b999

File tree

3 files changed

+74
-48
lines changed

3 files changed

+74
-48
lines changed

graphene_mongo/fields.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import graphene
77
import mongoengine
8+
from promise import Promise
89
from graphene.relay import ConnectionField
910
from graphene.types.argument import to_arguments
1011
from graphene.types.dynamic import Dynamic
@@ -126,7 +127,6 @@ def fields(self):
126127
return self._type._meta.fields
127128

128129
def get_queryset(self, model, info, **args):
129-
130130
if args:
131131
reference_fields = get_model_reference_fields(self.model)
132132
hydrated_references = {}
@@ -157,36 +157,49 @@ def default_resolver(self, _root, info, **args):
157157
_id = args.pop('id', None)
158158

159159
if _id is not None:
160-
objs = [get_node_from_global_id(self.node_type, info, _id)]
160+
iterables = [get_node_from_global_id(self.node_type, info, _id)]
161161
list_length = 1
162162
elif callable(getattr(self.model, 'objects', None)):
163-
objs = self.get_queryset(self.model, info, **args)
164-
list_length = objs.count()
163+
iterables = self.get_queryset(self.model, info, **args)
164+
list_length = iterables.count()
165165
else:
166-
objs = []
166+
iterables = []
167167
list_length = 0
168168

169169
connection = connection_from_list_slice(
170-
list_slice=objs,
170+
list_slice=iterables,
171171
args=connection_args,
172172
list_length=list_length,
173173
connection_type=self.type,
174174
edge_type=self.type.Edge,
175175
pageinfo_type=graphene.PageInfo,
176176
)
177-
connection.iterable = objs
177+
connection.iterable = iterables
178178
connection.list_length = list_length
179179
return connection
180180

181-
def chained_resolver(self, resolver, root, info, **args):
182-
if not bool(args):
181+
def chained_resolver(self, resolver, is_partial, root, info, **args):
182+
if not bool(args) or not is_partial:
183183
# XXX: Filter nested args
184184
resolved = resolver(root, info, **args)
185185
if resolved is not None:
186186
return resolved
187187
return self.default_resolver(root, info, **args)
188188

189+
@classmethod
190+
def connection_resolver(cls, resolver, connection_type, root, info, **args):
191+
iterable = resolver(root, info, **args)
192+
if isinstance(connection_type, graphene.NonNull):
193+
connection_type = connection_type.of_type
194+
195+
on_resolve = partial(cls.resolve_connection, connection_type, args)
196+
if Promise.is_thenable(iterable):
197+
return Promise.resolve(iterable).then(on_resolve)
198+
199+
return on_resolve(iterable)
200+
189201
def get_resolver(self, parent_resolver):
190202
super_resolver = self.resolver or parent_resolver
191-
resolver = partial(self.chained_resolver, super_resolver)
203+
resolver = partial(
204+
self.chained_resolver, super_resolver, isinstance(super_resolver, partial))
192205
return partial(self.connection_resolver, resolver, self.type)

graphene_mongo/tests/test_relay_query.py

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from . import types
1212
from .setup import fixtures, fixtures_dirname
1313
from ..fields import MongoengineConnectionField
14+
from ..types import MongoengineObjectType
1415

1516

1617
def test_should_query_reporter(fixtures):
@@ -250,64 +251,78 @@ class Query(graphene.ObjectType):
250251
assert result.data == expected
251252

252253

253-
254254
def test_should_query_editors_with_dataloader(fixtures):
255255
from promise import Promise
256256
from promise.dataloader import DataLoader
257257

258-
class EditorLoader(DataLoader):
258+
class ArticleLoader(DataLoader):
259259

260-
def batch_load_fn(self, keys):
261-
print(keys)
262-
queryset = models.Editor.objects(_id__in=keys)
263-
return Promise.resolve(
264-
[
265-
[e for e in queryset if e._id == _id]
266-
for _id in keys
267-
]
268-
)
260+
def batch_load_fn(self, instances):
261+
queryset = models.Article.objects(editor__in=instances)
262+
return Promise.resolve([
263+
[a for a in queryset if a.editor.id == instance.id]
264+
for instance in instances
265+
])
269266

270-
editor_loader = EditorLoader()
267+
article_loader = ArticleLoader()
271268

272-
class Query(graphene.ObjectType):
273-
# editors = MongoengineConnectionField(nodes.EditorNode)
274-
editors = graphene.List(types.EditorType)
269+
class _EditorNode(MongoengineObjectType):
270+
271+
class Meta:
272+
model = models.Editor
273+
interfaces = (graphene.Node,)
275274

276-
def resolve_editors(self, info, *args, **kwargs):
277-
print('hell')
278-
# print(self.__dict__)
279-
print(self)
280-
print(info)
281-
print(args)
282-
print(kwargs)
283-
return None
275+
articles = MongoengineConnectionField(nodes.ArticleNode)
276+
277+
def resolve_articles(self, *args, **kwargs):
278+
return article_loader.load(self)
284279

280+
class Query(graphene.ObjectType):
281+
editors = MongoengineConnectionField(_EditorNode)
285282

286283
query = '''
287-
query EditorPromiseQuery {
284+
query EditorsConnectionPromiseQuery {
288285
editors(first: 1) {
289-
firstName
290-
}
291-
}
292-
'''
293-
"""
294-
query = '''
295-
query EditorPromiseQuery {
296-
editors {
297286
edges {
298287
node {
299-
firstName
288+
firstName,
289+
articles(first: 1) {
290+
edges {
291+
node {
292+
headline
293+
}
294+
}
295+
}
300296
}
301297
}
302298
}
303299
}
304300
'''
305-
"""
301+
302+
expected = {
303+
'editors': {
304+
'edges': [
305+
{
306+
'node': {
307+
'firstName': 'Penny',
308+
'articles': {
309+
'edges': [
310+
{
311+
'node': {
312+
'headline': 'Hello'
313+
}
314+
}
315+
]
316+
}
317+
}
318+
}
319+
]
320+
}
321+
}
306322
schema = graphene.Schema(query=Query)
307323
result = schema.execute(query)
308324
assert not result.errors
309-
# print(result.errors)
310-
print('ccccc' * 10, result.data)
325+
assert result.data == expected
311326

312327

313328
def test_should_filter_editors_by_id(fixtures):
@@ -337,7 +352,6 @@ class Query(graphene.ObjectType):
337352
'firstName': 'Grant',
338353
'lastName': 'Hill'
339354
}
340-
341355
}
342356
]
343357
}

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ known_first_party=graphene,graphene_mongo
1919
test=pytest
2020

2121
[tool:pytest]
22-
addopts=-vv
2322
python_files = graphene_mongo/tests/*.py

0 commit comments

Comments
 (0)