mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-09 08:00:39 +03:00
Added Date type in datetime
This commit is contained in:
parent
43cda1d46a
commit
045d5fffbe
|
@ -15,6 +15,32 @@ except ImportError:
|
|||
)
|
||||
|
||||
|
||||
class Date(Scalar):
|
||||
'''
|
||||
The `Date` scalar type represents a Date
|
||||
value as specified by
|
||||
[iso8601](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
'''
|
||||
|
||||
@staticmethod
|
||||
def serialize(date):
|
||||
if isinstance(date, datetime.datetime):
|
||||
date = date.date()
|
||||
assert isinstance(date, datetime.date), (
|
||||
'Received not compatible date "{}"'.format(repr(date))
|
||||
)
|
||||
return date.isoformat()
|
||||
|
||||
@classmethod
|
||||
def parse_literal(cls, node):
|
||||
if isinstance(node, ast.StringValue):
|
||||
return cls.parse_value(node.value)
|
||||
|
||||
@staticmethod
|
||||
def parse_value(value):
|
||||
return iso8601.parse_date(value).date()
|
||||
|
||||
|
||||
class DateTime(Scalar):
|
||||
'''
|
||||
The `DateTime` scalar type represents a DateTime
|
||||
|
|
|
@ -2,18 +2,22 @@ import datetime
|
|||
|
||||
import pytz
|
||||
|
||||
from ..datetime import DateTime, Time
|
||||
from ..datetime import DateTime, Date, Time
|
||||
from ..objecttype import ObjectType
|
||||
from ..schema import Schema
|
||||
|
||||
|
||||
class Query(ObjectType):
|
||||
datetime = DateTime(_in=DateTime(name='in'))
|
||||
date = Date(_in=Date(name='in'))
|
||||
time = Time(_at=Time(name='at'))
|
||||
|
||||
def resolve_datetime(self, info, _in=None):
|
||||
return _in
|
||||
|
||||
def resolve_date(self, info, _in=None):
|
||||
return _in
|
||||
|
||||
def resolve_time(self, info, _at=None):
|
||||
return _at
|
||||
|
||||
|
@ -30,6 +34,15 @@ def test_datetime_query():
|
|||
assert result.data == {'datetime': isoformat}
|
||||
|
||||
|
||||
def test_datetime_query():
|
||||
now = datetime.datetime.now().replace(tzinfo=pytz.utc).date()
|
||||
isoformat = now.isoformat()
|
||||
|
||||
result = schema.execute('''{ date(in: "%s") }''' % isoformat)
|
||||
assert not result.errors
|
||||
assert result.data == {'date': isoformat}
|
||||
|
||||
|
||||
def test_time_query():
|
||||
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
|
||||
time = datetime.time(now.hour, now.minute, now.second, now.microsecond,
|
||||
|
@ -52,6 +65,17 @@ def test_datetime_query_variable():
|
|||
assert result.data == {'datetime': isoformat}
|
||||
|
||||
|
||||
def test_date_query_variable():
|
||||
now = datetime.datetime.now().replace(tzinfo=pytz.utc).date()
|
||||
isoformat = now.isoformat()
|
||||
|
||||
result = schema.execute(
|
||||
'''query Test($date: Date){ date(in: $date) }''',
|
||||
variable_values={'date': isoformat})
|
||||
assert not result.errors
|
||||
assert result.data == {'date': isoformat}
|
||||
|
||||
|
||||
def test_time_query_variable():
|
||||
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
|
||||
time = datetime.time(now.hour, now.minute, now.second, now.microsecond,
|
||||
|
|
Loading…
Reference in New Issue
Block a user