Fix DateTime Scalar parse_literal methods (#1199) (#1200)

This commit is contained in:
Christoph Zwerschke 2020-06-05 06:30:23 +02:00 committed by GitHub
parent 966aba06cd
commit 47c63f3dd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 3 deletions

View File

@ -25,7 +25,7 @@ class Date(Scalar):
return date.isoformat()
@classmethod
def parse_literal(cls, node):
def parse_literal(cls, node, _variables=None):
if not isinstance(node, StringValueNode):
raise GraphQLError(
f"Date cannot represent non-string value: {print_ast(node)}"
@ -58,7 +58,7 @@ class DateTime(Scalar):
return dt.isoformat()
@classmethod
def parse_literal(cls, node):
def parse_literal(cls, node, _variables=None):
if not isinstance(node, StringValueNode):
raise GraphQLError(
f"DateTime cannot represent non-string value: {print_ast(node)}"
@ -93,7 +93,7 @@ class Time(Scalar):
return time.isoformat()
@classmethod
def parse_literal(cls, node):
def parse_literal(cls, node, _variables=None):
if not isinstance(node, StringValueNode):
raise GraphQLError(
f"Time cannot represent non-string value: {print_ast(node)}"

View File

@ -60,6 +60,23 @@ def test_datetime_query(sample_datetime):
assert result.data == {"datetime": isoformat}
def test_datetime_query_with_variables(sample_datetime):
isoformat = sample_datetime.isoformat()
result = schema.execute(
"""
query GetDate($datetime: DateTime) {
literal: datetime(in: "%s")
value: datetime(in: $datetime)
}
"""
% isoformat,
variable_values={"datetime": isoformat},
)
assert not result.errors
assert result.data == {"literal": isoformat, "value": isoformat}
def test_date_query(sample_date):
isoformat = sample_date.isoformat()
@ -68,6 +85,23 @@ def test_date_query(sample_date):
assert result.data == {"date": isoformat}
def test_date_query_with_variables(sample_date):
isoformat = sample_date.isoformat()
result = schema.execute(
"""
query GetDate($date: Date) {
literal: date(in: "%s")
value: date(in: $date)
}
"""
% isoformat,
variable_values={"date": isoformat},
)
assert not result.errors
assert result.data == {"literal": isoformat, "value": isoformat}
def test_time_query(sample_time):
isoformat = sample_time.isoformat()
@ -76,6 +110,23 @@ def test_time_query(sample_time):
assert result.data == {"time": isoformat}
def test_time_query_with_variables(sample_time):
isoformat = sample_time.isoformat()
result = schema.execute(
"""
query GetTime($time: Time) {
literal: time(at: "%s")
value: time(at: $time)
}
"""
% isoformat,
variable_values={"time": isoformat},
)
assert not result.errors
assert result.data == {"literal": isoformat, "value": isoformat}
def test_bad_datetime_query():
not_a_date = "Some string that's not a datetime"