Skip to content

Commit 6d8e9ca

Browse files
committed
feat: Use uuid as part of union name as a work-around
1 parent 25cb89e commit 6d8e9ca

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

graphene_mongo/converter.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mongoengine
2+
import uuid
23
from graphene import (
34
ID,
45
Boolean,
@@ -23,8 +24,6 @@
2324

2425
singledispatch = import_single_dispatch()
2526

26-
_union_registry = {}
27-
2827

2928
class MongoEngineConversionError(Exception):
3029
pass
@@ -126,18 +125,21 @@ def convert_field_to_union(field, registry=None):
126125
if _type:
127126
_types.append(_type.type)
128127
else:
129-
# Can register type auto-matically here.
128+
# TODO: Register type auto-matically here.
130129
pass
131130

132131
if len(_types) == 0:
133132
return None
134133

135-
print('*' * 50)
136-
print(field.__dict__)
137-
name = field._owner_document.__name__ + '_' + field.db_field + '_union'
134+
# XXX: Use uuid to avoid duplicate name
135+
name = '{}_{}_union_{}'.format(
136+
field._owner_document.__name__,
137+
field.db_field,
138+
str(uuid.uuid1()).replace('-', '')
139+
)
138140
Meta = type('Meta', (object, ), {'types': tuple(_types)})
139-
_union_registry[name] = type(name, (Union, ), {'Meta': Meta})
140-
return Field(_union_registry.get(name))
141+
_union = type(name, (Union, ), {'Meta': Meta})
142+
return Field(_union)
141143

142144

143145
@convert_mongoengine_field.register(mongoengine.EmbeddedDocumentField)

graphene_mongo/tests/nodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import graphene
22
from graphene.relay import Node
33

4-
from . import models, types
4+
from . import models
5+
from . import types # noqa: F401
56
from ..types import MongoengineObjectType
67

78

graphene_mongo/types.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import mongoengine
33

44
from collections import OrderedDict
5-
from graphene.relay import Connection, Node, is_node
5+
from graphene.relay import Connection, Node
66
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
77
from graphene.types.utils import yank_fields_from_attrs
88

@@ -13,6 +13,17 @@
1313

1414

1515
def construct_fields(model, registry, only_fields, exclude_fields):
16+
"""
17+
Args:
18+
model (mongoengine.Document):
19+
registry (graphene_mongo.registry.Registry):
20+
only_fields ([str]):
21+
exclude_fields ([str]):
22+
23+
Returns:
24+
(OrderedDict, OrderedDict): coverted fields and self reference fields.
25+
26+
"""
1627
_model_fields = get_model_fields(model)
1728
fields = OrderedDict()
1829
self_referenced = OrderedDict()

0 commit comments

Comments
 (0)