Merge pull request #679 from anisjonischkeit/patch-1

Made DateTime types return GraphQLError on fail
This commit is contained in:
Syrus Akbary 2018-03-20 20:26:46 -07:00 committed by GitHub
commit 415b71f730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 5 deletions

View File

@ -38,7 +38,10 @@ class Date(Scalar):
@staticmethod
def parse_value(value):
try:
return iso8601.parse_date(value).date()
except iso8601.ParseError:
return None
class DateTime(Scalar):
@ -62,7 +65,10 @@ class DateTime(Scalar):
@staticmethod
def parse_value(value):
try:
return iso8601.parse_date(value)
except iso8601.ParseError:
return None
class Time(Scalar):
@ -87,5 +93,8 @@ class Time(Scalar):
@classmethod
def parse_value(cls, value):
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

View File

@ -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
@ -34,7 +36,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()
@ -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)