Skip to content

Commit

Permalink
feat: Add support for UUIDs in @hybrid_property-ies (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
flipbit03 committed Oct 1, 2022
1 parent 0a765a1 commit 75abf0b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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

0 comments on commit 75abf0b

Please sign in to comment.