From 84fbf5dc23351c26a852271aa6529a60bce13735 Mon Sep 17 00:00:00 2001 From: Anis Jonischkeit Date: Tue, 27 Feb 2018 10:00:20 +1000 Subject: [PATCH 1/4] Made DateTime types return GraphQLError on fail This change makes it so that when an incorrectly formatted date string gets passed to a Date / Time argument a GraphQLError is returned rather than a GraphQLLocatedError. Since Date / Time are types, their errors should not be in the same class as errors in your application. This is also inline with how other types work in graphene (graphene.Int, graphene.Float) --- graphene/types/datetime.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/graphene/types/datetime.py b/graphene/types/datetime.py index 3b0e3f13..b3e14682 100644 --- a/graphene/types/datetime.py +++ b/graphene/types/datetime.py @@ -38,8 +38,10 @@ class Date(Scalar): @staticmethod def parse_value(value): - return iso8601.parse_date(value).date() - + try: + return iso8601.parse_date(value).date() + except iso8601.ParseError: + return None class DateTime(Scalar): ''' @@ -62,8 +64,10 @@ class DateTime(Scalar): @staticmethod def parse_value(value): - return iso8601.parse_date(value) - + try: + return iso8601.parse_date(value) + except iso8601.ParseError: + return None class Time(Scalar): ''' @@ -87,5 +91,8 @@ class Time(Scalar): @classmethod def parse_value(cls, value): - dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value)) - return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo) + try: + dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value)) + return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo) + except iso8601.ParseError: + return None From 1a1efbd77d3958110747a3cbe9638cacab33d148 Mon Sep 17 00:00:00 2001 From: Anis Jonischkeit Date: Tue, 27 Feb 2018 10:21:41 +1000 Subject: [PATCH 2/4] linting: added two lines after end of class --- graphene/types/datetime.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/graphene/types/datetime.py b/graphene/types/datetime.py index b3e14682..c3c78fb2 100644 --- a/graphene/types/datetime.py +++ b/graphene/types/datetime.py @@ -43,6 +43,7 @@ class Date(Scalar): except iso8601.ParseError: return None + class DateTime(Scalar): ''' The `DateTime` scalar type represents a DateTime @@ -69,6 +70,7 @@ class DateTime(Scalar): except iso8601.ParseError: return None + class Time(Scalar): ''' The `Time` scalar type represents a Time value as From 2a67ffeb35493968fa3aa1177a088a538208e420 Mon Sep 17 00:00:00 2001 From: Anis Jonischkeit Date: Wed, 14 Mar 2018 12:51:34 +1000 Subject: [PATCH 3/4] fixed function name for test to be what it is actually testing and prevent name colision --- graphene/types/tests/test_datetime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene/types/tests/test_datetime.py b/graphene/types/tests/test_datetime.py index bfe491a3..b5ffba7a 100644 --- a/graphene/types/tests/test_datetime.py +++ b/graphene/types/tests/test_datetime.py @@ -34,7 +34,7 @@ def test_datetime_query(): assert result.data == {'datetime': isoformat} -def test_datetime_query(): +def test_date_query(): now = datetime.datetime.now().replace(tzinfo=pytz.utc).date() isoformat = now.isoformat() From 9973fd314f6134a96a78617fee1caf21d592b6af Mon Sep 17 00:00:00 2001 From: Anis Jonischkeit Date: Wed, 14 Mar 2018 13:06:10 +1000 Subject: [PATCH 4/4] added tests for when bad input is passed into date/datetime/time fields --- graphene/types/tests/test_datetime.py | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/graphene/types/tests/test_datetime.py b/graphene/types/tests/test_datetime.py index b5ffba7a..b516e497 100644 --- a/graphene/types/tests/test_datetime.py +++ b/graphene/types/tests/test_datetime.py @@ -2,6 +2,8 @@ import datetime import pytz +from graphql import GraphQLError + from ..datetime import DateTime, Date, Time from ..objecttype import ObjectType from ..schema import Schema @@ -53,6 +55,32 @@ def test_time_query(): assert not result.errors assert result.data == {'time': isoformat} +def test_bad_datetime_query(): + not_a_date = "Some string that's not a date" + + result = schema.execute('''{ datetime(in: "%s") }''' % not_a_date) + + assert len(result.errors) == 1 + assert isinstance(result.errors[0], GraphQLError) + assert result.data == None + +def test_bad_date_query(): + not_a_date = "Some string that's not a date" + + result = schema.execute('''{ date(in: "%s") }''' % not_a_date) + + assert len(result.errors) == 1 + assert isinstance(result.errors[0], GraphQLError) + assert result.data == None + +def test_bad_time_query(): + not_a_date = "Some string that's not a date" + + result = schema.execute('''{ time(at: "%s") }''' % not_a_date) + + assert len(result.errors) == 1 + assert isinstance(result.errors[0], GraphQLError) + assert result.data == None def test_datetime_query_variable(): now = datetime.datetime.now().replace(tzinfo=pytz.utc)