Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@query.field("testInputValueType")
def resolve_test_input_type(*_, value):
return value
datereadonly = ScalarType("DateReadOnly")
@datereadonly.serializer
def serialize_date(date):
return date.strftime("%Y-%m-%d")
dateinput = ScalarType("DateInput")
@dateinput.value_parser
def parse_date_str(formatted_date):
parsed_datetime = datetime.strptime(formatted_date, "%Y-%m-%d")
return parsed_datetime.date()
@dateinput.literal_parser
def parse_date_literal(ast):
if not isinstance(ast, StringValueNode):
raise ValueError()
formatted_date = ast.value
parsed_datetime = datetime.strptime(formatted_date, "%Y-%m-%d")
return parsed_datetime.date()
def test_scalar_literal_parser_can_be_set_on_initialization():
schema = build_schema(type_defs)
scalar = ScalarType("DateInput", literal_parser=parse_date_literal)
scalar.bind_to_schema(schema)
schema_scalar = schema.type_map.get("DateInput")
assert schema_scalar.parse_literal is parse_date_literal
def resolve_test_serialize(*_):
return TEST_DATE
@query.field("testInput")
def resolve_test_input(*_, value):
assert value == TEST_DATE
return True
@query.field("testInputValueType")
def resolve_test_input_type(*_, value):
return value
datereadonly = ScalarType("DateReadOnly")
@datereadonly.serializer
def serialize_date(date):
return date.strftime("%Y-%m-%d")
dateinput = ScalarType("DateInput")
@dateinput.value_parser
def parse_date_str(formatted_date):
parsed_datetime = datetime.strptime(formatted_date, "%Y-%m-%d")
return parsed_datetime.date()
def test_scalar_value_parser_can_be_set_on_initialization():
schema = build_schema(type_defs)
scalar = ScalarType("DateInput", value_parser=parse_date_str)
scalar.bind_to_schema(schema)
schema_scalar = schema.type_map.get("DateInput")
assert schema_scalar.parse_value is parse_date_str
def parse_date_str(formatted_date):
parsed_datetime = datetime.strptime(formatted_date, "%Y-%m-%d")
return parsed_datetime.date()
@dateinput.literal_parser
def parse_date_literal(ast):
if not isinstance(ast, StringValueNode):
raise ValueError()
formatted_date = ast.value
parsed_datetime = datetime.strptime(formatted_date, "%Y-%m-%d")
return parsed_datetime.date()
scalar_with_default_parser = ScalarType("ScalarWithDefaultParser")
@scalar_with_default_parser.value_parser
def parse_value_from_default_literal_parser(value):
return type(value).__name__
schema = make_executable_schema(
type_defs, [query, datereadonly, dateinput, scalar_with_default_parser]
)
def test_attempt_bind_scalar_to_undefined_type_raises_error():
schema = build_schema(type_defs)
scalar = ScalarType("Test")
with pytest.raises(ValueError):
def test_scalar_literal_parser_can_be_set_with_setter():
schema = build_schema(type_defs)
scalar = ScalarType("DateInput")
scalar.set_literal_parser(parse_date_literal)
scalar.bind_to_schema(schema)
schema_scalar = schema.type_map.get("DateInput")
assert schema_scalar.parse_literal is parse_date_literal
def test_setting_scalar_value_parser_doesnt_override_already_set_literal_parser():
schema = build_schema(type_defs)
scalar = ScalarType("DateInput")
scalar.set_literal_parser(parse_date_literal)
scalar.set_value_parser(parse_date_str)
scalar.bind_to_schema(schema)
schema_scalar = schema.type_map.get("DateInput")
assert schema_scalar.parse_value is parse_date_str
assert schema_scalar.parse_literal is parse_date_literal
def test_setting_scalar_value_parser_sets_default_literal_parsers_if_none_is_set():
schema = build_schema(type_defs)
scalar = ScalarType("DateInput")
scalar.set_value_parser(parse_date_str)
scalar.bind_to_schema(schema)
schema_scalar = schema.type_map.get("DateInput")
assert schema_scalar.parse_value is parse_date_str
assert schema_scalar.parse_literal
def test_scalar_serializer_can_be_set_with_setter():
schema = build_schema(type_defs)
scalar = ScalarType("DateInput")
scalar.set_serializer(serialize_date)
scalar.bind_to_schema(schema)
schema_scalar = schema.type_map.get("DateInput")
assert schema_scalar.serialize is serialize_date
from datetime import datetime
from ariadne import ScalarType
datetime_scalar = ScalarType("DateTime")
@datetime_scalar.serializer
def serialize_datetime(value: datetime) -> str:
return value.isoformat()[:23] + "Z"