| 1 |
from typing import cast, Any, Awaitable |
| 2 |
|
| 3 |
from pytest import mark |
| 4 |
|
| 5 |
from graphql.execution import execute, execute_sync, ExecutionResult |
| 6 |
from graphql.language import parse |
| 7 |
from graphql.pyutils import is_awaitable |
| 8 |
from graphql.utilities import build_schema |
| 9 |
|
| 10 |
|
| 11 |
class Data: |
| 12 |
def __init__(self, value): |
| 13 |
self.listField = value |
| 14 |
|
| 15 |
|
| 16 |
async def get_async(value): |
| 17 |
return value |
| 18 |
|
| 19 |
|
| 20 |
def describe_execute_accepts_any_iterable_as_list_value(): |
| 21 |
def _complete(list_field): |
| 22 |
return execute_sync( |
| 23 |
build_schema("type Query { listField: [String] }"), |
| 24 |
parse("{ listField }"), |
| 25 |
Data(list_field), |
| 26 |
) |
| 27 |
|
| 28 |
def accepts_a_set_as_a_list_value(): |
| 29 |
# Note that sets are not ordered in Python. |
| 30 |
list_field = {"apple", "banana", "coconut"} |
| 31 |
result = _complete(list_field) |
| 32 |
assert result.errors is None |
| 33 |
assert isinstance(result.data, dict) |
| 34 |
assert list(result.data) == ["listField"] |
| 35 |
assert isinstance(result.data["listField"], list) |
| 36 |
assert set(result.data["listField"]) == list_field |
| 37 |
|
| 38 |
def accepts_a_generator_as_a_list_value(): |
| 39 |
def list_field(): |
| 40 |
yield "one" |
| 41 |
yield 2 |
| 42 |
yield True |
| 43 |
|
| 44 |
assert _complete(list_field()) == ( |
| 45 |
{"listField": ["one", "2", "true"]}, |
| 46 |
None, |
| 47 |
) |
| 48 |
|
| 49 |
def accepts_a_custom_iterable_as_a_list_value(): |
| 50 |
class ListField: |
| 51 |
def __iter__(self): |
| 52 |
self.last = "hello" |
| 53 |
return self |
| 54 |
|
| 55 |
def __next__(self): |
| 56 |
last = self.last |
| 57 |
if last == "stop": |
| 58 |
raise StopIteration |
| 59 |
self.last = "world" if last == "hello" else "stop" |
| 60 |
return last |
| 61 |
|
| 62 |
assert _complete(ListField()) == ( |
| 63 |
{"listField": ["hello", "world"]}, |
| 64 |
None, |
| 65 |
) |
| 66 |
|
| 67 |
def accepts_function_arguments_as_a_list_value(): |
| 68 |
def get_args(*args): |
| 69 |
return args # actually just a tuple, nothing special in Python |
| 70 |
|
| 71 |
assert _complete(get_args("one", "two")) == ( |
| 72 |
{"listField": ["one", "two"]}, |
| 73 |
None, |
| 74 |
) |
| 75 |
|
| 76 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 77 |
def does_not_accept_a_dict_as_a_list_value(): |
| 78 |
assert _complete({1: "one", 2: "two"}) == ( |
| 79 |
{"listField": None}, |
| 80 |
[ |
| 81 |
{ |
| 82 |
"message": "Expected Iterable," |
| 83 |
" but did not find one for field 'Query.listField'.", |
| 84 |
"locations": [(1, 3)], |
| 85 |
"path": ["listField"], |
| 86 |
} |
| 87 |
], |
| 88 |
) |
| 89 |
|
| 90 |
def does_not_accept_iterable_string_literal_as_a_list_value(): |
| 91 |
assert _complete("Singular") == ( |
| 92 |
{"listField": None}, |
| 93 |
[ |
| 94 |
{ |
| 95 |
"message": "Expected Iterable," |
| 96 |
" but did not find one for field 'Query.listField'.", |
| 97 |
"locations": [(1, 3)], |
| 98 |
"path": ["listField"], |
| 99 |
} |
| 100 |
], |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
def describe_execute_handles_list_nullability(): |
| 105 |
async def _complete(list_field: Any, as_type: str) -> ExecutionResult: |
| 106 |
schema = build_schema(f"type Query {{ listField: {as_type} }}") |
| 107 |
document = parse("{ listField }") |
| 108 |
|
| 109 |
def execute_query(list_value: Any) -> Any: |
| 110 |
return execute(schema, document, Data(list_value)) |
| 111 |
|
| 112 |
result = execute_query(list_field) |
| 113 |
assert isinstance(result, ExecutionResult) |
| 114 |
assert await execute_query(get_async(list_field)) == result |
| 115 |
if isinstance(list_field, list): |
| 116 |
assert await execute_query(list(map(get_async, list_field))) == result |
| 117 |
assert await execute_query(get_async(list_field)) == result |
| 118 |
|
| 119 |
return result |
| 120 |
|
| 121 |
@mark.asyncio |
| 122 |
async def contains_values(): |
| 123 |
list_field = [1, 2] |
| 124 |
assert await _complete(list_field, "[Int]") == ({"listField": [1, 2]}, None) |
| 125 |
assert await _complete(list_field, "[Int]!") == ({"listField": [1, 2]}, None) |
| 126 |
assert await _complete(list_field, "[Int!]") == ({"listField": [1, 2]}, None) |
| 127 |
assert await _complete(list_field, "[Int!]!") == ({"listField": [1, 2]}, None) |
| 128 |
|
| 129 |
@mark.asyncio |
| 130 |
async def contains_null(): |
| 131 |
list_field = [1, None, 2] |
| 132 |
errors = [ |
| 133 |
{ |
| 134 |
"message": "Cannot return null for non-nullable field Query.listField.", |
| 135 |
"locations": [(1, 3)], |
| 136 |
"path": ["listField", 1], |
| 137 |
} |
| 138 |
] |
| 139 |
assert await _complete(list_field, "[Int]") == ( |
| 140 |
{"listField": [1, None, 2]}, |
| 141 |
None, |
| 142 |
) |
| 143 |
assert await _complete(list_field, "[Int]!") == ( |
| 144 |
{"listField": [1, None, 2]}, |
| 145 |
None, |
| 146 |
) |
| 147 |
assert await _complete(list_field, "[Int!]") == ({"listField": None}, errors) |
| 148 |
assert await _complete(list_field, "[Int!]!") == (None, errors) |
| 149 |
|
| 150 |
@mark.asyncio |
| 151 |
async def returns_null(): |
| 152 |
list_field = None |
| 153 |
errors = [ |
| 154 |
{ |
| 155 |
"message": "Cannot return null for non-nullable field Query.listField.", |
| 156 |
"locations": [(1, 3)], |
| 157 |
"path": ["listField"], |
| 158 |
} |
| 159 |
] |
| 160 |
assert await _complete(list_field, "[Int]") == ({"listField": None}, None) |
| 161 |
assert await _complete(list_field, "[Int]!") == (None, errors) |
| 162 |
assert await _complete(list_field, "[Int!]") == ({"listField": None}, None) |
| 163 |
assert await _complete(list_field, "[Int!]!") == (None, errors) |
| 164 |
|
| 165 |
@mark.asyncio |
| 166 |
async def contains_error(): |
| 167 |
list_field = [1, RuntimeError("bad"), 2] |
| 168 |
errors = [ |
| 169 |
{ |
| 170 |
"message": "bad", |
| 171 |
"locations": [(1, 3)], |
| 172 |
"path": ["listField", 1], |
| 173 |
} |
| 174 |
] |
| 175 |
assert await _complete(list_field, "[Int]") == ( |
| 176 |
{"listField": [1, None, 2]}, |
| 177 |
errors, |
| 178 |
) |
| 179 |
assert await _complete(list_field, "[Int]!") == ( |
| 180 |
{"listField": [1, None, 2]}, |
| 181 |
errors, |
| 182 |
) |
| 183 |
assert await _complete(list_field, "[Int!]") == ( |
| 184 |
{"listField": None}, |
| 185 |
errors, |
| 186 |
) |
| 187 |
assert await _complete(list_field, "[Int!]!") == ( |
| 188 |
None, |
| 189 |
errors, |
| 190 |
) |
| 191 |
|
| 192 |
@mark.asyncio |
| 193 |
async def results_in_errors(): |
| 194 |
list_field = RuntimeError("bad") |
| 195 |
errors = [ |
| 196 |
{ |
| 197 |
"message": "bad", |
| 198 |
"locations": [(1, 3)], |
| 199 |
"path": ["listField"], |
| 200 |
} |
| 201 |
] |
| 202 |
assert await _complete(list_field, "[Int]") == ( |
| 203 |
{"listField": None}, |
| 204 |
errors, |
| 205 |
) |
| 206 |
assert await _complete(list_field, "[Int]!") == ( |
| 207 |
None, |
| 208 |
errors, |
| 209 |
) |
| 210 |
assert await _complete(list_field, "[Int!]") == ( |
| 211 |
{"listField": None}, |
| 212 |
errors, |
| 213 |
) |
| 214 |
assert await _complete(list_field, "[Int!]!") == ( |
| 215 |
None, |
| 216 |
errors, |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def describe_experimental_execute_accepts_async_iterables_as_list_value(): |
| 221 |
async def _complete(list_field): |
| 222 |
result = execute( |
| 223 |
build_schema("type Query { listField: [String] }"), |
| 224 |
parse("{ listField }"), |
| 225 |
Data(list_field), |
| 226 |
) |
| 227 |
assert is_awaitable(result) |
| 228 |
result = cast(Awaitable, result) |
| 229 |
return await result |
| 230 |
|
| 231 |
@mark.asyncio |
| 232 |
async def accepts_an_async_generator_as_a_list_value(): |
| 233 |
async def list_field(): |
| 234 |
yield "one" |
| 235 |
yield 2 |
| 236 |
yield True |
| 237 |
|
| 238 |
assert await _complete(list_field()) == ( |
| 239 |
{"listField": ["one", "2", "true"]}, |
| 240 |
None, |
| 241 |
) |
| 242 |
|
| 243 |
@mark.asyncio |
| 244 |
async def accepts_a_custom_async_iterable_as_a_list_value(): |
| 245 |
class ListField: |
| 246 |
def __aiter__(self): |
| 247 |
self.last = "hello" |
| 248 |
return self |
| 249 |
|
| 250 |
async def __anext__(self): |
| 251 |
last = self.last |
| 252 |
if last == "stop": |
| 253 |
raise StopAsyncIteration |
| 254 |
self.last = "world" if last == "hello" else "stop" |
| 255 |
return last |
| 256 |
|
| 257 |
assert await _complete(ListField()) == ( |
| 258 |
{"listField": ["hello", "world"]}, |
| 259 |
None, |
| 260 |
) |