Skip to content

Commit

Permalink
fix: union type naming for mongo_fields having inner mongo_field
Browse files Browse the repository at this point in the history
eg:
field_1 = ListField(GenericLazyReferenceField(choices=[X1,X2])) generates type ModelNoneUnionType

This fix will make it ModelField1UnionType
  • Loading branch information
mak626 committed Nov 29, 2023
1 parent a5f7afe commit 9b9b1f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions graphene_mongo/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,20 @@ def convert_field_to_union(field, registry=None, executor: ExecutorEnum = Execut
if len(_types) == 0:
return None

name = (
to_camel_case("{}_{}".format(field._owner_document.__name__, field.db_field)) + "UnionType"
if ExecutorEnum.SYNC
else "AsyncUnionType"
field_name = field.db_field
if field_name is None:
# Get db_field name from parent mongo_field
for db_field_name, _mongo_parent_field in field.owner_document._fields.items():
if hasattr(_mongo_parent_field, "field") and _mongo_parent_field.field == field:
field_name = db_field_name
break

name = to_camel_case(
"{}_{}_{}".format(
field._owner_document.__name__,
field_name,
"union_type" if executor == ExecutorEnum.SYNC else "async_union_type",
)
)
Meta = type("Meta", (object,), {"types": tuple(_types)})
_union = type(name, (graphene.Union,), {"Meta": Meta})
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "graphene-mongo"
packages = [{ include = "graphene_mongo" }]
version = "0.4.0"
version = "0.4.1"
description = "Graphene Mongoengine integration"
authors = [
"Abaw Chen <[email protected]>",
Expand Down

0 comments on commit 9b9b1f3

Please sign in to comment.