diff --git a/graphene_pydantic/converters.py b/graphene_pydantic/converters.py index b12eab6..821423c 100644 --- a/graphene_pydantic/converters.py +++ b/graphene_pydantic/converters.py @@ -236,6 +236,12 @@ def find_graphene_type( return Enum.from_enum(type_) elif issubclass(type_, str): return String + elif issubclass(type_, datetime.datetime): + return DateTime + elif issubclass(type_, datetime.date): + return Date + elif issubclass(type_, datetime.time): + return Time else: raise ConversionError( f"Don't know how to convert the Pydantic field {field!r} ({field.type_})" diff --git a/pyproject.toml b/pyproject.toml index 0d7fe16..29f6567 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "graphene_pydantic" -version = "0.3.0" +version = "0.4.0" description = "Graphene Pydantic integration" readme = "README.md" repository = "https://github.com/graphql-python/graphene-pydantic" diff --git a/tests/test_converters.py b/tests/test_converters.py index 1b0f1d9..5d555ac 100644 --- a/tests/test_converters.py +++ b/tests/test_converters.py @@ -48,6 +48,15 @@ def test_default_values(): assert field.type == graphene.String assert field.default_value == "hi" +class DatetimeSubclass(datetime.datetime): + pass + +class TimeSubclass(datetime.time): + pass + +class DateSubclass(datetime.date): + pass + @pytest.mark.parametrize( "input, expected", @@ -60,6 +69,10 @@ def test_default_values(): ((datetime.date, datetime.date(2019, 1, 1)), graphene.Date), ((datetime.time, datetime.time(15, 29)), graphene.Time), ((datetime.datetime, datetime.datetime(2019, 1, 1, 1, 37)), graphene.DateTime), + # Tests support for datetime mocking libraries like Freezegun + ((DatetimeSubclass, DatetimeSubclass(2019, 1, 1, 1, 37)), graphene.DateTime), + ((DateSubclass, DateSubclass(2019, 1, 1)), graphene.Date), + ((TimeSubclass, TimeSubclass(15, 29)), graphene.Time), ], ) def test_builtin_scalars(input, expected):