From 2a89227f9127710a5da6b564b54caab299ee0203 Mon Sep 17 00:00:00 2001 From: Erik Wrede Date: Thu, 9 Jun 2022 17:02:55 +0200 Subject: [PATCH] BREAKING: PG UUID & sqlalchemy_utils.UUIDType now convert to graphene.UUID instead of graphene.String Signed-off-by: Erik Wrede --- graphene_sqlalchemy/converter.py | 18 ++++++++++-------- graphene_sqlalchemy/tests/test_converter.py | 8 ++++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/graphene_sqlalchemy/converter.py b/graphene_sqlalchemy/converter.py index 18ddd904..5c880272 100644 --- a/graphene_sqlalchemy/converter.py +++ b/graphene_sqlalchemy/converter.py @@ -9,8 +9,8 @@ from sqlalchemy.dialects import postgresql from sqlalchemy.orm import interfaces, strategies -from graphene import (ID, Boolean, Date, DateTime, Dynamic, Enum, Field, Float, - Int, List, String, Time) +from graphene import (ID, Boolean, Date, Time, DateTime, Dynamic, Enum, Field, Float, + Int, List, String, Time, UUID) from graphene.types.json import JSONString from .batching import get_batch_resolver @@ -30,9 +30,9 @@ try: from sqlalchemy_utils import (ChoiceType, JSONType, ScalarListType, - TSVectorType) + TSVectorType, UUIDType) except ImportError: - ChoiceType = JSONType = ScalarListType = TSVectorType = object + ChoiceType = JSONType = ScalarListType = TSVectorType = UUIDType = object try: from sqlalchemy_utils.types.choice import EnumTypeImpl @@ -199,7 +199,6 @@ def convert_sqlalchemy_type(type, column, registry=None): @convert_sqlalchemy_type.register(types.Text) @convert_sqlalchemy_type.register(types.Unicode) @convert_sqlalchemy_type.register(types.UnicodeText) -@convert_sqlalchemy_type.register(postgresql.UUID) @convert_sqlalchemy_type.register(postgresql.INET) @convert_sqlalchemy_type.register(postgresql.CIDR) @convert_sqlalchemy_type.register(TSVectorType) @@ -207,21 +206,24 @@ def convert_column_to_string(type, column, registry=None): return String +@convert_sqlalchemy_type.register(postgresql.UUID) +@convert_sqlalchemy_type.register(UUIDType) +def convert_column_to_uuid(type, column, registry=None): + return UUID + + @convert_sqlalchemy_type.register(types.DateTime) def convert_column_to_datetime(type, column, registry=None): - from graphene.types.datetime import DateTime return DateTime @convert_sqlalchemy_type.register(types.Time) def convert_column_to_time(type, column, registry=None): - from graphene.types.datetime import Time return Time @convert_sqlalchemy_type.register(types.Date) def convert_column_to_date(type, column, registry=None): - from graphene.types.datetime import Date return Date diff --git a/graphene_sqlalchemy/tests/test_converter.py b/graphene_sqlalchemy/tests/test_converter.py index bb074a96..13551285 100644 --- a/graphene_sqlalchemy/tests/test_converter.py +++ b/graphene_sqlalchemy/tests/test_converter.py @@ -7,7 +7,7 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.inspection import inspect from sqlalchemy.orm import column_property, composite -from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType +from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType, UUIDType import graphene from graphene import Boolean, Float, Int, Scalar, String @@ -300,7 +300,11 @@ class Meta: def test_should_postgresql_uuid_convert(): - assert get_field(postgresql.UUID()).type == graphene.String + assert get_field(postgresql.UUID()).type == graphene.UUID + + +def test_should_sqlalchemy_utils_uuid_convert(): + assert get_field(UUIDType()).type == graphene.UUID def test_should_postgresql_enum_convert():