Skip to content

Commit 2409659

Browse files
Merge branch 'support-monogengine-field-enum' into support-graphene-federation
# Conflicts: # graphene_mongo/fields.py
2 parents 7713cee + bd8be20 commit 2409659

File tree

13 files changed

+429
-158
lines changed

13 files changed

+429
-158
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ htmlcov/
1010
*.pyc
1111
*.swo
1212
*.swp
13-
13+
venv/
14+
.vscode/

graphene_mongo/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from .fields import MongoengineConnectionField
22

3-
from .types import MongoengineObjectType
3+
from .types import MongoengineObjectType, MongoengineInputType, MongoengineInterfaceType
44

55
__version__ = "0.1.1"
66

7-
__all__ = ["__version__", "MongoengineObjectType", "MongoengineConnectionField"]
7+
__all__ = [
8+
"__version__",
9+
"MongoengineObjectType",
10+
"MongoengineInputType",
11+
"MongoengineInterfaceType",
12+
"MongoengineConnectionField"
13+
]

graphene_mongo/converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def dynamic_type():
429429
return graphene.Dynamic(dynamic_type)
430430

431431

432-
if sys.version_info[0] >= 3:
432+
if sys.version_info[0] > 3.5:
433433
@convert_mongoengine_field.register(mongoengine.EnumField)
434434
def convert_field_to_enum(field, registry=None):
435435
return graphene.Field(graphene.Enum.from_enum(field._enum_cls),

graphene_mongo/registry.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ def __init__(self):
44
self._registry_string_map = {}
55

66
def register(self, cls):
7-
from .types import MongoengineObjectType
7+
from .types import GrapheneMongoengineObjectTypes
88

99
assert issubclass(
10-
cls, MongoengineObjectType
11-
), 'Only MongoengineObjectTypes can be registered, received "{}"'.format(
10+
cls,
11+
GrapheneMongoengineObjectTypes
12+
), 'Only Mongoengine object types can be registered, received "{}"'.format(
1213
cls.__name__
1314
)
1415
assert cls._meta.registry == self, "Registry for a Model have to match."
@@ -24,6 +25,14 @@ def get_type_for_model(self, model):
2425

2526

2627
registry = None
28+
inputs_registry = None
29+
30+
31+
def get_inputs_registry():
32+
global inputs_registry
33+
if not inputs_registry:
34+
inputs_registry = Registry()
35+
return inputs_registry
2736

2837

2938
def get_global_registry():
@@ -35,4 +44,6 @@ def get_global_registry():
3544

3645
def reset_global_registry():
3746
global registry
47+
global inputs_registry
3848
registry = None
49+
inputs_registry = None

graphene_mongo/tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Player,
1010
Reporter,
1111
Child,
12+
AnotherChild,
1213
ProfessorMetadata,
1314
ProfessorVector,
1415
ChildRegisteredBefore,
@@ -108,6 +109,12 @@ def fixtures():
108109
child2 = Child(bar="bar", baz="baz", loc=[10, 20])
109110
child2.save()
110111

112+
another_child1 = AnotherChild(bar="BAR", qux="QUX")
113+
another_child1.save()
114+
115+
another_child2 = AnotherChild(bar="bar", qux="qux", loc=[20, 10])
116+
another_child2.save()
117+
111118
CellTower.drop_collection()
112119
ct = CellTower(
113120
code="bar",

graphene_mongo/tests/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,19 @@ class CellTower(mongoengine.Document):
115115

116116

117117
class Child(Parent):
118-
meta = {"collection": "test_child"}
118+
119+
meta = {"collection": "test_parent"}
119120
baz = mongoengine.StringField()
120121
loc = mongoengine.PointField()
121122

122123

124+
class AnotherChild(Parent):
125+
126+
meta = {"collection": "test_parent"}
127+
qux = mongoengine.StringField()
128+
loc = mongoengine.PointField()
129+
130+
123131
class ProfessorMetadata(mongoengine.EmbeddedDocument):
124132
meta = {"collection": "test_professor_metadata"}
125133
id = mongoengine.StringField(primary_key=False)

graphene_mongo/tests/test_inputs.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import graphene
2+
3+
from graphene.relay import Node
4+
5+
from .models import Article, Editor
6+
from .nodes import ArticleNode, EditorNode
7+
from .types import ArticleInput, EditorInput
8+
9+
10+
def test_should_create(fixtures):
11+
class CreateArticle(graphene.Mutation):
12+
class Arguments:
13+
article = ArticleInput(required=True)
14+
15+
article = graphene.Field(ArticleNode)
16+
17+
def mutate(self, info, article):
18+
article = Article(**article)
19+
article.save()
20+
21+
return CreateArticle(article=article)
22+
23+
class Query(graphene.ObjectType):
24+
25+
node = Node.Field()
26+
27+
class Mutation(graphene.ObjectType):
28+
29+
create_article = CreateArticle.Field()
30+
31+
query = """
32+
mutation ArticleCreator {
33+
createArticle(
34+
article: {headline: "My Article"}
35+
) {
36+
article {
37+
headline
38+
}
39+
}
40+
}
41+
"""
42+
expected = {"createArticle": {"article": {"headline": "My Article"}}}
43+
schema = graphene.Schema(query=Query, mutation=Mutation)
44+
result = schema.execute(query)
45+
assert not result.errors
46+
assert result.data == expected
47+
48+
49+
def test_should_update(fixtures):
50+
class UpdateEditor(graphene.Mutation):
51+
class Arguments:
52+
id = graphene.ID(required=True)
53+
editor = EditorInput(required=True)
54+
55+
editor = graphene.Field(EditorNode)
56+
57+
def mutate(self, info, id, editor):
58+
editor_to_update = Editor.objects.get(id=id)
59+
for key, value in editor.items():
60+
setattr(editor_to_update, key, value)
61+
editor_to_update.save()
62+
return UpdateEditor(editor=editor_to_update)
63+
64+
class Query(graphene.ObjectType):
65+
66+
node = Node.Field()
67+
68+
class Mutation(graphene.ObjectType):
69+
70+
update_editor = UpdateEditor.Field()
71+
72+
query = """
73+
mutation EditorUpdater {
74+
updateEditor(
75+
id: "1"
76+
editor: {
77+
lastName: "Lane"
78+
}
79+
) {
80+
editor {
81+
firstName
82+
lastName
83+
}
84+
}
85+
}
86+
"""
87+
expected = {"updateEditor": {"editor": {"firstName": "Penny", "lastName": "Lane"}}}
88+
schema = graphene.Schema(query=Query, mutation=Mutation)
89+
result = schema.execute(query)
90+
# print(result.data)
91+
assert not result.errors
92+
assert result.data == expected

graphene_mongo/tests/test_query.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,96 @@ def resolve_children(self, *args, **kwargs):
262262
assert result.data == expected
263263

264264

265+
def test_should_query_other_childs(fixtures):
266+
class Query(graphene.ObjectType):
267+
268+
children = graphene.List(types.AnotherChildType)
269+
270+
def resolve_children(self, *args, **kwargs):
271+
return list(models.AnotherChild.objects.all())
272+
273+
query = """
274+
query Query {
275+
children {
276+
bar,
277+
qux,
278+
loc {
279+
type,
280+
coordinates
281+
}
282+
}
283+
}
284+
"""
285+
expected = {
286+
"children": [
287+
{"bar": "BAR", "qux": "QUX", "loc": None},
288+
{
289+
"bar": "bar",
290+
"qux": "qux",
291+
"loc": {"type": "Point", "coordinates": [20, 10]},
292+
},
293+
]
294+
}
295+
296+
schema = graphene.Schema(query=Query)
297+
result = schema.execute(query)
298+
assert not result.errors
299+
assert result.data == expected
300+
301+
302+
def test_should_query_all_childs(fixtures):
303+
class Query(graphene.ObjectType):
304+
children = graphene.List(types.ChildUnionType)
305+
306+
def resolve_children(self, *args, **kwargs):
307+
return list(models.Parent.objects.all())
308+
309+
query = """
310+
query Query {
311+
children {
312+
... on ParentInterface {
313+
bar
314+
}
315+
... on ChildType{
316+
baz
317+
loc {
318+
type,
319+
coordinates
320+
}
321+
}
322+
... on AnotherChildType {
323+
qux
324+
loc {
325+
type,
326+
coordinates
327+
}
328+
}
329+
}
330+
}
331+
"""
332+
expected = {
333+
"children": [
334+
{"bar": "BAR", "baz": "BAZ", "loc": None},
335+
{
336+
"bar": "bar",
337+
"baz": "baz",
338+
"loc": {"type": "Point", "coordinates": [10.0, 20.0]},
339+
},
340+
{"bar": "BAR", "qux": "QUX", "loc": None},
341+
{
342+
"bar": "bar",
343+
"qux": "qux",
344+
"loc": {"type": "Point", "coordinates": [20, 10]},
345+
},
346+
]
347+
}
348+
349+
schema = graphene.Schema(query=Query)
350+
result = schema.execute(query)
351+
assert not result.errors
352+
assert result.data == expected
353+
354+
265355
def test_should_query_cell_tower(fixtures):
266356
class Query(graphene.ObjectType):
267357

graphene_mongo/tests/test_relay_query.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ class Query(graphene.ObjectType):
455455
}
456456
}
457457
schema = graphene.Schema(query=Query)
458-
print(schema)
459458
result = schema.execute(query)
460459
assert not result.errors
461460
assert result.data == expected

graphene_mongo/tests/test_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class A(TypeSubclassWithBadOptions):
157157
class Meta:
158158
model = Article
159159

160-
assert "instance of MongoengineObjectTypeOptions" in str(einfo.value)
160+
assert "instance of MongoengineGenericObjectTypeOptions" in str(einfo.value)
161161

162162
class TypeSubclass(MongoengineObjectType):
163163
class Meta:

0 commit comments

Comments
 (0)