Debian Codegraph @

tests/execution/test_one_of.py

raw
1 from typing import Any, Dict, Optional
2
3 from graphql.execution import ExecutionResult, execute
4 from graphql.language import parse
5 from graphql.pyutils import AwaitableOrValue
6 from graphql.utilities import build_schema
7
8 schema = build_schema("""
9 type Query {
10 test(input: TestInputObject!): TestObject
11 }
12
13 input TestInputObject @oneOf {
14 a: String
15 b: Int
16 }
17
18 type TestObject {
19 a: String
20 b: Int
21 }
22 """)
23
24
25 def execute_query(
26 query: str, root_value: Any, variable_values: Optional[Dict[str, Any]] = None
27 ) -> AwaitableOrValue[ExecutionResult]:
28 return execute(schema, parse(query), root_value, variable_values=variable_values)
29
30
31 def describe_execute_handles_one_of_input_objects():
32 def describe_one_of_input_objects():
33 root_value = {
34 "test": lambda _info, input: input, # noqa: A002
35 }
36
37 def accepts_a_good_default_value():
38 query = """
39 query ($input: TestInputObject! = {a: "abc"}) {
40 test(input: $input) {
41 a
42 b
43 }
44 }
45 """
46 result = execute_query(query, root_value)
47
48 assert result == ({"test": {"a": "abc", "b": None}}, None)
49
50 def rejects_a_bad_default_value():
51 query = """
52 query ($input: TestInputObject! = {a: "abc", b: 123}) {
53 test(input: $input) {
54 a
55 b
56 }
57 }
58 """
59 result = execute_query(query, root_value)
60
61 assert result == (
62 {"test": None},
63 [
64 {
65 # This type of error would be caught at validation-time
66 # hence the vague error message here.
67 "message": "Argument 'input' of non-null type"
68 " 'TestInputObject!' must not be null.",
69 "locations": [(3, 31)],
70 "path": ["test"],
71 }
72 ],
73 )
74
75 def accepts_a_good_variable():
76 query = """
77 query ($input: TestInputObject!) {
78 test(input: $input) {
79 a
80 b
81 }
82 }
83 """
84 result = execute_query(query, root_value, {"input": {"a": "abc"}})
85
86 assert result == ({"test": {"a": "abc", "b": None}}, None)
87
88 def accepts_a_good_variable_with_an_undefined_key():
89 query = """
90 query ($input: TestInputObject!) {
91 test(input: $input) {
92 a
93 b
94 }
95 }
96 """
97 result = execute_query(query, root_value, {"input": {"a": "abc"}})
98
99 assert result == ({"test": {"a": "abc", "b": None}}, None)
100
101 def rejects_a_variable_with_multiple_non_null_keys():
102 query = """
103 query ($input: TestInputObject!) {
104 test(input: $input) {
105 a
106 b
107 }
108 }
109 """
110 result = execute_query(query, root_value, {"input": {"a": "abc", "b": 123}})
111
112 assert result == (
113 None,
114 [
115 {
116 "message": "Variable '$input' got invalid value"
117 " {'a': 'abc', 'b': 123}; Exactly one key must be specified"
118 " for OneOf type 'TestInputObject'.",
119 "locations": [(2, 24)],
120 }
121 ],
122 )
123
124 def rejects_a_variable_with_multiple_nullable_keys():
125 query = """
126 query ($input: TestInputObject!) {
127 test(input: $input) {
128 a
129 b
130 }
131 }
132 """
133 result = execute_query(
134 query, root_value, {"input": {"a": "abc", "b": None}}
135 )
136
137 assert result == (
138 None,
139 [
140 {
141 "message": "Variable '$input' got invalid value"
142 " {'a': 'abc', 'b': None}; Exactly one key must be specified"
143 " for OneOf type 'TestInputObject'.",
144 "locations": [(2, 24)],
145 }
146 ],
147 )