| 1 |
import re |
| 2 |
from asyncio import sleep |
| 3 |
from typing import Any, Awaitable, cast |
| 4 |
|
| 5 |
from pytest import mark |
| 6 |
|
| 7 |
from graphql.execution import execute, execute_sync, ExecutionResult |
| 8 |
from graphql.language import parse |
| 9 |
from graphql.pyutils import AwaitableOrValue |
| 10 |
from graphql.type import ( |
| 11 |
GraphQLArgument, |
| 12 |
GraphQLField, |
| 13 |
GraphQLNonNull, |
| 14 |
GraphQLObjectType, |
| 15 |
GraphQLSchema, |
| 16 |
GraphQLString, |
| 17 |
) |
| 18 |
from graphql.utilities import build_schema |
| 19 |
|
| 20 |
sync_error = RuntimeError("sync") |
| 21 |
sync_non_null_error = RuntimeError("syncNonNull") |
| 22 |
promise_error = RuntimeError("promise") |
| 23 |
promise_non_null_error = RuntimeError("promiseNonNull") |
| 24 |
|
| 25 |
|
| 26 |
# noinspection PyPep8Naming,PyMethodMayBeStatic |
| 27 |
class ThrowingData: |
| 28 |
def sync(self, _info): |
| 29 |
raise sync_error |
| 30 |
|
| 31 |
def syncNonNull(self, _info): |
| 32 |
raise sync_non_null_error |
| 33 |
|
| 34 |
async def promise(self, _info): |
| 35 |
raise promise_error |
| 36 |
|
| 37 |
async def promiseNonNull(self, _info): |
| 38 |
raise promise_non_null_error |
| 39 |
|
| 40 |
def syncNest(self, _info): |
| 41 |
return ThrowingData() |
| 42 |
|
| 43 |
def syncNonNullNest(self, _info): |
| 44 |
return ThrowingData() |
| 45 |
|
| 46 |
async def promiseNest(self, _info): |
| 47 |
return ThrowingData() |
| 48 |
|
| 49 |
async def promiseNonNullNest(self, _info): |
| 50 |
return ThrowingData() |
| 51 |
|
| 52 |
|
| 53 |
# noinspection PyPep8Naming,PyMethodMayBeStatic |
| 54 |
class NullingData: |
| 55 |
def sync(self, _info): |
| 56 |
return None |
| 57 |
|
| 58 |
def syncNonNull(self, _info): |
| 59 |
return None |
| 60 |
|
| 61 |
async def promise(self, _info): |
| 62 |
return None |
| 63 |
|
| 64 |
async def promiseNonNull(self, _info): |
| 65 |
return None |
| 66 |
|
| 67 |
def syncNest(self, _info): |
| 68 |
return NullingData() |
| 69 |
|
| 70 |
def syncNonNullNest(self, _info): |
| 71 |
return NullingData() |
| 72 |
|
| 73 |
async def promiseNest(self, _info): |
| 74 |
return NullingData() |
| 75 |
|
| 76 |
async def promiseNonNullNest(self, _info): |
| 77 |
return NullingData() |
| 78 |
|
| 79 |
|
| 80 |
schema = build_schema(""" |
| 81 |
type DataType { |
| 82 |
sync: String |
| 83 |
syncNonNull: String! |
| 84 |
promise: String |
| 85 |
promiseNonNull: String! |
| 86 |
syncNest: DataType |
| 87 |
syncNonNullNest: DataType! |
| 88 |
promiseNest: DataType |
| 89 |
promiseNonNullNest: DataType! |
| 90 |
} |
| 91 |
|
| 92 |
schema { |
| 93 |
query: DataType |
| 94 |
} |
| 95 |
""") |
| 96 |
|
| 97 |
|
| 98 |
def execute_query(query: str, root_value: Any) -> AwaitableOrValue[ExecutionResult]: |
| 99 |
return execute(schema=schema, document=parse(query), root_value=root_value) |
| 100 |
|
| 101 |
|
| 102 |
# avoids also doing any nests |
| 103 |
def patch(data: str) -> str: |
| 104 |
return re.sub( |
| 105 |
r"\bsyncNonNull\b", "promiseNonNull", re.sub(r"\bsync\b", "promise", data) |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
async def execute_sync_and_async(query: str, root_value: Any) -> ExecutionResult: |
| 110 |
sync_result = execute_sync(schema, parse(query), root_value) |
| 111 |
async_result = await cast( |
| 112 |
Awaitable[ExecutionResult], execute(schema, parse(patch(query)), root_value) |
| 113 |
) |
| 114 |
|
| 115 |
assert repr(async_result) == patch(repr(sync_result)) |
| 116 |
return sync_result |
| 117 |
|
| 118 |
|
| 119 |
def describe_execute_handles_non_nullable_types(): |
| 120 |
def describe_nulls_a_nullable_field(): |
| 121 |
query = """ |
| 122 |
{ |
| 123 |
sync |
| 124 |
} |
| 125 |
""" |
| 126 |
|
| 127 |
@mark.asyncio |
| 128 |
async def returns_null(): |
| 129 |
result = await execute_sync_and_async(query, NullingData()) |
| 130 |
assert result == ({"sync": None}, None) |
| 131 |
|
| 132 |
@mark.asyncio |
| 133 |
async def throws(): |
| 134 |
result = await execute_sync_and_async(query, ThrowingData()) |
| 135 |
assert result == ( |
| 136 |
{"sync": None}, |
| 137 |
[ |
| 138 |
{ |
| 139 |
"message": str(sync_error), |
| 140 |
"path": ["sync"], |
| 141 |
"locations": [(3, 15)], |
| 142 |
} |
| 143 |
], |
| 144 |
) |
| 145 |
|
| 146 |
def describe_nulls_a_returned_object_that_contains_a_non_null_field(): |
| 147 |
|
| 148 |
query = """ |
| 149 |
{ |
| 150 |
syncNest { |
| 151 |
syncNonNull, |
| 152 |
} |
| 153 |
} |
| 154 |
""" |
| 155 |
|
| 156 |
@mark.asyncio |
| 157 |
async def that_returns_null(): |
| 158 |
result = await execute_sync_and_async(query, NullingData()) |
| 159 |
assert result == ( |
| 160 |
{"syncNest": None}, |
| 161 |
[ |
| 162 |
{ |
| 163 |
"message": "Cannot return null for non-nullable field" |
| 164 |
" DataType.syncNonNull.", |
| 165 |
"path": ["syncNest", "syncNonNull"], |
| 166 |
"locations": [(4, 17)], |
| 167 |
} |
| 168 |
], |
| 169 |
) |
| 170 |
|
| 171 |
@mark.asyncio |
| 172 |
async def that_throws(): |
| 173 |
result = await execute_sync_and_async(query, ThrowingData()) |
| 174 |
assert result == ( |
| 175 |
{"syncNest": None}, |
| 176 |
[ |
| 177 |
{ |
| 178 |
"message": str(sync_non_null_error), |
| 179 |
"path": ["syncNest", "syncNonNull"], |
| 180 |
"locations": [(4, 17)], |
| 181 |
} |
| 182 |
], |
| 183 |
) |
| 184 |
|
| 185 |
def describe_nulls_a_complex_tree_of_nullable_fields_each(): |
| 186 |
query = """ |
| 187 |
{ |
| 188 |
syncNest { |
| 189 |
sync |
| 190 |
promise |
| 191 |
syncNest { sync promise } |
| 192 |
promiseNest { sync promise } |
| 193 |
} |
| 194 |
promiseNest { |
| 195 |
sync |
| 196 |
promise |
| 197 |
syncNest { sync promise } |
| 198 |
promiseNest { sync promise } |
| 199 |
} |
| 200 |
} |
| 201 |
""" |
| 202 |
data = { |
| 203 |
"syncNest": { |
| 204 |
"sync": None, |
| 205 |
"promise": None, |
| 206 |
"syncNest": {"sync": None, "promise": None}, |
| 207 |
"promiseNest": {"sync": None, "promise": None}, |
| 208 |
}, |
| 209 |
"promiseNest": { |
| 210 |
"sync": None, |
| 211 |
"promise": None, |
| 212 |
"syncNest": {"sync": None, "promise": None}, |
| 213 |
"promiseNest": {"sync": None, "promise": None}, |
| 214 |
}, |
| 215 |
} |
| 216 |
|
| 217 |
@mark.asyncio |
| 218 |
async def returns_null(): |
| 219 |
result = await cast( |
| 220 |
Awaitable[ExecutionResult], execute_query(query, NullingData()) |
| 221 |
) |
| 222 |
assert result == (data, None) |
| 223 |
|
| 224 |
@mark.asyncio |
| 225 |
async def throws(): |
| 226 |
result = await cast( |
| 227 |
Awaitable[ExecutionResult], execute_query(query, ThrowingData()) |
| 228 |
) |
| 229 |
assert result == ( |
| 230 |
data, |
| 231 |
[ |
| 232 |
{ |
| 233 |
"message": str(sync_error), |
| 234 |
"path": ["syncNest", "sync"], |
| 235 |
"locations": [(4, 17)], |
| 236 |
}, |
| 237 |
{ |
| 238 |
"message": str(promise_error), |
| 239 |
"path": ["syncNest", "promise"], |
| 240 |
"locations": [(5, 17)], |
| 241 |
}, |
| 242 |
{ |
| 243 |
"message": str(sync_error), |
| 244 |
"path": ["syncNest", "syncNest", "sync"], |
| 245 |
"locations": [(6, 28)], |
| 246 |
}, |
| 247 |
{ |
| 248 |
"message": str(promise_error), |
| 249 |
"path": ["syncNest", "syncNest", "promise"], |
| 250 |
"locations": [(6, 33)], |
| 251 |
}, |
| 252 |
{ |
| 253 |
"message": str(sync_error), |
| 254 |
"path": ["syncNest", "promiseNest", "sync"], |
| 255 |
"locations": [(7, 31)], |
| 256 |
}, |
| 257 |
{ |
| 258 |
"message": str(promise_error), |
| 259 |
"path": ["syncNest", "promiseNest", "promise"], |
| 260 |
"locations": [(7, 36)], |
| 261 |
}, |
| 262 |
{ |
| 263 |
"message": str(sync_error), |
| 264 |
"path": ["promiseNest", "sync"], |
| 265 |
"locations": [(10, 17)], |
| 266 |
}, |
| 267 |
{ |
| 268 |
"message": str(promise_error), |
| 269 |
"path": ["promiseNest", "promise"], |
| 270 |
"locations": [(11, 17)], |
| 271 |
}, |
| 272 |
{ |
| 273 |
"message": str(sync_error), |
| 274 |
"path": ["promiseNest", "syncNest", "sync"], |
| 275 |
"locations": [(12, 28)], |
| 276 |
}, |
| 277 |
{ |
| 278 |
"message": str(promise_error), |
| 279 |
"path": ["promiseNest", "syncNest", "promise"], |
| 280 |
"locations": [(12, 33)], |
| 281 |
}, |
| 282 |
{ |
| 283 |
"message": str(sync_error), |
| 284 |
"path": ["promiseNest", "promiseNest", "sync"], |
| 285 |
"locations": [(13, 31)], |
| 286 |
}, |
| 287 |
{ |
| 288 |
"message": str(promise_error), |
| 289 |
"path": ["promiseNest", "promiseNest", "promise"], |
| 290 |
"locations": [(13, 36)], |
| 291 |
}, |
| 292 |
], |
| 293 |
) |
| 294 |
|
| 295 |
def describe_nulls_first_nullable_after_long_chain_of_non_null_fields(): |
| 296 |
query = """ |
| 297 |
{ |
| 298 |
syncNest { |
| 299 |
syncNonNullNest { |
| 300 |
promiseNonNullNest { |
| 301 |
syncNonNullNest { |
| 302 |
promiseNonNullNest { |
| 303 |
syncNonNull |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
promiseNest { |
| 310 |
syncNonNullNest { |
| 311 |
promiseNonNullNest { |
| 312 |
syncNonNullNest { |
| 313 |
promiseNonNullNest { |
| 314 |
syncNonNull |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
anotherNest: syncNest { |
| 321 |
syncNonNullNest { |
| 322 |
promiseNonNullNest { |
| 323 |
syncNonNullNest { |
| 324 |
promiseNonNullNest { |
| 325 |
promiseNonNull |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
anotherPromiseNest: promiseNest { |
| 332 |
syncNonNullNest { |
| 333 |
promiseNonNullNest { |
| 334 |
syncNonNullNest { |
| 335 |
promiseNonNullNest { |
| 336 |
promiseNonNull |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
""" |
| 344 |
data = { |
| 345 |
"syncNest": None, |
| 346 |
"promiseNest": None, |
| 347 |
"anotherNest": None, |
| 348 |
"anotherPromiseNest": None, |
| 349 |
} |
| 350 |
|
| 351 |
@mark.asyncio |
| 352 |
async def returns_null(): |
| 353 |
result = await cast( |
| 354 |
Awaitable[ExecutionResult], execute_query(query, NullingData()) |
| 355 |
) |
| 356 |
assert result == ( |
| 357 |
data, |
| 358 |
[ |
| 359 |
{ |
| 360 |
"message": "Cannot return null for non-nullable field" |
| 361 |
" DataType.syncNonNull.", |
| 362 |
"path": [ |
| 363 |
"syncNest", |
| 364 |
"syncNonNullNest", |
| 365 |
"promiseNonNullNest", |
| 366 |
"syncNonNullNest", |
| 367 |
"promiseNonNullNest", |
| 368 |
"syncNonNull", |
| 369 |
], |
| 370 |
"locations": [(8, 25)], |
| 371 |
}, |
| 372 |
{ |
| 373 |
"message": "Cannot return null for non-nullable field" |
| 374 |
" DataType.syncNonNull.", |
| 375 |
"path": [ |
| 376 |
"promiseNest", |
| 377 |
"syncNonNullNest", |
| 378 |
"promiseNonNullNest", |
| 379 |
"syncNonNullNest", |
| 380 |
"promiseNonNullNest", |
| 381 |
"syncNonNull", |
| 382 |
], |
| 383 |
"locations": [(19, 25)], |
| 384 |
}, |
| 385 |
{ |
| 386 |
"message": "Cannot return null for non-nullable field" |
| 387 |
" DataType.promiseNonNull.", |
| 388 |
"path": [ |
| 389 |
"anotherNest", |
| 390 |
"syncNonNullNest", |
| 391 |
"promiseNonNullNest", |
| 392 |
"syncNonNullNest", |
| 393 |
"promiseNonNullNest", |
| 394 |
"promiseNonNull", |
| 395 |
], |
| 396 |
"locations": [(30, 25)], |
| 397 |
}, |
| 398 |
{ |
| 399 |
"message": "Cannot return null for non-nullable field" |
| 400 |
" DataType.promiseNonNull.", |
| 401 |
"path": [ |
| 402 |
"anotherPromiseNest", |
| 403 |
"syncNonNullNest", |
| 404 |
"promiseNonNullNest", |
| 405 |
"syncNonNullNest", |
| 406 |
"promiseNonNullNest", |
| 407 |
"promiseNonNull", |
| 408 |
], |
| 409 |
"locations": [(41, 25)], |
| 410 |
}, |
| 411 |
], |
| 412 |
) |
| 413 |
|
| 414 |
@mark.asyncio |
| 415 |
async def throws(): |
| 416 |
result = await cast( |
| 417 |
Awaitable[ExecutionResult], execute_query(query, ThrowingData()) |
| 418 |
) |
| 419 |
assert result == ( |
| 420 |
data, |
| 421 |
[ |
| 422 |
{ |
| 423 |
"message": str(sync_non_null_error), |
| 424 |
"path": [ |
| 425 |
"syncNest", |
| 426 |
"syncNonNullNest", |
| 427 |
"promiseNonNullNest", |
| 428 |
"syncNonNullNest", |
| 429 |
"promiseNonNullNest", |
| 430 |
"syncNonNull", |
| 431 |
], |
| 432 |
"locations": [(8, 25)], |
| 433 |
}, |
| 434 |
{ |
| 435 |
"message": str(sync_non_null_error), |
| 436 |
"path": [ |
| 437 |
"promiseNest", |
| 438 |
"syncNonNullNest", |
| 439 |
"promiseNonNullNest", |
| 440 |
"syncNonNullNest", |
| 441 |
"promiseNonNullNest", |
| 442 |
"syncNonNull", |
| 443 |
], |
| 444 |
"locations": [(19, 25)], |
| 445 |
}, |
| 446 |
{ |
| 447 |
"message": str(promise_non_null_error), |
| 448 |
"path": [ |
| 449 |
"anotherNest", |
| 450 |
"syncNonNullNest", |
| 451 |
"promiseNonNullNest", |
| 452 |
"syncNonNullNest", |
| 453 |
"promiseNonNullNest", |
| 454 |
"promiseNonNull", |
| 455 |
], |
| 456 |
"locations": [(30, 25)], |
| 457 |
}, |
| 458 |
{ |
| 459 |
"message": str(promise_non_null_error), |
| 460 |
"path": [ |
| 461 |
"anotherPromiseNest", |
| 462 |
"syncNonNullNest", |
| 463 |
"promiseNonNullNest", |
| 464 |
"syncNonNullNest", |
| 465 |
"promiseNonNullNest", |
| 466 |
"promiseNonNull", |
| 467 |
], |
| 468 |
"locations": [(41, 25)], |
| 469 |
}, |
| 470 |
], |
| 471 |
) |
| 472 |
|
| 473 |
def describe_nulls_the_top_level_if_non_nullable_field(): |
| 474 |
query = """ |
| 475 |
{ |
| 476 |
syncNonNull |
| 477 |
} |
| 478 |
""" |
| 479 |
|
| 480 |
@mark.asyncio |
| 481 |
async def returns_null(): |
| 482 |
result = await execute_sync_and_async(query, NullingData()) |
| 483 |
assert result == ( |
| 484 |
None, |
| 485 |
[ |
| 486 |
{ |
| 487 |
"message": "Cannot return null for non-nullable field" |
| 488 |
" DataType.syncNonNull.", |
| 489 |
"path": ["syncNonNull"], |
| 490 |
"locations": [(3, 17)], |
| 491 |
} |
| 492 |
], |
| 493 |
) |
| 494 |
|
| 495 |
@mark.asyncio |
| 496 |
async def throws(): |
| 497 |
result = await execute_sync_and_async(query, ThrowingData()) |
| 498 |
assert result == ( |
| 499 |
None, |
| 500 |
[ |
| 501 |
{ |
| 502 |
"message": str(sync_non_null_error), |
| 503 |
"path": ["syncNonNull"], |
| 504 |
"locations": [(3, 17)], |
| 505 |
} |
| 506 |
], |
| 507 |
) |
| 508 |
|
| 509 |
def describe_handles_multiple_errors_for_a_single_response_position(): |
| 510 |
@mark.asyncio |
| 511 |
async def nullable_and_non_nullable_root_fields_throw_nested_errors(): |
| 512 |
query = """ |
| 513 |
{ |
| 514 |
promiseNonNullNest { |
| 515 |
syncNonNull |
| 516 |
} |
| 517 |
promiseNest { |
| 518 |
syncNonNull |
| 519 |
} |
| 520 |
} |
| 521 |
""" |
| 522 |
result = await cast( |
| 523 |
Awaitable[ExecutionResult], execute_query(query, ThrowingData()) |
| 524 |
) |
| 525 |
assert result == ( |
| 526 |
None, |
| 527 |
[ |
| 528 |
{ |
| 529 |
"message": str(sync_non_null_error), |
| 530 |
"path": ["promiseNonNullNest", "syncNonNull"], |
| 531 |
"locations": [(4, 21)], |
| 532 |
}, |
| 533 |
{ |
| 534 |
"message": str(sync_non_null_error), |
| 535 |
"path": ["promiseNest", "syncNonNull"], |
| 536 |
"locations": [(7, 21)], |
| 537 |
}, |
| 538 |
], |
| 539 |
) |
| 540 |
|
| 541 |
@mark.asyncio |
| 542 |
async def slower_nullable_after_non_nullable_root_field(): |
| 543 |
query = """ |
| 544 |
{ |
| 545 |
promiseNonNullNest { |
| 546 |
syncNonNull |
| 547 |
} |
| 548 |
promiseNest { |
| 549 |
promiseNonNull |
| 550 |
} |
| 551 |
} |
| 552 |
""" |
| 553 |
result = await cast( |
| 554 |
Awaitable[ExecutionResult], execute_query(query, ThrowingData()) |
| 555 |
) |
| 556 |
assert result == ( |
| 557 |
None, |
| 558 |
[ |
| 559 |
{ |
| 560 |
"message": str(sync_non_null_error), |
| 561 |
"path": ["promiseNonNullNest", "syncNonNull"], |
| 562 |
"locations": [(4, 21)], |
| 563 |
}, |
| 564 |
], |
| 565 |
) |
| 566 |
|
| 567 |
# allow time for slower error to reject |
| 568 |
assert result.errors is not None |
| 569 |
initial_errors = list(result.errors) |
| 570 |
for _ in range(5): |
| 571 |
await sleep(0) |
| 572 |
assert result.errors == initial_errors |
| 573 |
|
| 574 |
@mark.asyncio |
| 575 |
async def nullable_and_non_nullable_nested_fields_throw_nested_errors(): |
| 576 |
query = """ |
| 577 |
{ |
| 578 |
syncNest { |
| 579 |
promiseNonNullNest { |
| 580 |
syncNonNull |
| 581 |
} |
| 582 |
promiseNest { |
| 583 |
syncNonNull |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
""" |
| 588 |
result = await cast( |
| 589 |
Awaitable[ExecutionResult], execute_query(query, ThrowingData()) |
| 590 |
) |
| 591 |
assert result == ( |
| 592 |
{"syncNest": None}, |
| 593 |
[ |
| 594 |
{ |
| 595 |
"message": str(sync_non_null_error), |
| 596 |
"path": ["syncNest", "promiseNonNullNest", "syncNonNull"], |
| 597 |
"locations": [(5, 23)], |
| 598 |
}, |
| 599 |
{ |
| 600 |
"message": str(sync_non_null_error), |
| 601 |
"path": ["syncNest", "promiseNest", "syncNonNull"], |
| 602 |
"locations": [(8, 23)], |
| 603 |
}, |
| 604 |
], |
| 605 |
) |
| 606 |
|
| 607 |
@mark.asyncio |
| 608 |
async def slower_nullable_after_non_nullable_nested_field(): |
| 609 |
query = """ |
| 610 |
{ |
| 611 |
syncNest { |
| 612 |
promiseNonNullNest { |
| 613 |
syncNonNull |
| 614 |
} |
| 615 |
promiseNest { |
| 616 |
promiseNest { |
| 617 |
promiseNest { |
| 618 |
promiseNonNull |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
""" |
| 625 |
result = await cast( |
| 626 |
Awaitable[ExecutionResult], execute_query(query, ThrowingData()) |
| 627 |
) |
| 628 |
assert result == ( |
| 629 |
{"syncNest": None}, |
| 630 |
[ |
| 631 |
{ |
| 632 |
"message": str(sync_non_null_error), |
| 633 |
"path": ["syncNest", "promiseNonNullNest", "syncNonNull"], |
| 634 |
"locations": [(5, 23)], |
| 635 |
}, |
| 636 |
], |
| 637 |
) |
| 638 |
|
| 639 |
assert result.errors is not None |
| 640 |
initial_errors = list(result.errors) |
| 641 |
for _ in range(20): |
| 642 |
await sleep(0) |
| 643 |
assert result.errors == initial_errors |
| 644 |
|
| 645 |
def describe_handles_non_null_argument(): |
| 646 |
|
| 647 |
# noinspection PyPep8Naming |
| 648 |
schema_with_non_null_arg = GraphQLSchema( |
| 649 |
GraphQLObjectType( |
| 650 |
"Query", |
| 651 |
{ |
| 652 |
"withNonNullArg": GraphQLField( |
| 653 |
GraphQLString, |
| 654 |
args={ |
| 655 |
"cannotBeNull": GraphQLArgument( |
| 656 |
GraphQLNonNull(GraphQLString) |
| 657 |
) |
| 658 |
}, |
| 659 |
resolve=lambda _obj, _info, cannotBeNull: "Passed: " |
| 660 |
+ str(cannotBeNull), |
| 661 |
) |
| 662 |
}, |
| 663 |
) |
| 664 |
) |
| 665 |
|
| 666 |
def succeeds_when_passed_non_null_literal_value(): |
| 667 |
result = execute_sync( |
| 668 |
schema_with_non_null_arg, |
| 669 |
parse(""" |
| 670 |
query { |
| 671 |
withNonNullArg (cannotBeNull: "literal value") |
| 672 |
} |
| 673 |
"""), |
| 674 |
) |
| 675 |
|
| 676 |
assert result == ({"withNonNullArg": "Passed: literal value"}, None) |
| 677 |
|
| 678 |
def succeeds_when_passed_non_null_variable_value(): |
| 679 |
result = execute_sync( |
| 680 |
schema_with_non_null_arg, |
| 681 |
parse(""" |
| 682 |
query ($testVar: String!) { |
| 683 |
withNonNullArg (cannotBeNull: $testVar) |
| 684 |
} |
| 685 |
"""), |
| 686 |
variable_values={ |
| 687 |
"testVar": "variable value", |
| 688 |
}, |
| 689 |
) |
| 690 |
|
| 691 |
assert result == ({"withNonNullArg": "Passed: variable value"}, None) |
| 692 |
|
| 693 |
def succeeds_when_missing_variable_has_default_value(): |
| 694 |
result = execute_sync( |
| 695 |
schema_with_non_null_arg, |
| 696 |
parse(""" |
| 697 |
query ($testVar: String = "default value") { |
| 698 |
withNonNullArg (cannotBeNull: $testVar) |
| 699 |
} |
| 700 |
"""), |
| 701 |
variable_values={}, # intentionally missing variable |
| 702 |
) |
| 703 |
|
| 704 |
assert result == ({"withNonNullArg": "Passed: default value"}, None) |
| 705 |
|
| 706 |
def field_error_when_missing_non_null_arg(): |
| 707 |
# Note: validation should identify this issue first |
| 708 |
# (missing args rule) however execution should still |
| 709 |
# protect against this. |
| 710 |
result = execute_sync( |
| 711 |
schema_with_non_null_arg, |
| 712 |
parse(""" |
| 713 |
query { |
| 714 |
withNonNullArg |
| 715 |
} |
| 716 |
"""), |
| 717 |
) |
| 718 |
|
| 719 |
assert result == ( |
| 720 |
{"withNonNullArg": None}, |
| 721 |
[ |
| 722 |
{ |
| 723 |
"message": "Argument 'cannotBeNull' of required type" |
| 724 |
" 'String!' was not provided.", |
| 725 |
"locations": [(3, 23)], |
| 726 |
"path": ["withNonNullArg"], |
| 727 |
} |
| 728 |
], |
| 729 |
) |
| 730 |
|
| 731 |
def field_error_when_non_null_arg_provided_null(): |
| 732 |
# Note: validation should identify this issue first |
| 733 |
# (values of correct type rule) however execution |
| 734 |
# should still protect against this. |
| 735 |
result = execute_sync( |
| 736 |
schema_with_non_null_arg, |
| 737 |
parse(""" |
| 738 |
query { |
| 739 |
withNonNullArg(cannotBeNull: null) |
| 740 |
} |
| 741 |
"""), |
| 742 |
) |
| 743 |
|
| 744 |
assert result == ( |
| 745 |
{"withNonNullArg": None}, |
| 746 |
[ |
| 747 |
{ |
| 748 |
"message": "Argument 'cannotBeNull' of non-null type" |
| 749 |
" 'String!' must not be null.", |
| 750 |
"locations": [(3, 52)], |
| 751 |
"path": ["withNonNullArg"], |
| 752 |
} |
| 753 |
], |
| 754 |
) |
| 755 |
|
| 756 |
def field_error_when_non_null_arg_not_provided_variable_value(): |
| 757 |
# Note: validation should identify this issue first |
| 758 |
# (variables in allowed position rule) however execution |
| 759 |
# should still protect against this. |
| 760 |
result = execute_sync( |
| 761 |
schema_with_non_null_arg, |
| 762 |
parse(""" |
| 763 |
query ($testVar: String) { |
| 764 |
withNonNullArg(cannotBeNull: $testVar) |
| 765 |
} |
| 766 |
"""), |
| 767 |
variable_values={}, |
| 768 |
) # intentionally missing variable |
| 769 |
|
| 770 |
assert result == ( |
| 771 |
{"withNonNullArg": None}, |
| 772 |
[ |
| 773 |
{ |
| 774 |
"message": "Argument 'cannotBeNull' of required type" |
| 775 |
" 'String!' was provided the variable" |
| 776 |
" '$testVar' which was not provided" |
| 777 |
" a runtime value.", |
| 778 |
"locations": [(3, 52)], |
| 779 |
"path": ["withNonNullArg"], |
| 780 |
} |
| 781 |
], |
| 782 |
) |
| 783 |
|
| 784 |
def field_error_when_non_null_arg_provided_explicit_null_variable(): |
| 785 |
result = execute_sync( |
| 786 |
schema_with_non_null_arg, |
| 787 |
parse(""" |
| 788 |
query ($testVar: String = "default value") { |
| 789 |
withNonNullArg (cannotBeNull: $testVar) |
| 790 |
} |
| 791 |
"""), |
| 792 |
variable_values={"testVar": None}, |
| 793 |
) |
| 794 |
|
| 795 |
assert result == ( |
| 796 |
{"withNonNullArg": None}, |
| 797 |
[ |
| 798 |
{ |
| 799 |
"message": "Argument 'cannotBeNull' of non-null type" |
| 800 |
" 'String!' must not be null.", |
| 801 |
"locations": [(3, 53)], |
| 802 |
"path": ["withNonNullArg"], |
| 803 |
} |
| 804 |
], |
| 805 |
) |