| 1 |
from math import nan |
| 2 |
from typing import Any, Dict, Optional |
| 3 |
|
| 4 |
from graphql.error import GraphQLError |
| 5 |
from graphql.execution import ExecutionResult, execute_sync |
| 6 |
from graphql.execution.values import get_variable_values |
| 7 |
from graphql.language import OperationDefinitionNode, StringValueNode, ValueNode, parse |
| 8 |
from graphql.pyutils import Undefined |
| 9 |
from graphql.type import ( |
| 10 |
GraphQLArgument, |
| 11 |
GraphQLEnumType, |
| 12 |
GraphQLEnumValue, |
| 13 |
GraphQLField, |
| 14 |
GraphQLFloat, |
| 15 |
GraphQLInputField, |
| 16 |
GraphQLInputObjectType, |
| 17 |
GraphQLList, |
| 18 |
GraphQLNonNull, |
| 19 |
GraphQLObjectType, |
| 20 |
GraphQLScalarType, |
| 21 |
GraphQLSchema, |
| 22 |
GraphQLString, |
| 23 |
) |
| 24 |
|
| 25 |
TestFaultyScalarGraphQLError = GraphQLError( |
| 26 |
"FaultyScalarErrorMessage", extensions={"code": "FaultyScalarExtensionCode"} |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
def faulty_parse_value(value: str) -> str: |
| 31 |
raise TestFaultyScalarGraphQLError |
| 32 |
|
| 33 |
|
| 34 |
def faulty_parse_literal(ast: ValueNode, _variables=None) -> str: |
| 35 |
raise TestFaultyScalarGraphQLError |
| 36 |
|
| 37 |
|
| 38 |
TestFaultyScalar = GraphQLScalarType( |
| 39 |
name="FaultyScalar", |
| 40 |
parse_value=faulty_parse_value, |
| 41 |
parse_literal=faulty_parse_literal, |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
def parse_serialized_value(value: str) -> str: |
| 46 |
assert value == "SerializedValue" |
| 47 |
return "DeserializedValue" |
| 48 |
|
| 49 |
|
| 50 |
def parse_literal_value(ast: ValueNode, _variables=None) -> str: |
| 51 |
assert isinstance(ast, StringValueNode) |
| 52 |
assert ast.value == "SerializedValue" |
| 53 |
return parse_serialized_value(ast.value) |
| 54 |
|
| 55 |
|
| 56 |
TestComplexScalar = GraphQLScalarType( |
| 57 |
name="ComplexScalar", |
| 58 |
parse_value=parse_serialized_value, |
| 59 |
parse_literal=parse_literal_value, |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
TestInputObject = GraphQLInputObjectType( |
| 64 |
"TestInputObject", |
| 65 |
{ |
| 66 |
"a": GraphQLInputField(GraphQLString), |
| 67 |
"b": GraphQLInputField(GraphQLList(GraphQLString)), |
| 68 |
"c": GraphQLInputField(GraphQLNonNull(GraphQLString)), |
| 69 |
"d": GraphQLInputField(TestComplexScalar), |
| 70 |
"e": GraphQLInputField(TestFaultyScalar), |
| 71 |
}, |
| 72 |
) |
| 73 |
|
| 74 |
TestCustomInputObject = GraphQLInputObjectType( |
| 75 |
"TestCustomInputObject", |
| 76 |
{"x": GraphQLInputField(GraphQLFloat), "y": GraphQLInputField(GraphQLFloat)}, |
| 77 |
out_type=lambda value: f"(x|y) = ({value['x']}|{value['y']})", |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
TestNestedInputObject = GraphQLInputObjectType( |
| 82 |
"TestNestedInputObject", |
| 83 |
{ |
| 84 |
"na": GraphQLInputField(GraphQLNonNull(TestInputObject)), |
| 85 |
"nb": GraphQLInputField(GraphQLNonNull(GraphQLString)), |
| 86 |
}, |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
TestEnum = GraphQLEnumType( |
| 91 |
"TestEnum", |
| 92 |
{ |
| 93 |
"NULL": None, |
| 94 |
"UNDEFINED": Undefined, |
| 95 |
"NAN": nan, |
| 96 |
"FALSE": False, |
| 97 |
"CUSTOM": "custom value", |
| 98 |
"DEFAULT_VALUE": GraphQLEnumValue(), |
| 99 |
}, |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
def field_with_input_arg(input_arg: GraphQLArgument): |
| 104 |
return GraphQLField( |
| 105 |
GraphQLString, |
| 106 |
args={"input": input_arg}, |
| 107 |
resolve=lambda _obj, _info, **args: ( |
| 108 |
repr(args["input"]) if "input" in args else None |
| 109 |
), |
| 110 |
) |
| 111 |
|
| 112 |
|
| 113 |
TestType = GraphQLObjectType( |
| 114 |
"TestType", |
| 115 |
{ |
| 116 |
"fieldWithEnumInput": field_with_input_arg(GraphQLArgument(TestEnum)), |
| 117 |
"fieldWithNonNullableEnumInput": field_with_input_arg( |
| 118 |
GraphQLArgument(GraphQLNonNull(TestEnum)) |
| 119 |
), |
| 120 |
"fieldWithObjectInput": field_with_input_arg(GraphQLArgument(TestInputObject)), |
| 121 |
"fieldWithCustomObjectInput": field_with_input_arg( |
| 122 |
GraphQLArgument(TestCustomInputObject) |
| 123 |
), |
| 124 |
"fieldWithNullableStringInput": field_with_input_arg( |
| 125 |
GraphQLArgument(GraphQLString) |
| 126 |
), |
| 127 |
"fieldWithNonNullableStringInput": field_with_input_arg( |
| 128 |
GraphQLArgument(GraphQLNonNull(GraphQLString)) |
| 129 |
), |
| 130 |
"fieldWithDefaultArgumentValue": field_with_input_arg( |
| 131 |
GraphQLArgument(GraphQLString, default_value="Hello World") |
| 132 |
), |
| 133 |
"fieldWithNonNullableStringInputAndDefaultArgValue": field_with_input_arg( |
| 134 |
GraphQLArgument(GraphQLNonNull(GraphQLString), default_value="Hello World") |
| 135 |
), |
| 136 |
"fieldWithNestedInputObject": field_with_input_arg( |
| 137 |
GraphQLArgument(TestNestedInputObject, default_value="Hello World") |
| 138 |
), |
| 139 |
"list": field_with_input_arg(GraphQLArgument(GraphQLList(GraphQLString))), |
| 140 |
"nnList": field_with_input_arg( |
| 141 |
GraphQLArgument(GraphQLNonNull(GraphQLList(GraphQLString))) |
| 142 |
), |
| 143 |
"listNN": field_with_input_arg( |
| 144 |
GraphQLArgument(GraphQLList(GraphQLNonNull(GraphQLString))) |
| 145 |
), |
| 146 |
"nnListNN": field_with_input_arg( |
| 147 |
GraphQLArgument(GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLString)))) |
| 148 |
), |
| 149 |
}, |
| 150 |
) |
| 151 |
|
| 152 |
schema = GraphQLSchema(TestType) |
| 153 |
|
| 154 |
|
| 155 |
def execute_query( |
| 156 |
query: str, variable_values: Optional[Dict[str, Any]] = None |
| 157 |
) -> ExecutionResult: |
| 158 |
document = parse(query) |
| 159 |
return execute_sync(schema, document, variable_values=variable_values) |
| 160 |
|
| 161 |
|
| 162 |
def describe_execute_handles_inputs(): |
| 163 |
def describe_handles_objects_and_nullability(): |
| 164 |
def describe_using_inline_struct(): |
| 165 |
def executes_with_complex_input(): |
| 166 |
result = execute_query(""" |
| 167 |
{ |
| 168 |
fieldWithObjectInput( |
| 169 |
input: {a: "foo", b: ["bar"], c: "baz"}) |
| 170 |
} |
| 171 |
""") |
| 172 |
|
| 173 |
assert result == ( |
| 174 |
{"fieldWithObjectInput": "{'a': 'foo', 'b': ['bar'], 'c': 'baz'}"}, |
| 175 |
None, |
| 176 |
) |
| 177 |
|
| 178 |
def executes_with_custom_input(): |
| 179 |
# This is an extension of GraphQL.js. |
| 180 |
result = execute_query(""" |
| 181 |
{ |
| 182 |
fieldWithCustomObjectInput( |
| 183 |
input: {x: -3.0, y: 4.5}) |
| 184 |
} |
| 185 |
""") |
| 186 |
|
| 187 |
assert result == ( |
| 188 |
{"fieldWithCustomObjectInput": "'(x|y) = (-3.0|4.5)'"}, |
| 189 |
None, |
| 190 |
) |
| 191 |
|
| 192 |
def properly_parses_single_value_to_list(): |
| 193 |
result = execute_query(""" |
| 194 |
{ |
| 195 |
fieldWithObjectInput(input: {a: "foo", b: "bar", c: "baz"}) |
| 196 |
} |
| 197 |
""") |
| 198 |
|
| 199 |
assert result == ( |
| 200 |
{"fieldWithObjectInput": "{'a': 'foo', 'b': ['bar'], 'c': 'baz'}"}, |
| 201 |
None, |
| 202 |
) |
| 203 |
|
| 204 |
def properly_parses_null_value_to_null(): |
| 205 |
result = execute_query(""" |
| 206 |
{ |
| 207 |
fieldWithObjectInput( |
| 208 |
input: {a: null, b: null, c: "C", d: null}) |
| 209 |
} |
| 210 |
""") |
| 211 |
|
| 212 |
assert result == ( |
| 213 |
{ |
| 214 |
"fieldWithObjectInput": "{'a': None, 'b': None," |
| 215 |
" 'c': 'C', 'd': None}" |
| 216 |
}, |
| 217 |
None, |
| 218 |
) |
| 219 |
|
| 220 |
def properly_parses_null_value_in_list(): |
| 221 |
result = execute_query(""" |
| 222 |
{ |
| 223 |
fieldWithObjectInput(input: {b: ["A",null,"C"], c: "C"}) |
| 224 |
} |
| 225 |
""") |
| 226 |
|
| 227 |
assert result == ( |
| 228 |
{"fieldWithObjectInput": "{'b': ['A', None, 'C'], 'c': 'C'}"}, |
| 229 |
None, |
| 230 |
) |
| 231 |
|
| 232 |
def does_not_use_incorrect_value(): |
| 233 |
result = execute_query(""" |
| 234 |
{ |
| 235 |
fieldWithObjectInput(input: ["foo", "bar", "baz"]) |
| 236 |
} |
| 237 |
""") |
| 238 |
|
| 239 |
assert result == ( |
| 240 |
{"fieldWithObjectInput": None}, |
| 241 |
[ |
| 242 |
{ |
| 243 |
"message": "Argument 'input' has invalid value" |
| 244 |
' ["foo", "bar", "baz"].', |
| 245 |
"path": ["fieldWithObjectInput"], |
| 246 |
"locations": [(3, 51)], |
| 247 |
} |
| 248 |
], |
| 249 |
) |
| 250 |
|
| 251 |
def properly_runs_parse_literal_on_complex_scalar_types(): |
| 252 |
result = execute_query(""" |
| 253 |
{ |
| 254 |
fieldWithObjectInput(input: {c: "foo", d: "SerializedValue"}) |
| 255 |
} |
| 256 |
""") |
| 257 |
|
| 258 |
assert result == ( |
| 259 |
{"fieldWithObjectInput": "{'c': 'foo', 'd': 'DeserializedValue'}"}, |
| 260 |
None, |
| 261 |
) |
| 262 |
|
| 263 |
def errors_on_faulty_scalar_type_input(): |
| 264 |
result = execute_query(""" |
| 265 |
{ |
| 266 |
fieldWithObjectInput(input: {c: "foo", e: "bar"}) |
| 267 |
} |
| 268 |
""") |
| 269 |
|
| 270 |
assert result == ( |
| 271 |
{"fieldWithObjectInput": None}, |
| 272 |
[ |
| 273 |
{ |
| 274 |
"message": "Argument 'input' has invalid value" |
| 275 |
' {c: "foo", e: "bar"}.', |
| 276 |
"path": ["fieldWithObjectInput"], |
| 277 |
"locations": [(3, 51)], |
| 278 |
} |
| 279 |
], |
| 280 |
) |
| 281 |
|
| 282 |
def describe_using_variables(): |
| 283 |
doc = """ |
| 284 |
query ($input: TestInputObject) { |
| 285 |
fieldWithObjectInput(input: $input) |
| 286 |
} |
| 287 |
""" |
| 288 |
|
| 289 |
def executes_with_complex_input(): |
| 290 |
params = {"input": {"a": "foo", "b": ["bar"], "c": "baz"}} |
| 291 |
result = execute_query(doc, params) |
| 292 |
|
| 293 |
assert result == ( |
| 294 |
{"fieldWithObjectInput": "{'a': 'foo', 'b': ['bar'], 'c': 'baz'}"}, |
| 295 |
None, |
| 296 |
) |
| 297 |
|
| 298 |
def uses_undefined_when_variable_not_provided(): |
| 299 |
result = execute_query( |
| 300 |
""" |
| 301 |
query q($input: String) { |
| 302 |
fieldWithNullableStringInput(input: $input) |
| 303 |
} |
| 304 |
""", |
| 305 |
{}, |
| 306 |
) # Intentionally missing variable values. |
| 307 |
|
| 308 |
assert result == ({"fieldWithNullableStringInput": None}, None) |
| 309 |
|
| 310 |
def uses_null_when_variable_provided_explicit_null_value(): |
| 311 |
result = execute_query( |
| 312 |
""" |
| 313 |
query q($input: String) { |
| 314 |
fieldWithNullableStringInput(input: $input) |
| 315 |
} |
| 316 |
""", |
| 317 |
{"input": None}, |
| 318 |
) |
| 319 |
|
| 320 |
assert result == ({"fieldWithNullableStringInput": "None"}, None) |
| 321 |
|
| 322 |
def uses_default_value_when_not_provided(): |
| 323 |
result = execute_query(""" |
| 324 |
query ($input: TestInputObject = { |
| 325 |
a: "foo", b: ["bar"], c: "baz"}) { |
| 326 |
fieldWithObjectInput(input: $input) |
| 327 |
} |
| 328 |
""") |
| 329 |
|
| 330 |
assert result == ( |
| 331 |
{"fieldWithObjectInput": "{'a': 'foo', 'b': ['bar'], 'c': 'baz'}"}, |
| 332 |
None, |
| 333 |
) |
| 334 |
|
| 335 |
def does_not_use_default_value_when_provided(): |
| 336 |
result = execute_query( |
| 337 |
""" |
| 338 |
query q($input: String = "Default value") { |
| 339 |
fieldWithNullableStringInput(input: $input) |
| 340 |
} |
| 341 |
""", |
| 342 |
{"input": "Variable value"}, |
| 343 |
) |
| 344 |
|
| 345 |
assert result == ( |
| 346 |
{"fieldWithNullableStringInput": "'Variable value'"}, |
| 347 |
None, |
| 348 |
) |
| 349 |
|
| 350 |
def uses_explicit_null_value_instead_of_default_value(): |
| 351 |
result = execute_query( |
| 352 |
""" |
| 353 |
query q($input: String = "Default value") { |
| 354 |
fieldWithNullableStringInput(input: $input) |
| 355 |
} |
| 356 |
""", |
| 357 |
{"input": None}, |
| 358 |
) |
| 359 |
|
| 360 |
assert result == ({"fieldWithNullableStringInput": "None"}, None) |
| 361 |
|
| 362 |
def uses_null_default_value_when_not_provided(): |
| 363 |
result = execute_query( |
| 364 |
""" |
| 365 |
query q($input: String = null) { |
| 366 |
fieldWithNullableStringInput(input: $input) |
| 367 |
} |
| 368 |
""", |
| 369 |
{}, |
| 370 |
) # Intentionally missing variable values. |
| 371 |
|
| 372 |
assert result == ({"fieldWithNullableStringInput": "None"}, None) |
| 373 |
|
| 374 |
def properly_parses_single_value_to_list(): |
| 375 |
params = {"input": {"a": "foo", "b": "bar", "c": "baz"}} |
| 376 |
result = execute_query(doc, params) |
| 377 |
|
| 378 |
assert result == ( |
| 379 |
{"fieldWithObjectInput": "{'a': 'foo', 'b': ['bar'], 'c': 'baz'}"}, |
| 380 |
None, |
| 381 |
) |
| 382 |
|
| 383 |
def executes_with_complex_scalar_input(): |
| 384 |
params = {"input": {"c": "foo", "d": "SerializedValue"}} |
| 385 |
result = execute_query(doc, params) |
| 386 |
|
| 387 |
assert result == ( |
| 388 |
{"fieldWithObjectInput": "{'c': 'foo', 'd': 'DeserializedValue'}"}, |
| 389 |
None, |
| 390 |
) |
| 391 |
|
| 392 |
def errors_on_faulty_scalar_type_input(): |
| 393 |
params = {"input": {"c": "foo", "e": "SerializedValue"}} |
| 394 |
result = execute_query(doc, params) |
| 395 |
|
| 396 |
assert result == ( |
| 397 |
None, |
| 398 |
[ |
| 399 |
{ |
| 400 |
"message": "Variable '$input' got invalid value" |
| 401 |
" 'SerializedValue' at 'input.e'; FaultyScalarErrorMessage", |
| 402 |
"locations": [(2, 24)], |
| 403 |
"extensions": {"code": "FaultyScalarExtensionCode"}, |
| 404 |
} |
| 405 |
], |
| 406 |
) |
| 407 |
|
| 408 |
def errors_on_null_for_nested_non_null(): |
| 409 |
params = {"input": {"a": "foo", "b": "bar", "c": None}} |
| 410 |
result = execute_query(doc, params) |
| 411 |
|
| 412 |
assert result == ( |
| 413 |
None, |
| 414 |
[ |
| 415 |
{ |
| 416 |
"message": "Variable '$input' got invalid value" |
| 417 |
" None at 'input.c';" |
| 418 |
" Expected non-nullable type 'String!' not to be None.", |
| 419 |
"locations": [(2, 24)], |
| 420 |
} |
| 421 |
], |
| 422 |
) |
| 423 |
|
| 424 |
def errors_on_incorrect_type(): |
| 425 |
result = execute_query(doc, {"input": "foo bar"}) |
| 426 |
|
| 427 |
assert result == ( |
| 428 |
None, |
| 429 |
[ |
| 430 |
{ |
| 431 |
"message": "Variable '$input' got invalid value 'foo bar';" |
| 432 |
" Expected type 'TestInputObject' to be a mapping.", |
| 433 |
"locations": [(2, 24)], |
| 434 |
"path": None, |
| 435 |
} |
| 436 |
], |
| 437 |
) |
| 438 |
|
| 439 |
def errors_on_omission_of_nested_non_null(): |
| 440 |
result = execute_query(doc, {"input": {"a": "foo", "b": "bar"}}) |
| 441 |
|
| 442 |
assert result == ( |
| 443 |
None, |
| 444 |
[ |
| 445 |
{ |
| 446 |
"message": "Variable '$input' got invalid value" |
| 447 |
" {'a': 'foo', 'b': 'bar'};" |
| 448 |
" Field 'c' of required type 'String!' was not provided.", |
| 449 |
"locations": [(2, 24)], |
| 450 |
} |
| 451 |
], |
| 452 |
) |
| 453 |
|
| 454 |
def errors_on_deep_nested_errors_and_with_many_errors(): |
| 455 |
nested_doc = """ |
| 456 |
query ($input: TestNestedInputObject) { |
| 457 |
fieldWithNestedObjectInput(input: $input) |
| 458 |
} |
| 459 |
""" |
| 460 |
result = execute_query(nested_doc, {"input": {"na": {"a": "foo"}}}) |
| 461 |
|
| 462 |
assert result == ( |
| 463 |
None, |
| 464 |
[ |
| 465 |
{ |
| 466 |
"message": "Variable '$input' got invalid value" |
| 467 |
" {'a': 'foo'} at 'input.na';" |
| 468 |
" Field 'c' of required type 'String!' was not provided.", |
| 469 |
"locations": [(2, 28)], |
| 470 |
}, |
| 471 |
{ |
| 472 |
"message": "Variable '$input' got invalid value" |
| 473 |
" {'na': {'a': 'foo'}};" |
| 474 |
" Field 'nb' of required type 'String!' was not provided.", |
| 475 |
"locations": [(2, 28)], |
| 476 |
}, |
| 477 |
], |
| 478 |
) |
| 479 |
|
| 480 |
def errors_on_addition_of_unknown_input_field(): |
| 481 |
params = {"input": {"a": "foo", "b": "bar", "c": "baz", "extra": "dog"}} |
| 482 |
result = execute_query(doc, params) |
| 483 |
|
| 484 |
assert result == ( |
| 485 |
None, |
| 486 |
[ |
| 487 |
{ |
| 488 |
"message": "Variable '$input' got invalid value {'a':" |
| 489 |
" 'foo', 'b': 'bar', 'c': 'baz', 'extra': 'dog'}; Field" |
| 490 |
" 'extra' is not defined by type 'TestInputObject'.", |
| 491 |
"locations": [(2, 24)], |
| 492 |
} |
| 493 |
], |
| 494 |
) |
| 495 |
|
| 496 |
def describe_handles_custom_enum_values(): |
| 497 |
def allows_custom_enum_values_as_inputs(): |
| 498 |
result = execute_query(""" |
| 499 |
{ |
| 500 |
null: fieldWithEnumInput(input: NULL) |
| 501 |
NaN: fieldWithEnumInput(input: NAN) |
| 502 |
false: fieldWithEnumInput(input: FALSE) |
| 503 |
customValue: fieldWithEnumInput(input: CUSTOM) |
| 504 |
defaultValue: fieldWithEnumInput(input: DEFAULT_VALUE) |
| 505 |
} |
| 506 |
""") |
| 507 |
|
| 508 |
assert result == ( |
| 509 |
{ |
| 510 |
"null": "None", |
| 511 |
"NaN": "nan", |
| 512 |
"false": "False", |
| 513 |
"customValue": "'custom value'", |
| 514 |
# different from graphql.js, enum values are always wrapped |
| 515 |
"defaultValue": "None", |
| 516 |
}, |
| 517 |
None, |
| 518 |
) |
| 519 |
|
| 520 |
def allows_non_nullable_inputs_to_have_null_as_enum_custom_value(): |
| 521 |
result = execute_query(""" |
| 522 |
{ |
| 523 |
fieldWithNonNullableEnumInput(input: NULL) |
| 524 |
} |
| 525 |
""") |
| 526 |
|
| 527 |
assert result == ({"fieldWithNonNullableEnumInput": "None"}, None) |
| 528 |
|
| 529 |
def describe_handles_nullable_scalars(): |
| 530 |
def allows_nullable_inputs_to_be_omitted(): |
| 531 |
result = execute_query(""" |
| 532 |
{ |
| 533 |
fieldWithNullableStringInput |
| 534 |
} |
| 535 |
""") |
| 536 |
|
| 537 |
assert result == ({"fieldWithNullableStringInput": None}, None) |
| 538 |
|
| 539 |
def allows_nullable_inputs_to_be_omitted_in_a_variable(): |
| 540 |
result = execute_query(""" |
| 541 |
query ($value: String) { |
| 542 |
fieldWithNullableStringInput(input: $value) |
| 543 |
} |
| 544 |
""") |
| 545 |
|
| 546 |
assert result == ({"fieldWithNullableStringInput": None}, None) |
| 547 |
|
| 548 |
def allows_nullable_inputs_to_be_omitted_in_an_unlisted_variable(): |
| 549 |
result = execute_query(""" |
| 550 |
query SetsNullable { |
| 551 |
fieldWithNullableStringInput(input: $value) |
| 552 |
} |
| 553 |
""") |
| 554 |
|
| 555 |
assert result == ({"fieldWithNullableStringInput": None}, None) |
| 556 |
|
| 557 |
def allows_nullable_inputs_to_be_set_to_null_in_a_variable(): |
| 558 |
doc = """ |
| 559 |
query SetsNullable($value: String) { |
| 560 |
fieldWithNullableStringInput(input: $value) |
| 561 |
} |
| 562 |
""" |
| 563 |
result = execute_query(doc, {"value": None}) |
| 564 |
|
| 565 |
assert result == ({"fieldWithNullableStringInput": "None"}, None) |
| 566 |
|
| 567 |
def allows_nullable_inputs_to_be_set_to_a_value_in_a_variable(): |
| 568 |
doc = """ |
| 569 |
query SetsNullable($value: String) { |
| 570 |
fieldWithNullableStringInput(input: $value) |
| 571 |
} |
| 572 |
""" |
| 573 |
result = execute_query(doc, {"value": "a"}) |
| 574 |
|
| 575 |
assert result == ({"fieldWithNullableStringInput": "'a'"}, None) |
| 576 |
|
| 577 |
def allows_nullable_inputs_to_be_set_to_a_value_directly(): |
| 578 |
result = execute_query(""" |
| 579 |
{ |
| 580 |
fieldWithNullableStringInput(input: "a") |
| 581 |
} |
| 582 |
""") |
| 583 |
|
| 584 |
assert result == ({"fieldWithNullableStringInput": "'a'"}, None) |
| 585 |
|
| 586 |
def describe_handles_non_nullable_scalars(): |
| 587 |
def allows_non_nullable_variable_to_be_omitted_given_a_default(): |
| 588 |
result = execute_query(""" |
| 589 |
query ($value: String! = "default") { |
| 590 |
fieldWithNullableStringInput(input: $value) |
| 591 |
} |
| 592 |
""") |
| 593 |
|
| 594 |
assert result == ({"fieldWithNullableStringInput": "'default'"}, None) |
| 595 |
|
| 596 |
def allows_non_nullable_inputs_to_be_omitted_given_a_default(): |
| 597 |
result = execute_query(""" |
| 598 |
query ($value: String = "default") { |
| 599 |
fieldWithNonNullableStringInput(input: $value) |
| 600 |
} |
| 601 |
""") |
| 602 |
|
| 603 |
assert result == ({"fieldWithNonNullableStringInput": "'default'"}, None) |
| 604 |
|
| 605 |
def does_not_allow_non_nullable_inputs_to_be_omitted_in_a_variable(): |
| 606 |
result = execute_query(""" |
| 607 |
query ($value: String!) { |
| 608 |
fieldWithNonNullableStringInput(input: $value) |
| 609 |
} |
| 610 |
""") |
| 611 |
|
| 612 |
assert result == ( |
| 613 |
None, |
| 614 |
[ |
| 615 |
{ |
| 616 |
"message": "Variable '$value' of required type 'String!'" |
| 617 |
" was not provided.", |
| 618 |
"locations": [(2, 24)], |
| 619 |
"path": None, |
| 620 |
} |
| 621 |
], |
| 622 |
) |
| 623 |
|
| 624 |
def does_not_allow_non_nullable_inputs_to_be_set_to_null_in_variable(): |
| 625 |
doc = """ |
| 626 |
query ($value: String!) { |
| 627 |
fieldWithNonNullableStringInput(input: $value) |
| 628 |
} |
| 629 |
""" |
| 630 |
result = execute_query(doc, {"value": None}) |
| 631 |
|
| 632 |
assert result == ( |
| 633 |
None, |
| 634 |
[ |
| 635 |
{ |
| 636 |
"message": "Variable '$value' of non-null type 'String!'" |
| 637 |
" must not be null.", |
| 638 |
"locations": [(2, 24)], |
| 639 |
"path": None, |
| 640 |
} |
| 641 |
], |
| 642 |
) |
| 643 |
|
| 644 |
def allows_non_nullable_inputs_to_be_set_to_a_value_in_a_variable(): |
| 645 |
doc = """ |
| 646 |
query ($value: String!) { |
| 647 |
fieldWithNonNullableStringInput(input: $value) |
| 648 |
} |
| 649 |
""" |
| 650 |
result = execute_query(doc, {"value": "a"}) |
| 651 |
|
| 652 |
assert result == ({"fieldWithNonNullableStringInput": "'a'"}, None) |
| 653 |
|
| 654 |
def allows_non_nullable_inputs_to_be_set_to_a_value_directly(): |
| 655 |
result = execute_query(""" |
| 656 |
{ |
| 657 |
fieldWithNonNullableStringInput(input: "a") |
| 658 |
} |
| 659 |
""") |
| 660 |
|
| 661 |
assert result == ({"fieldWithNonNullableStringInput": "'a'"}, None) |
| 662 |
|
| 663 |
def reports_error_for_missing_non_nullable_inputs(): |
| 664 |
result = execute_query("{ fieldWithNonNullableStringInput }") |
| 665 |
|
| 666 |
assert result == ( |
| 667 |
{"fieldWithNonNullableStringInput": None}, |
| 668 |
[ |
| 669 |
{ |
| 670 |
"message": "Argument 'input' of required type 'String!'" |
| 671 |
" was not provided.", |
| 672 |
"locations": [(1, 3)], |
| 673 |
"path": ["fieldWithNonNullableStringInput"], |
| 674 |
} |
| 675 |
], |
| 676 |
) |
| 677 |
|
| 678 |
def reports_error_for_array_passed_into_string_input(): |
| 679 |
doc = """ |
| 680 |
query ($value: String!) { |
| 681 |
fieldWithNonNullableStringInput(input: $value) |
| 682 |
} |
| 683 |
""" |
| 684 |
result = execute_query(doc, {"value": [1, 2, 3]}) |
| 685 |
|
| 686 |
assert result == ( |
| 687 |
None, |
| 688 |
[ |
| 689 |
{ |
| 690 |
"message": "Variable '$value' got invalid value [1, 2, 3];" |
| 691 |
" String cannot represent a non string value: [1, 2, 3]", |
| 692 |
"locations": [(2, 24)], |
| 693 |
"path": None, |
| 694 |
} |
| 695 |
], |
| 696 |
) |
| 697 |
|
| 698 |
errors = result.errors |
| 699 |
assert errors |
| 700 |
assert errors[0].original_error |
| 701 |
|
| 702 |
def reports_error_for_non_provided_variables_for_non_nullable_inputs(): |
| 703 |
# Note: this test would typically fail validation before |
| 704 |
# encountering this execution error, however for queries which |
| 705 |
# previously validated and are being run against a new schema which |
| 706 |
# have introduced a breaking change to make a formerly non-required |
| 707 |
# argument required, this asserts failure before allowing the |
| 708 |
# underlying code to receive a non-null value. |
| 709 |
result = execute_query(""" |
| 710 |
{ |
| 711 |
fieldWithNonNullableStringInput(input: $foo) |
| 712 |
} |
| 713 |
""") |
| 714 |
|
| 715 |
assert result == ( |
| 716 |
{"fieldWithNonNullableStringInput": None}, |
| 717 |
[ |
| 718 |
{ |
| 719 |
"message": "Argument 'input' of required type 'String!'" |
| 720 |
" was provided the variable '$foo' which was" |
| 721 |
" not provided a runtime value.", |
| 722 |
"locations": [(3, 58)], |
| 723 |
"path": ["fieldWithNonNullableStringInput"], |
| 724 |
} |
| 725 |
], |
| 726 |
) |
| 727 |
|
| 728 |
def describe_handles_lists_and_nullability(): |
| 729 |
def allows_lists_to_be_null(): |
| 730 |
doc = """ |
| 731 |
query ($input: [String]) { |
| 732 |
list(input: $input) |
| 733 |
} |
| 734 |
""" |
| 735 |
result = execute_query(doc, {"input": None}) |
| 736 |
|
| 737 |
assert result == ({"list": "None"}, None) |
| 738 |
|
| 739 |
def allows_lists_to_contain_values(): |
| 740 |
doc = """ |
| 741 |
query ($input: [String]) { |
| 742 |
list(input: $input) |
| 743 |
} |
| 744 |
""" |
| 745 |
result = execute_query(doc, {"input": ["A"]}) |
| 746 |
|
| 747 |
assert result == ({"list": "['A']"}, None) |
| 748 |
|
| 749 |
def allows_lists_to_contain_null(): |
| 750 |
doc = """ |
| 751 |
query ($input: [String]) { |
| 752 |
list(input: $input) |
| 753 |
} |
| 754 |
""" |
| 755 |
|
| 756 |
result = execute_query(doc, {"input": ["A", None, "B"]}) |
| 757 |
|
| 758 |
assert result == ({"list": "['A', None, 'B']"}, None) |
| 759 |
|
| 760 |
def does_not_allow_non_null_lists_to_be_null(): |
| 761 |
doc = """ |
| 762 |
query ($input: [String]!) { |
| 763 |
nnList(input: $input) |
| 764 |
} |
| 765 |
""" |
| 766 |
|
| 767 |
result = execute_query(doc, {"input": None}) |
| 768 |
|
| 769 |
assert result == ( |
| 770 |
None, |
| 771 |
[ |
| 772 |
{ |
| 773 |
"message": "Variable '$input' of non-null type '[String]!'" |
| 774 |
" must not be null.", |
| 775 |
"locations": [(2, 24)], |
| 776 |
"path": None, |
| 777 |
} |
| 778 |
], |
| 779 |
) |
| 780 |
|
| 781 |
def allows_non_null_lists_to_contain_values(): |
| 782 |
doc = """ |
| 783 |
query ($input: [String]!) { |
| 784 |
nnList(input: $input) |
| 785 |
} |
| 786 |
""" |
| 787 |
|
| 788 |
result = execute_query(doc, {"input": ["A"]}) |
| 789 |
|
| 790 |
assert result == ({"nnList": "['A']"}, None) |
| 791 |
|
| 792 |
def allows_non_null_lists_to_contain_null(): |
| 793 |
doc = """ |
| 794 |
query ($input: [String]!) { |
| 795 |
nnList(input: $input) |
| 796 |
} |
| 797 |
""" |
| 798 |
|
| 799 |
result = execute_query(doc, {"input": ["A", None, "B"]}) |
| 800 |
|
| 801 |
assert result == ({"nnList": "['A', None, 'B']"}, None) |
| 802 |
|
| 803 |
def allows_lists_of_non_nulls_to_be_null(): |
| 804 |
doc = """ |
| 805 |
query ($input: [String!]) { |
| 806 |
listNN(input: $input) |
| 807 |
} |
| 808 |
""" |
| 809 |
|
| 810 |
result = execute_query(doc, {"input": None}) |
| 811 |
|
| 812 |
assert result == ({"listNN": "None"}, None) |
| 813 |
|
| 814 |
def allows_lists_of_non_nulls_to_contain_values(): |
| 815 |
doc = """ |
| 816 |
query ($input: [String!]) { |
| 817 |
listNN(input: $input) |
| 818 |
} |
| 819 |
""" |
| 820 |
|
| 821 |
result = execute_query(doc, {"input": ["A"]}) |
| 822 |
|
| 823 |
assert result == ({"listNN": "['A']"}, None) |
| 824 |
|
| 825 |
def does_not_allow_lists_of_non_nulls_to_contain_null(): |
| 826 |
doc = """ |
| 827 |
query ($input: [String!]) { |
| 828 |
listNN(input: $input) |
| 829 |
} |
| 830 |
""" |
| 831 |
result = execute_query(doc, {"input": ["A", None, "B"]}) |
| 832 |
|
| 833 |
assert result == ( |
| 834 |
None, |
| 835 |
[ |
| 836 |
{ |
| 837 |
"message": "Variable '$input' got invalid value None" |
| 838 |
" at 'input[1]';" |
| 839 |
" Expected non-nullable type 'String!' not to be None.", |
| 840 |
"locations": [(2, 24)], |
| 841 |
} |
| 842 |
], |
| 843 |
) |
| 844 |
|
| 845 |
def does_not_allow_non_null_lists_of_non_nulls_to_be_null(): |
| 846 |
doc = """ |
| 847 |
query ($input: [String!]!) { |
| 848 |
nnListNN(input: $input) |
| 849 |
} |
| 850 |
""" |
| 851 |
result = execute_query(doc, {"input": None}) |
| 852 |
|
| 853 |
assert result == ( |
| 854 |
None, |
| 855 |
[ |
| 856 |
{ |
| 857 |
"message": "Variable '$input' of non-null type '[String!]!'" |
| 858 |
" must not be null.", |
| 859 |
"locations": [(2, 24)], |
| 860 |
} |
| 861 |
], |
| 862 |
) |
| 863 |
|
| 864 |
def allows_non_null_lists_of_non_nulls_to_contain_values(): |
| 865 |
doc = """ |
| 866 |
query ($input: [String!]!) { |
| 867 |
nnListNN(input: $input) |
| 868 |
} |
| 869 |
""" |
| 870 |
result = execute_query(doc, {"input": ["A"]}) |
| 871 |
|
| 872 |
assert result == ({"nnListNN": "['A']"}, None) |
| 873 |
|
| 874 |
def does_not_allow_non_null_lists_of_non_nulls_to_contain_null(): |
| 875 |
doc = """ |
| 876 |
query ($input: [String!]!) { |
| 877 |
nnListNN(input: $input) |
| 878 |
} |
| 879 |
""" |
| 880 |
result = execute_query(doc, {"input": ["A", None, "B"]}) |
| 881 |
|
| 882 |
assert result == ( |
| 883 |
None, |
| 884 |
[ |
| 885 |
{ |
| 886 |
"message": "Variable '$input' got invalid value None" |
| 887 |
" at 'input[1]';" |
| 888 |
" Expected non-nullable type 'String!' not to be None.", |
| 889 |
"locations": [(2, 24)], |
| 890 |
"path": None, |
| 891 |
} |
| 892 |
], |
| 893 |
) |
| 894 |
|
| 895 |
def does_not_allow_invalid_types_to_be_used_as_values(): |
| 896 |
doc = """ |
| 897 |
query ($input: TestType!) { |
| 898 |
fieldWithObjectInput(input: $input) |
| 899 |
} |
| 900 |
""" |
| 901 |
result = execute_query(doc, {"input": {"list": ["A", "B"]}}) |
| 902 |
|
| 903 |
assert result == ( |
| 904 |
None, |
| 905 |
[ |
| 906 |
{ |
| 907 |
"message": "Variable '$input' expected value" |
| 908 |
" of type 'TestType!' which cannot" |
| 909 |
" be used as an input type.", |
| 910 |
"locations": [(2, 32)], |
| 911 |
} |
| 912 |
], |
| 913 |
) |
| 914 |
|
| 915 |
def does_not_allow_unknown_types_to_be_used_as_values(): |
| 916 |
doc = """ |
| 917 |
query ($input: UnknownType!) { |
| 918 |
fieldWithObjectInput(input: $input) |
| 919 |
} |
| 920 |
""" |
| 921 |
result = execute_query(doc, {"input": "whoKnows"}) |
| 922 |
|
| 923 |
assert result == ( |
| 924 |
None, |
| 925 |
[ |
| 926 |
{ |
| 927 |
"message": "Variable '$input' expected value" |
| 928 |
" of type 'UnknownType!' which cannot" |
| 929 |
" be used as an input type.", |
| 930 |
"locations": [(2, 32)], |
| 931 |
} |
| 932 |
], |
| 933 |
) |
| 934 |
|
| 935 |
def describe_execute_uses_argument_default_values(): |
| 936 |
def when_no_argument_provided(): |
| 937 |
result = execute_query("{ fieldWithDefaultArgumentValue }") |
| 938 |
|
| 939 |
assert result == ({"fieldWithDefaultArgumentValue": "'Hello World'"}, None) |
| 940 |
|
| 941 |
def when_omitted_variable_provided(): |
| 942 |
result = execute_query(""" |
| 943 |
query ($optional: String) { |
| 944 |
fieldWithDefaultArgumentValue(input: $optional) |
| 945 |
} |
| 946 |
""") |
| 947 |
|
| 948 |
assert result == ({"fieldWithDefaultArgumentValue": "'Hello World'"}, None) |
| 949 |
|
| 950 |
def not_when_argument_cannot_be_coerced(): |
| 951 |
result = execute_query(""" |
| 952 |
{ |
| 953 |
fieldWithDefaultArgumentValue(input: WRONG_TYPE) |
| 954 |
} |
| 955 |
""") |
| 956 |
|
| 957 |
assert result == ( |
| 958 |
{"fieldWithDefaultArgumentValue": None}, |
| 959 |
[ |
| 960 |
{ |
| 961 |
"message": "Argument 'input' has invalid value WRONG_TYPE.", |
| 962 |
"locations": [(3, 56)], |
| 963 |
"path": ["fieldWithDefaultArgumentValue"], |
| 964 |
} |
| 965 |
], |
| 966 |
) |
| 967 |
|
| 968 |
def when_no_runtime_value_is_provided_to_a_non_null_argument(): |
| 969 |
result = execute_query(""" |
| 970 |
query optionalVariable($optional: String) { |
| 971 |
fieldWithNonNullableStringInputAndDefaultArgValue(input: $optional) |
| 972 |
} |
| 973 |
""") |
| 974 |
|
| 975 |
assert result == ( |
| 976 |
{"fieldWithNonNullableStringInputAndDefaultArgValue": "'Hello World'"}, |
| 977 |
None, |
| 978 |
) |
| 979 |
|
| 980 |
def describe_get_variable_values_limit_maximum_number_of_coercion_errors(): |
| 981 |
doc = parse(""" |
| 982 |
query ($input: [String!]) { |
| 983 |
listNN(input: $input) |
| 984 |
} |
| 985 |
""") |
| 986 |
|
| 987 |
operation = doc.definitions[0] |
| 988 |
assert isinstance(operation, OperationDefinitionNode) |
| 989 |
variable_definitions = operation.variable_definitions |
| 990 |
assert variable_definitions is not None |
| 991 |
|
| 992 |
input_value = {"input": [0, 1, 2]} |
| 993 |
|
| 994 |
def _invalid_value_error(value: int, index: int) -> Dict[str, Any]: |
| 995 |
return { |
| 996 |
"message": "Variable '$input' got invalid value" |
| 997 |
f" {value} at 'input[{index}]';" |
| 998 |
f" String cannot represent a non string value: {value}", |
| 999 |
"locations": [(2, 20)], |
| 1000 |
} |
| 1001 |
|
| 1002 |
def return_all_errors_by_default(): |
| 1003 |
result = get_variable_values(schema, variable_definitions, input_value) |
| 1004 |
|
| 1005 |
assert result == [ |
| 1006 |
_invalid_value_error(0, 0), |
| 1007 |
_invalid_value_error(1, 1), |
| 1008 |
_invalid_value_error(2, 2), |
| 1009 |
] |
| 1010 |
|
| 1011 |
def when_max_errors_is_equal_to_number_of_errors(): |
| 1012 |
result = get_variable_values( |
| 1013 |
schema, variable_definitions, input_value, max_errors=3 |
| 1014 |
) |
| 1015 |
|
| 1016 |
assert result == [ |
| 1017 |
_invalid_value_error(0, 0), |
| 1018 |
_invalid_value_error(1, 1), |
| 1019 |
_invalid_value_error(2, 2), |
| 1020 |
] |
| 1021 |
|
| 1022 |
def when_max_errors_is_less_than_number_of_errors(): |
| 1023 |
result = get_variable_values( |
| 1024 |
schema, variable_definitions, input_value, max_errors=2 |
| 1025 |
) |
| 1026 |
|
| 1027 |
assert result == [ |
| 1028 |
_invalid_value_error(0, 0), |
| 1029 |
_invalid_value_error(1, 1), |
| 1030 |
{ |
| 1031 |
"message": "Too many errors processing variables," |
| 1032 |
" error limit reached. Execution aborted." |
| 1033 |
}, |
| 1034 |
] |