| 1 |
from math import isnan, nan |
| 2 |
from typing import Any, Dict, Optional |
| 3 |
|
| 4 |
from graphql.language import parse_value, ValueNode |
| 5 |
from graphql.pyutils import Undefined |
| 6 |
from graphql.type import ( |
| 7 |
GraphQLBoolean, |
| 8 |
GraphQLEnumType, |
| 9 |
GraphQLFloat, |
| 10 |
GraphQLID, |
| 11 |
GraphQLInputField, |
| 12 |
GraphQLInputObjectType, |
| 13 |
GraphQLInputType, |
| 14 |
GraphQLInt, |
| 15 |
GraphQLList, |
| 16 |
GraphQLNonNull, |
| 17 |
GraphQLScalarType, |
| 18 |
GraphQLString, |
| 19 |
) |
| 20 |
from graphql.utilities import value_from_ast |
| 21 |
|
| 22 |
|
| 23 |
def describe_value_from_ast(): |
| 24 |
def _value_from( |
| 25 |
value_text: str, |
| 26 |
type_: GraphQLInputType, |
| 27 |
variables: Optional[Dict[str, Any]] = None, |
| 28 |
): |
| 29 |
ast = parse_value(value_text) |
| 30 |
return value_from_ast(ast, type_, variables) |
| 31 |
|
| 32 |
def rejects_empty_input(): |
| 33 |
# noinspection PyTypeChecker |
| 34 |
assert value_from_ast(None, GraphQLBoolean) is Undefined |
| 35 |
|
| 36 |
def converts_according_to_input_coercion_rules(): |
| 37 |
assert _value_from("true", GraphQLBoolean) is True |
| 38 |
assert _value_from("false", GraphQLBoolean) is False |
| 39 |
assert _value_from("123", GraphQLInt) == 123 |
| 40 |
assert _value_from("123", GraphQLFloat) == 123 |
| 41 |
assert _value_from("123.456", GraphQLFloat) == 123.456 |
| 42 |
assert _value_from('"abc123"', GraphQLString) == "abc123" |
| 43 |
assert _value_from("123456", GraphQLID) == "123456" |
| 44 |
assert _value_from('"123456"', GraphQLID) == "123456" |
| 45 |
|
| 46 |
def does_not_convert_when_input_coercion_rules_reject_a_value(): |
| 47 |
assert _value_from("123", GraphQLBoolean) is Undefined |
| 48 |
assert _value_from("123.456", GraphQLInt) is Undefined |
| 49 |
assert _value_from("true", GraphQLInt) is Undefined |
| 50 |
assert _value_from('"123"', GraphQLInt) is Undefined |
| 51 |
assert _value_from('"123"', GraphQLFloat) is Undefined |
| 52 |
assert _value_from("123", GraphQLString) is Undefined |
| 53 |
assert _value_from("true", GraphQLString) is Undefined |
| 54 |
assert _value_from("123.456", GraphQLID) is Undefined |
| 55 |
|
| 56 |
def convert_using_parse_literal_from_a_custom_scalar_type(): |
| 57 |
def pass_through_parse_literal(node, _vars=None): |
| 58 |
assert node.kind == "string_value" |
| 59 |
return node.value |
| 60 |
|
| 61 |
pass_through_scalar = GraphQLScalarType( |
| 62 |
"PassThroughScalar", |
| 63 |
parse_literal=pass_through_parse_literal, |
| 64 |
parse_value=lambda value: value, # pragma: no cover |
| 65 |
) |
| 66 |
|
| 67 |
assert _value_from('"value"', pass_through_scalar) == "value" |
| 68 |
|
| 69 |
def throw_parse_literal(_node: ValueNode, _vars=None): |
| 70 |
raise RuntimeError("Test") |
| 71 |
|
| 72 |
throw_scalar = GraphQLScalarType( |
| 73 |
"ThrowScalar", |
| 74 |
parse_literal=throw_parse_literal, |
| 75 |
parse_value=lambda value: value, # pragma: no cover |
| 76 |
) |
| 77 |
|
| 78 |
assert _value_from("value", throw_scalar) is Undefined |
| 79 |
|
| 80 |
def undefined_parse_literal(_node: ValueNode, _vars=None): |
| 81 |
return Undefined |
| 82 |
|
| 83 |
return_undefined_scalar = GraphQLScalarType( |
| 84 |
"ReturnUndefinedScalar", |
| 85 |
parse_literal=undefined_parse_literal, |
| 86 |
parse_value=lambda value: value, # pragma: no cover |
| 87 |
) |
| 88 |
|
| 89 |
assert _value_from("value", return_undefined_scalar) is Undefined |
| 90 |
|
| 91 |
def converts_enum_values_according_to_input_coercion_rules(): |
| 92 |
test_enum = GraphQLEnumType( |
| 93 |
"TestColor", |
| 94 |
{ |
| 95 |
"RED": 1, |
| 96 |
"GREEN": 2, |
| 97 |
"BLUE": 3, |
| 98 |
"NULL": None, |
| 99 |
"NAN": nan, |
| 100 |
"NO_CUSTOM_VALUE": Undefined, |
| 101 |
}, |
| 102 |
) |
| 103 |
|
| 104 |
assert _value_from("RED", test_enum) == 1 |
| 105 |
assert _value_from("BLUE", test_enum) == 3 |
| 106 |
assert _value_from("YELLOW", test_enum) is Undefined |
| 107 |
assert _value_from("3", test_enum) is Undefined |
| 108 |
assert _value_from('"BLUE"', test_enum) is Undefined |
| 109 |
assert _value_from("null", test_enum) is None |
| 110 |
assert _value_from("NULL", test_enum) is None |
| 111 |
assert _value_from("NULL", GraphQLNonNull(test_enum)) is None |
| 112 |
assert isnan(_value_from("NAN", test_enum)) |
| 113 |
assert _value_from("NO_CUSTOM_VALUE", test_enum) is Undefined |
| 114 |
|
| 115 |
# Boolean! |
| 116 |
non_null_bool = GraphQLNonNull(GraphQLBoolean) |
| 117 |
# [Boolean] |
| 118 |
list_of_bool = GraphQLList(GraphQLBoolean) |
| 119 |
# [Boolean!] |
| 120 |
list_of_non_null_bool = GraphQLList(non_null_bool) |
| 121 |
# [Boolean]! |
| 122 |
non_null_list_of_bool = GraphQLNonNull(list_of_bool) |
| 123 |
# [Boolean!]! |
| 124 |
non_null_list_of_non_mull_bool = GraphQLNonNull(list_of_non_null_bool) |
| 125 |
|
| 126 |
def coerces_to_null_unless_non_null(): |
| 127 |
assert _value_from("null", GraphQLBoolean) is None |
| 128 |
assert _value_from("null", non_null_bool) is Undefined |
| 129 |
|
| 130 |
def coerces_lists_of_values(): |
| 131 |
assert _value_from("true", list_of_bool) == [True] |
| 132 |
assert _value_from("123", list_of_bool) is Undefined |
| 133 |
assert _value_from("null", list_of_bool) is None |
| 134 |
assert _value_from("[true, false]", list_of_bool) == [True, False] |
| 135 |
assert _value_from("[true, 123]", list_of_bool) is Undefined |
| 136 |
assert _value_from("[true, null]", list_of_bool) == [True, None] |
| 137 |
assert _value_from("{ true: true }", list_of_bool) is Undefined |
| 138 |
|
| 139 |
def coerces_non_null_lists_of_values(): |
| 140 |
assert _value_from("true", non_null_list_of_bool) == [True] |
| 141 |
assert _value_from("123", non_null_list_of_bool) is Undefined |
| 142 |
assert _value_from("null", non_null_list_of_bool) is Undefined |
| 143 |
assert _value_from("[true, false]", non_null_list_of_bool) == [True, False] |
| 144 |
assert _value_from("[true, 123]", non_null_list_of_bool) is Undefined |
| 145 |
assert _value_from("[true, null]", non_null_list_of_bool) == [True, None] |
| 146 |
|
| 147 |
def coerces_lists_of_non_null_values(): |
| 148 |
assert _value_from("true", list_of_non_null_bool) == [True] |
| 149 |
assert _value_from("123", list_of_non_null_bool) is Undefined |
| 150 |
assert _value_from("null", list_of_non_null_bool) is None |
| 151 |
assert _value_from("[true, false]", list_of_non_null_bool) == [True, False] |
| 152 |
assert _value_from("[true, 123]", list_of_non_null_bool) is Undefined |
| 153 |
assert _value_from("[true, null]", list_of_non_null_bool) is Undefined |
| 154 |
|
| 155 |
def coerces_non_null_lists_of_non_null_values(): |
| 156 |
assert _value_from("true", non_null_list_of_non_mull_bool) == [True] |
| 157 |
assert _value_from("123", non_null_list_of_non_mull_bool) is Undefined |
| 158 |
assert _value_from("null", non_null_list_of_non_mull_bool) is Undefined |
| 159 |
assert _value_from("[true, false]", non_null_list_of_non_mull_bool) == [ |
| 160 |
True, |
| 161 |
False, |
| 162 |
] |
| 163 |
assert _value_from("[true, 123]", non_null_list_of_non_mull_bool) is Undefined |
| 164 |
assert _value_from("[true, null]", non_null_list_of_non_mull_bool) is Undefined |
| 165 |
|
| 166 |
test_input_obj = GraphQLInputObjectType( |
| 167 |
"TestInput", |
| 168 |
{ |
| 169 |
"int": GraphQLInputField(GraphQLInt, default_value=42), |
| 170 |
"bool": GraphQLInputField(GraphQLBoolean), |
| 171 |
"requiredBool": GraphQLInputField(non_null_bool), |
| 172 |
}, |
| 173 |
) |
| 174 |
|
| 175 |
test_one_of_input_obj = GraphQLInputObjectType( |
| 176 |
"TestOneOfInput", |
| 177 |
{ |
| 178 |
"a": GraphQLInputField(GraphQLString), |
| 179 |
"b": GraphQLInputField(GraphQLString), |
| 180 |
}, |
| 181 |
is_one_of=True, |
| 182 |
) |
| 183 |
|
| 184 |
def coerces_input_objects_according_to_input_coercion_rules(): |
| 185 |
assert _value_from("null", test_input_obj) is None |
| 186 |
assert _value_from("[]", test_input_obj) is Undefined |
| 187 |
assert _value_from("123", test_input_obj) is Undefined |
| 188 |
assert _value_from("{ int: 123, requiredBool: false }", test_input_obj) == { |
| 189 |
"int": 123, |
| 190 |
"requiredBool": False, |
| 191 |
} |
| 192 |
assert _value_from("{ bool: true, requiredBool: false }", test_input_obj) == { |
| 193 |
"int": 42, |
| 194 |
"bool": True, |
| 195 |
"requiredBool": False, |
| 196 |
} |
| 197 |
assert ( |
| 198 |
_value_from("{ int: true, requiredBool: true }", test_input_obj) |
| 199 |
is Undefined |
| 200 |
) |
| 201 |
assert _value_from("{ requiredBool: null }", test_input_obj) is Undefined |
| 202 |
assert _value_from("{ bool: true }", test_input_obj) is Undefined |
| 203 |
assert _value_from('{ a: "abc" }', test_one_of_input_obj) == {"a": "abc"} |
| 204 |
assert _value_from('{ b: "def" }', test_one_of_input_obj) == {"b": "def"} |
| 205 |
assert _value_from('{ a: "abc", b: None }', test_one_of_input_obj) is Undefined |
| 206 |
assert _value_from("{ a: null }", test_one_of_input_obj) is Undefined |
| 207 |
assert _value_from("{ a: 1 }", test_one_of_input_obj) is Undefined |
| 208 |
assert _value_from('{ a: "abc", b: "def" }', test_one_of_input_obj) is Undefined |
| 209 |
assert _value_from("{}", test_one_of_input_obj) is Undefined |
| 210 |
assert _value_from('{ c: "abc" }', test_one_of_input_obj) is Undefined |
| 211 |
|
| 212 |
def accepts_variable_values_assuming_already_coerced(): |
| 213 |
assert _value_from("$var", GraphQLBoolean, {}) is Undefined |
| 214 |
assert _value_from("$var", GraphQLBoolean, {"var": True}) is True |
| 215 |
assert _value_from("$var", GraphQLBoolean, {"var": None}) is None |
| 216 |
assert _value_from("$var", non_null_bool, {"var": None}) is Undefined |
| 217 |
# Python dicts have no prototype chain, so an inherited attribute name |
| 218 |
# is just a missing key, and an explicit Undefined value is missing too. |
| 219 |
assert _value_from("$toString", GraphQLBoolean, {}) is Undefined |
| 220 |
assert _value_from("$var", GraphQLBoolean, {"var": Undefined}) is Undefined |
| 221 |
|
| 222 |
def asserts_variables_are_provided_as_items_in_lists(): |
| 223 |
assert _value_from("[ $foo ]", list_of_bool, {}) == [None] |
| 224 |
assert _value_from("[ $foo ]", list_of_bool, {"foo": Undefined}) == [None] |
| 225 |
assert _value_from("[ $foo ]", list_of_non_null_bool, {}) is Undefined |
| 226 |
assert _value_from("[ $toString ]", list_of_bool, {}) == [None] |
| 227 |
assert _value_from("[ $foo ]", list_of_non_null_bool, {"foo": True}) == [True] |
| 228 |
# Note: variables are expected to have already been coerced, so we |
| 229 |
# do not expect the singleton wrapping behavior for variables. |
| 230 |
assert _value_from("$foo", list_of_non_null_bool, {"foo": True}) is True |
| 231 |
assert _value_from("$foo", list_of_non_null_bool, {"foo": [True]}) == [True] |
| 232 |
|
| 233 |
def omits_input_object_fields_for_unprovided_variables(): |
| 234 |
assert _value_from( |
| 235 |
"{ int: $foo, bool: $foo, requiredBool: true }", test_input_obj, {} |
| 236 |
) == {"int": 42, "requiredBool": True} |
| 237 |
assert _value_from("{ requiredBool: $foo }", test_input_obj, {}) is Undefined |
| 238 |
assert _value_from("{ requiredBool: $foo }", test_input_obj, {"foo": True}) == { |
| 239 |
"int": 42, |
| 240 |
"requiredBool": True, |
| 241 |
} |
| 242 |
assert _value_from( |
| 243 |
"{ int: $toString, requiredBool: true }", test_input_obj, {} |
| 244 |
) == {"int": 42, "requiredBool": True} |
| 245 |
|
| 246 |
def transforms_names_using_out_name(): |
| 247 |
# This is an extension of GraphQL.js. |
| 248 |
complex_input_obj = GraphQLInputObjectType( |
| 249 |
"Complex", |
| 250 |
{ |
| 251 |
"realPart": GraphQLInputField(GraphQLFloat, out_name="real_part"), |
| 252 |
"imagPart": GraphQLInputField( |
| 253 |
GraphQLFloat, default_value=0, out_name="imag_part" |
| 254 |
), |
| 255 |
}, |
| 256 |
) |
| 257 |
assert _value_from("{ realPart: 1 }", complex_input_obj) == { |
| 258 |
"real_part": 1, |
| 259 |
"imag_part": 0, |
| 260 |
} |
| 261 |
|
| 262 |
def transforms_values_with_out_type(): |
| 263 |
# This is an extension of GraphQL.js. |
| 264 |
complex_input_obj = GraphQLInputObjectType( |
| 265 |
"Complex", |
| 266 |
{ |
| 267 |
"real": GraphQLInputField(GraphQLFloat), |
| 268 |
"imag": GraphQLInputField(GraphQLFloat), |
| 269 |
}, |
| 270 |
out_type=lambda value: complex(value["real"], value["imag"]), |
| 271 |
) |
| 272 |
assert _value_from("{ real: 1, imag: 2 }", complex_input_obj) == 1 + 2j |