Skip to content

Add support for UUIDs in @hybrid_property #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import sys
import typing
import uuid
import warnings
from decimal import Decimal
from functools import singledispatch
Expand Down Expand Up @@ -398,6 +399,11 @@ def convert_sqlalchemy_hybrid_property_type_time(arg):
return graphene.Time


@convert_sqlalchemy_hybrid_property_type.register(value_equals(uuid.UUID))
def convert_sqlalchemy_hybrid_property_type_uuid(arg):
return graphene.UUID


def is_union(arg) -> bool:
if sys.version_info >= (3, 10):
from types import UnionType
Expand Down
16 changes: 16 additions & 0 deletions graphene_sqlalchemy/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import enum
import uuid
from decimal import Decimal
from typing import List, Optional, Tuple

Expand Down Expand Up @@ -267,6 +268,21 @@ def hybrid_prop_self_referential_list(self) -> List["ShoppingCart"]:
def hybrid_prop_optional_self_referential(self) -> Optional["ShoppingCart"]:
return None

# UUIDS
@hybrid_property
def hybrid_prop_uuid(self) -> uuid.UUID:
return uuid.uuid4()

@hybrid_property
def hybrid_prop_uuid_list(self) -> List[uuid.UUID]:
return [
uuid.uuid4(),
]

@hybrid_property
def hybrid_prop_optional_uuid(self) -> Optional[uuid.UUID]:
return None


class KeyedModel(Base):
__tablename__ = "test330"
Expand Down
4 changes: 4 additions & 0 deletions graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,10 @@ class Meta:
"hybrid_prop_self_referential_list": graphene.List(ShoppingCartType),
# Optionals
"hybrid_prop_optional_self_referential": ShoppingCartType,
# UUIDs
"hybrid_prop_uuid": graphene.UUID,
"hybrid_prop_optional_uuid": graphene.UUID,
"hybrid_prop_uuid_list": graphene.List(graphene.UUID),
}

assert sorted(list(ShoppingCartType._meta.fields.keys())) == sorted(
Expand Down