added time type

This commit is contained in:
Paul Bailey 2016-11-22 18:04:22 -05:00
parent 5cfa895881
commit dddb20a0f4
2 changed files with 31 additions and 4 deletions

View File

@ -9,9 +9,10 @@ Graphene defines the following base Scalar Types:
- ``graphene.Boolean``
- ``graphene.ID``
Graphene also provides custom scalars for Dates and JSON:
Graphene also provides custom scalars for Dates, Times, and JSON:
- ``graphene.types.datetime.DateTime``
- ``graphene.types.datetime.Time``
- ``graphene.types.json.JSONString``

View File

@ -29,11 +29,37 @@ class DateTime(Scalar):
)
return dt.isoformat()
@staticmethod
def parse_literal(node):
@classmethod
def parse_literal(cls, node):
if isinstance(node, ast.StringValue):
return iso8601.parse_date(node.value)
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
return iso8601.parse_date(value)
class Time(Scalar):
'''
The `Time` scalar type represents a Time value as
specified by
[iso8601](https://en.wikipedia.org/wiki/ISO_8601).
'''
epoch_date = '1970-01-01'
@staticmethod
def serialize(time):
assert isinstance(time, datetime.time), (
'Received not compatible time "{}"'.format(repr(time))
)
return time.isoformat()
@classmethod
def parse_literal(cls, node):
if isinstance(node, ast.StringValue):
return cls.parse_value(node.value)
@classmethod
def parse_value(cls, value):
dt = iso8601.parse_date('{}T{}'.format(cls.epocj_time, value))
return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)