|
12 | 12 | GraphQLInt,
|
13 | 13 | GraphQLList,
|
14 | 14 | GraphQLNonNull,
|
| 15 | + GraphQLScalarType, |
15 | 16 | GraphQLString,
|
16 | 17 | )
|
17 | 18 | from graphql.utilities import value_from_ast
|
@@ -46,6 +47,38 @@ def does_not_convert_when_input_coercion_rules_reject_a_value():
|
46 | 47 | assert _value_from("true", GraphQLString) is Undefined
|
47 | 48 | assert _value_from("123.456", GraphQLID) is Undefined
|
48 | 49 |
|
| 50 | + def convert_using_parse_literal_from_a_custom_scalar_type(): |
| 51 | + def pass_through_parse_literal(node, _vars=None): |
| 52 | + assert node.kind == "string_value" |
| 53 | + return node.value |
| 54 | + |
| 55 | + pass_through_scalar = GraphQLScalarType( |
| 56 | + "PassThroughScalar", |
| 57 | + parse_literal=pass_through_parse_literal, |
| 58 | + parse_value=lambda value: value, |
| 59 | + ) |
| 60 | + |
| 61 | + assert _value_from('"value"', pass_through_scalar) == "value" |
| 62 | + |
| 63 | + def throw_parse_literal(_node, _vars=None): |
| 64 | + raise RuntimeError("Test") |
| 65 | + |
| 66 | + throw_scalar = GraphQLScalarType( |
| 67 | + "ThrowScalar", |
| 68 | + parse_literal=throw_parse_literal, |
| 69 | + parse_value=lambda value: value, |
| 70 | + ) |
| 71 | + |
| 72 | + assert _value_from("value", throw_scalar) is Undefined |
| 73 | + |
| 74 | + return_undefined_scalar = GraphQLScalarType( |
| 75 | + "ReturnUndefinedScalar", |
| 76 | + parse_literal=lambda _node, _vars=None: Undefined, |
| 77 | + parse_value=lambda value: value, |
| 78 | + ) |
| 79 | + |
| 80 | + assert _value_from("value", return_undefined_scalar) is Undefined |
| 81 | + |
49 | 82 | def converts_enum_values_according_to_input_coercion_rules():
|
50 | 83 | test_enum = GraphQLEnumType(
|
51 | 84 | "TestColor",
|
|
0 commit comments