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