Merge pull request #372 from pizzapanther/master

Added Time Type
This commit is contained in:
Syrus Akbary 2016-11-23 08:18:53 -08:00 committed by GitHub
commit 692d7e76ad
3 changed files with 47 additions and 5 deletions

View File

@ -9,9 +9,10 @@ Graphene defines the following base Scalar Types:
- ``graphene.Boolean`` - ``graphene.Boolean``
- ``graphene.ID`` - ``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.DateTime``
- ``graphene.types.datetime.Time``
- ``graphene.types.json.JSONString`` - ``graphene.types.json.JSONString``

View File

@ -29,11 +29,37 @@ class DateTime(Scalar):
) )
return dt.isoformat() return dt.isoformat()
@staticmethod @classmethod
def parse_literal(node): def parse_literal(cls, node):
if isinstance(node, ast.StringValue): if isinstance(node, ast.StringValue):
return iso8601.parse_date(node.value) return cls.parse_value(node.value)
@staticmethod @staticmethod
def parse_value(value): def parse_value(value):
return iso8601.parse_date(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.epoch_date, value))
return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)

View File

@ -1,18 +1,22 @@
import datetime import datetime
import pytz import pytz
from ..datetime import DateTime from ..datetime import DateTime, Time
from ..objecttype import ObjectType from ..objecttype import ObjectType
from ..schema import Schema from ..schema import Schema
class Query(ObjectType): class Query(ObjectType):
datetime = DateTime(_in=DateTime(name='in')) datetime = DateTime(_in=DateTime(name='in'))
time = Time(_at=Time(name='at'))
def resolve_datetime(self, args, context, info): def resolve_datetime(self, args, context, info):
_in = args.get('in') _in = args.get('in')
return _in return _in
def resolve_time(self, args, context, info):
return args.get('at')
schema = Schema(query=Query) schema = Schema(query=Query)
@ -27,6 +31,17 @@ def test_datetime_query():
} }
def test_time_query():
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
time = datetime.time(now.hour, now.minute, now.second, now.microsecond, now.tzinfo)
isoformat = time.isoformat()
result = schema.execute('''{ time(at: "%s") }'''%isoformat)
assert not result.errors
assert result.data == {
'time': isoformat
}
def test_datetime_query_variable(): def test_datetime_query_variable():
now = datetime.datetime.now().replace(tzinfo=pytz.utc) now = datetime.datetime.now().replace(tzinfo=pytz.utc)
isoformat = now.isoformat() isoformat = now.isoformat()