| 1 |
import asyncio |
| 2 |
from typing import Any, Awaitable, Optional, cast |
| 3 |
|
| 4 |
from graphql.error import GraphQLError |
| 5 |
from graphql.execution import execute, execute_sync |
| 6 |
from graphql.language import FieldNode, OperationDefinitionNode, parse |
| 7 |
from graphql.pyutils import Undefined, inspect |
| 8 |
from graphql.type import ( |
| 9 |
GraphQLArgument, |
| 10 |
GraphQLBoolean, |
| 11 |
GraphQLField, |
| 12 |
GraphQLInputField, |
| 13 |
GraphQLInputObjectType, |
| 14 |
GraphQLInt, |
| 15 |
GraphQLInterfaceType, |
| 16 |
GraphQLList, |
| 17 |
GraphQLNonNull, |
| 18 |
GraphQLObjectType, |
| 19 |
GraphQLResolveInfo, |
| 20 |
GraphQLScalarType, |
| 21 |
GraphQLSchema, |
| 22 |
GraphQLString, |
| 23 |
GraphQLUnionType, |
| 24 |
ResponsePath, |
| 25 |
) |
| 26 |
from pytest import mark, raises |
| 27 |
|
| 28 |
|
| 29 |
def describe_execute_handles_basic_execution_tasks(): |
| 30 |
# noinspection PyTypeChecker |
| 31 |
def throws_if_no_document_is_provided(): |
| 32 |
schema = GraphQLSchema( |
| 33 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 34 |
) |
| 35 |
|
| 36 |
with raises(TypeError) as exc_info: |
| 37 |
assert execute_sync(schema=schema, document=None) # type: ignore |
| 38 |
|
| 39 |
assert str(exc_info.value) == "Must provide document." |
| 40 |
|
| 41 |
# noinspection PyTypeChecker |
| 42 |
def throws_if_no_schema_is_provided(): |
| 43 |
document = parse("{ field }") |
| 44 |
|
| 45 |
with raises(TypeError) as exc_info: |
| 46 |
assert execute_sync(schema=None, document=document) # type: ignore |
| 47 |
|
| 48 |
assert str(exc_info.value) == "Expected None to be a GraphQL schema." |
| 49 |
|
| 50 |
def throws_on_invalid_variables(): |
| 51 |
schema = GraphQLSchema( |
| 52 |
GraphQLObjectType( |
| 53 |
"Type", |
| 54 |
{ |
| 55 |
"fieldA": GraphQLField( |
| 56 |
GraphQLString, args={"argA": GraphQLArgument(GraphQLInt)} |
| 57 |
) |
| 58 |
}, |
| 59 |
) |
| 60 |
) |
| 61 |
document = parse(""" |
| 62 |
query ($a: Int) { |
| 63 |
fieldA(argA: $a) |
| 64 |
} |
| 65 |
""") |
| 66 |
variable_values = "{'a': 1}" |
| 67 |
|
| 68 |
with raises(TypeError) as exc_info: |
| 69 |
assert execute_sync( |
| 70 |
schema=schema, |
| 71 |
document=document, |
| 72 |
variable_values=variable_values, # type: ignore |
| 73 |
) |
| 74 |
|
| 75 |
assert str(exc_info.value) == ( |
| 76 |
"Variable values must be provided as a dictionary" |
| 77 |
" with variable names as keys. Perhaps look to see" |
| 78 |
" if an unparsed JSON string was provided." |
| 79 |
) |
| 80 |
|
| 81 |
def accepts_positional_arguments(): |
| 82 |
schema = GraphQLSchema( |
| 83 |
GraphQLObjectType( |
| 84 |
"Type", |
| 85 |
{"a": GraphQLField(GraphQLString, resolve=lambda obj, *args: obj)}, |
| 86 |
) |
| 87 |
) |
| 88 |
|
| 89 |
result = execute_sync(schema, parse("{ a }"), "rootValue") |
| 90 |
|
| 91 |
assert result == ({"a": "rootValue"}, None) |
| 92 |
|
| 93 |
@mark.asyncio |
| 94 |
async def executes_arbitrary_code(): |
| 95 |
# noinspection PyMethodMayBeStatic,PyMethodMayBeStatic |
| 96 |
class Data: |
| 97 |
def a(self, _info): |
| 98 |
return "Apple" |
| 99 |
|
| 100 |
def b(self, _info): |
| 101 |
return "Banana" |
| 102 |
|
| 103 |
def c(self, _info): |
| 104 |
return "Cookie" |
| 105 |
|
| 106 |
def d(self, _info): |
| 107 |
return "Donut" |
| 108 |
|
| 109 |
def e(self, _info): |
| 110 |
return "Egg" |
| 111 |
|
| 112 |
f = "Fish" |
| 113 |
|
| 114 |
# Called only by DataType::pic static resolver |
| 115 |
def pic(self, _info, size: int): |
| 116 |
return f"Pic of size: {size}" |
| 117 |
|
| 118 |
def deep(self, _info): |
| 119 |
return DeepData() |
| 120 |
|
| 121 |
def promise(self, _info): |
| 122 |
return promise_data() |
| 123 |
|
| 124 |
# noinspection PyMethodMayBeStatic,PyMethodMayBeStatic |
| 125 |
class DeepData: |
| 126 |
def a(self, _info): |
| 127 |
return "Already Been Done" |
| 128 |
|
| 129 |
def b(self, _info): |
| 130 |
return "Boring" |
| 131 |
|
| 132 |
def c(self, _info): |
| 133 |
return ["Contrived", None, "Confusing"] |
| 134 |
|
| 135 |
def deeper(self, _info): |
| 136 |
return [Data(), None, Data()] |
| 137 |
|
| 138 |
async def promise_data(): |
| 139 |
await asyncio.sleep(0) |
| 140 |
return Data() |
| 141 |
|
| 142 |
DeepDataType: GraphQLObjectType |
| 143 |
|
| 144 |
DataType = GraphQLObjectType( |
| 145 |
"DataType", |
| 146 |
lambda: { |
| 147 |
"a": GraphQLField(GraphQLString), |
| 148 |
"b": GraphQLField(GraphQLString), |
| 149 |
"c": GraphQLField(GraphQLString), |
| 150 |
"d": GraphQLField(GraphQLString), |
| 151 |
"e": GraphQLField(GraphQLString), |
| 152 |
"f": GraphQLField(GraphQLString), |
| 153 |
"pic": GraphQLField( |
| 154 |
GraphQLString, |
| 155 |
args={"size": GraphQLArgument(GraphQLInt)}, |
| 156 |
resolve=lambda obj, info, size: obj.pic(info, size), |
| 157 |
), |
| 158 |
"deep": GraphQLField(DeepDataType), |
| 159 |
"promise": GraphQLField(DataType), |
| 160 |
}, |
| 161 |
) |
| 162 |
|
| 163 |
DeepDataType = GraphQLObjectType( |
| 164 |
"DeepDataType", |
| 165 |
{ |
| 166 |
"a": GraphQLField(GraphQLString), |
| 167 |
"b": GraphQLField(GraphQLString), |
| 168 |
"c": GraphQLField(GraphQLList(GraphQLString)), |
| 169 |
"deeper": GraphQLField(GraphQLList(DataType)), |
| 170 |
}, |
| 171 |
) |
| 172 |
|
| 173 |
document = parse(""" |
| 174 |
query ($size: Int) { |
| 175 |
a, |
| 176 |
b, |
| 177 |
x: c |
| 178 |
...c |
| 179 |
f |
| 180 |
...on DataType { |
| 181 |
pic(size: $size) |
| 182 |
promise { |
| 183 |
a |
| 184 |
} |
| 185 |
} |
| 186 |
deep { |
| 187 |
a |
| 188 |
b |
| 189 |
c |
| 190 |
deeper { |
| 191 |
a |
| 192 |
b |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
fragment c on DataType { |
| 198 |
d |
| 199 |
e |
| 200 |
} |
| 201 |
""") |
| 202 |
|
| 203 |
awaitable_result = execute( |
| 204 |
GraphQLSchema(DataType), document, Data(), variable_values={"size": 100} |
| 205 |
) |
| 206 |
assert isinstance(awaitable_result, Awaitable) |
| 207 |
result = await awaitable_result |
| 208 |
|
| 209 |
assert result == ( |
| 210 |
{ |
| 211 |
"a": "Apple", |
| 212 |
"b": "Banana", |
| 213 |
"x": "Cookie", |
| 214 |
"d": "Donut", |
| 215 |
"e": "Egg", |
| 216 |
"f": "Fish", |
| 217 |
"pic": "Pic of size: 100", |
| 218 |
"promise": {"a": "Apple"}, |
| 219 |
"deep": { |
| 220 |
"a": "Already Been Done", |
| 221 |
"b": "Boring", |
| 222 |
"c": ["Contrived", None, "Confusing"], |
| 223 |
"deeper": [ |
| 224 |
{"a": "Apple", "b": "Banana"}, |
| 225 |
None, |
| 226 |
{"a": "Apple", "b": "Banana"}, |
| 227 |
], |
| 228 |
}, |
| 229 |
}, |
| 230 |
None, |
| 231 |
) |
| 232 |
|
| 233 |
def merges_parallel_fragments(): |
| 234 |
Type = GraphQLObjectType( |
| 235 |
"Type", |
| 236 |
lambda: { |
| 237 |
"a": GraphQLField(GraphQLString, resolve=lambda *_args: "Apple"), |
| 238 |
"b": GraphQLField(GraphQLString, resolve=lambda *_args: "Banana"), |
| 239 |
"c": GraphQLField(GraphQLString, resolve=lambda *_args: "Cherry"), |
| 240 |
"deep": GraphQLField(Type, resolve=lambda *_args: {}), |
| 241 |
}, |
| 242 |
) |
| 243 |
schema = GraphQLSchema(Type) |
| 244 |
|
| 245 |
ast = parse(""" |
| 246 |
{ a, ...FragOne, ...FragTwo } |
| 247 |
|
| 248 |
fragment FragOne on Type { |
| 249 |
b |
| 250 |
deep { b, deeper: deep { b } } |
| 251 |
} |
| 252 |
|
| 253 |
fragment FragTwo on Type { |
| 254 |
c |
| 255 |
deep { c, deeper: deep { c } } |
| 256 |
} |
| 257 |
""") |
| 258 |
|
| 259 |
result = execute_sync(schema, ast) |
| 260 |
assert result == ( |
| 261 |
{ |
| 262 |
"a": "Apple", |
| 263 |
"b": "Banana", |
| 264 |
"c": "Cherry", |
| 265 |
"deep": { |
| 266 |
"b": "Banana", |
| 267 |
"c": "Cherry", |
| 268 |
"deeper": {"b": "Banana", "c": "Cherry"}, |
| 269 |
}, |
| 270 |
}, |
| 271 |
None, |
| 272 |
) |
| 273 |
|
| 274 |
def provides_info_about_current_execution_state(): |
| 275 |
resolved_infos = [] |
| 276 |
|
| 277 |
def resolve(_obj, info): |
| 278 |
resolved_infos.append(info) |
| 279 |
|
| 280 |
test_type = GraphQLObjectType( |
| 281 |
"Test", {"test": GraphQLField(GraphQLString, resolve=resolve)} |
| 282 |
) |
| 283 |
|
| 284 |
schema = GraphQLSchema(test_type) |
| 285 |
|
| 286 |
document = parse("query ($var: String) { result: test }") |
| 287 |
root_value = {"root": "val"} |
| 288 |
variable_values = {"var": "abc"} |
| 289 |
execute_sync(schema, document, root_value, variable_values=variable_values) |
| 290 |
|
| 291 |
assert len(resolved_infos) == 1 |
| 292 |
operation = cast(OperationDefinitionNode, document.definitions[0]) |
| 293 |
assert operation and operation.kind == "operation_definition" |
| 294 |
field = cast(FieldNode, operation.selection_set.selections[0]) |
| 295 |
|
| 296 |
assert resolved_infos[0] == GraphQLResolveInfo( |
| 297 |
field_name="test", |
| 298 |
field_nodes=[field], |
| 299 |
return_type=GraphQLString, |
| 300 |
parent_type=cast(GraphQLObjectType, schema.query_type), |
| 301 |
path=ResponsePath(None, "result", "Test"), |
| 302 |
schema=schema, |
| 303 |
fragments={}, |
| 304 |
root_value=root_value, |
| 305 |
operation=operation, |
| 306 |
variable_values=variable_values, |
| 307 |
context=None, |
| 308 |
is_awaitable=resolved_infos[0].is_awaitable, |
| 309 |
) |
| 310 |
|
| 311 |
def it_populates_path_correctly_with_complex_types(): |
| 312 |
path: Optional[ResponsePath] = None |
| 313 |
|
| 314 |
def resolve(_val, info): |
| 315 |
nonlocal path |
| 316 |
path = info.path |
| 317 |
|
| 318 |
def resolve_type(_val, _info, _type): |
| 319 |
return "SomeObject" |
| 320 |
|
| 321 |
some_object = GraphQLObjectType( |
| 322 |
"SomeObject", {"test": GraphQLField(GraphQLString, resolve=resolve)} |
| 323 |
) |
| 324 |
some_union = GraphQLUnionType( |
| 325 |
"SomeUnion", [some_object], resolve_type=resolve_type |
| 326 |
) |
| 327 |
test_type = GraphQLObjectType( |
| 328 |
"SomeQuery", |
| 329 |
{ |
| 330 |
"test": GraphQLField( |
| 331 |
GraphQLNonNull(GraphQLList(GraphQLNonNull(some_union))) |
| 332 |
) |
| 333 |
}, |
| 334 |
) |
| 335 |
schema = GraphQLSchema(test_type) |
| 336 |
root_value: Any = {"test": [{}]} |
| 337 |
document = parse(""" |
| 338 |
query { |
| 339 |
l1: test { |
| 340 |
... on SomeObject { |
| 341 |
l2: test |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
""") |
| 346 |
|
| 347 |
execute_sync(schema, document, root_value) |
| 348 |
|
| 349 |
assert path is not None |
| 350 |
prev, key, typename = path |
| 351 |
assert key == "l2" |
| 352 |
assert typename == "SomeObject" |
| 353 |
prev, key, typename = prev |
| 354 |
assert key == 0 |
| 355 |
assert typename is None |
| 356 |
prev, key, typename = prev |
| 357 |
assert key == "l1" |
| 358 |
assert typename == "SomeQuery" |
| 359 |
assert prev is None |
| 360 |
|
| 361 |
def threads_root_value_context_correctly(): |
| 362 |
resolved_values = [] |
| 363 |
|
| 364 |
class Data: |
| 365 |
context_thing = "thing" |
| 366 |
|
| 367 |
def resolve(obj, _info): |
| 368 |
resolved_values.append(obj) |
| 369 |
|
| 370 |
schema = GraphQLSchema( |
| 371 |
GraphQLObjectType( |
| 372 |
"Type", {"a": GraphQLField(GraphQLString, resolve=resolve)} |
| 373 |
) |
| 374 |
) |
| 375 |
|
| 376 |
document = parse("query Example { a }") |
| 377 |
root_value = Data() |
| 378 |
execute_sync(schema, document, root_value) |
| 379 |
|
| 380 |
assert len(resolved_values) == 1 |
| 381 |
assert resolved_values[0] is root_value |
| 382 |
|
| 383 |
def correctly_threads_arguments(): |
| 384 |
resolved_args = [] |
| 385 |
|
| 386 |
def resolve(_obj, _info, **args): |
| 387 |
resolved_args.append(args) |
| 388 |
|
| 389 |
schema = GraphQLSchema( |
| 390 |
GraphQLObjectType( |
| 391 |
"Type", |
| 392 |
{ |
| 393 |
"b": GraphQLField( |
| 394 |
GraphQLString, |
| 395 |
args={ |
| 396 |
"numArg": GraphQLArgument(GraphQLInt), |
| 397 |
"stringArg": GraphQLArgument(GraphQLString), |
| 398 |
}, |
| 399 |
resolve=resolve, |
| 400 |
) |
| 401 |
}, |
| 402 |
) |
| 403 |
) |
| 404 |
|
| 405 |
document = parse(""" |
| 406 |
query Example { |
| 407 |
b(numArg: 123, stringArg: "foo") |
| 408 |
} |
| 409 |
""") |
| 410 |
|
| 411 |
execute_sync(schema, document) |
| 412 |
|
| 413 |
assert len(resolved_args) == 1 |
| 414 |
assert resolved_args[0] == {"numArg": 123, "stringArg": "foo"} |
| 415 |
|
| 416 |
@mark.asyncio |
| 417 |
async def nulls_out_error_subtrees(): |
| 418 |
document = parse(""" |
| 419 |
{ |
| 420 |
syncOk |
| 421 |
syncError |
| 422 |
syncRawError |
| 423 |
syncReturnError |
| 424 |
syncReturnErrorList |
| 425 |
asyncOk |
| 426 |
asyncError |
| 427 |
asyncRawError |
| 428 |
asyncReturnError |
| 429 |
asyncReturnErrorWithExtensions |
| 430 |
} |
| 431 |
""") |
| 432 |
|
| 433 |
schema = GraphQLSchema( |
| 434 |
GraphQLObjectType( |
| 435 |
"Type", |
| 436 |
{ |
| 437 |
"syncOk": GraphQLField(GraphQLString), |
| 438 |
"syncError": GraphQLField(GraphQLString), |
| 439 |
"syncRawError": GraphQLField(GraphQLString), |
| 440 |
"syncReturnError": GraphQLField(GraphQLString), |
| 441 |
"syncReturnErrorList": GraphQLField(GraphQLList(GraphQLString)), |
| 442 |
"asyncOk": GraphQLField(GraphQLString), |
| 443 |
"asyncError": GraphQLField(GraphQLString), |
| 444 |
"asyncErrorWithExtensions": GraphQLField(GraphQLString), |
| 445 |
"asyncRawError": GraphQLField(GraphQLString), |
| 446 |
"asyncReturnError": GraphQLField(GraphQLString), |
| 447 |
"asyncReturnErrorWithExtensions": GraphQLField(GraphQLString), |
| 448 |
}, |
| 449 |
) |
| 450 |
) |
| 451 |
|
| 452 |
# noinspection PyPep8Naming,PyMethodMayBeStatic |
| 453 |
class Data: |
| 454 |
def syncOk(self, _info): |
| 455 |
return "sync ok" |
| 456 |
|
| 457 |
def syncError(self, _info): |
| 458 |
raise GraphQLError("Error getting syncError") |
| 459 |
|
| 460 |
def syncRawError(self, _info): |
| 461 |
raise Exception("Error getting syncRawError") |
| 462 |
|
| 463 |
def syncReturnError(self, _info): |
| 464 |
return Exception("Error getting syncReturnError") |
| 465 |
|
| 466 |
def syncReturnErrorList(self, _info): |
| 467 |
return [ |
| 468 |
"sync0", |
| 469 |
Exception("Error getting syncReturnErrorList1"), |
| 470 |
"sync2", |
| 471 |
Exception("Error getting syncReturnErrorList3"), |
| 472 |
] |
| 473 |
|
| 474 |
async def asyncOk(self, _info): |
| 475 |
return "async ok" |
| 476 |
|
| 477 |
async def asyncError(self, _info): |
| 478 |
raise GraphQLError("Error getting asyncError") |
| 479 |
|
| 480 |
async def asyncRawError(self, _info): |
| 481 |
raise Exception("Error getting asyncRawError") |
| 482 |
|
| 483 |
async def asyncReturnError(self, _info): |
| 484 |
return GraphQLError("Error getting asyncReturnError") |
| 485 |
|
| 486 |
async def asyncReturnErrorWithExtensions(self, _info): |
| 487 |
return GraphQLError( |
| 488 |
"Error getting asyncReturnErrorWithExtensions", |
| 489 |
extensions={"foo": "bar"}, |
| 490 |
) |
| 491 |
|
| 492 |
awaitable_result = execute(schema, document, Data()) |
| 493 |
assert isinstance(awaitable_result, Awaitable) |
| 494 |
result = await awaitable_result |
| 495 |
|
| 496 |
assert result == ( |
| 497 |
{ |
| 498 |
"syncOk": "sync ok", |
| 499 |
"syncError": None, |
| 500 |
"syncRawError": None, |
| 501 |
"syncReturnError": None, |
| 502 |
"syncReturnErrorList": ["sync0", None, "sync2", None], |
| 503 |
"asyncOk": "async ok", |
| 504 |
"asyncError": None, |
| 505 |
"asyncRawError": None, |
| 506 |
"asyncReturnError": None, |
| 507 |
"asyncReturnErrorWithExtensions": None, |
| 508 |
}, |
| 509 |
[ |
| 510 |
{ |
| 511 |
"message": "Error getting syncError", |
| 512 |
"locations": [(4, 15)], |
| 513 |
"path": ["syncError"], |
| 514 |
}, |
| 515 |
{ |
| 516 |
"message": "Error getting syncRawError", |
| 517 |
"locations": [(5, 15)], |
| 518 |
"path": ["syncRawError"], |
| 519 |
}, |
| 520 |
{ |
| 521 |
"message": "Error getting syncReturnError", |
| 522 |
"locations": [(6, 15)], |
| 523 |
"path": ["syncReturnError"], |
| 524 |
}, |
| 525 |
{ |
| 526 |
"message": "Error getting syncReturnErrorList1", |
| 527 |
"locations": [(7, 15)], |
| 528 |
"path": ["syncReturnErrorList", 1], |
| 529 |
}, |
| 530 |
{ |
| 531 |
"message": "Error getting syncReturnErrorList3", |
| 532 |
"locations": [(7, 15)], |
| 533 |
"path": ["syncReturnErrorList", 3], |
| 534 |
}, |
| 535 |
{ |
| 536 |
"message": "Error getting asyncError", |
| 537 |
"locations": [(9, 15)], |
| 538 |
"path": ["asyncError"], |
| 539 |
}, |
| 540 |
{ |
| 541 |
"message": "Error getting asyncRawError", |
| 542 |
"locations": [(10, 15)], |
| 543 |
"path": ["asyncRawError"], |
| 544 |
}, |
| 545 |
{ |
| 546 |
"message": "Error getting asyncReturnError", |
| 547 |
"locations": [(11, 15)], |
| 548 |
"path": ["asyncReturnError"], |
| 549 |
}, |
| 550 |
{ |
| 551 |
"message": "Error getting asyncReturnErrorWithExtensions", |
| 552 |
"locations": [(12, 15)], |
| 553 |
"path": ["asyncReturnErrorWithExtensions"], |
| 554 |
"extensions": {"foo": "bar"}, |
| 555 |
}, |
| 556 |
], |
| 557 |
) |
| 558 |
|
| 559 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 560 |
def handles_sync_errors_combined_with_async_ones(): |
| 561 |
is_async_resolver_finished = False |
| 562 |
|
| 563 |
async def async_resolver(_obj, _info): |
| 564 |
nonlocal is_async_resolver_finished |
| 565 |
is_async_resolver_finished = True # pragma: no cover |
| 566 |
|
| 567 |
schema = GraphQLSchema( |
| 568 |
GraphQLObjectType( |
| 569 |
"Query", |
| 570 |
{ |
| 571 |
"syncNullError": GraphQLField( |
| 572 |
GraphQLNonNull(GraphQLString), resolve=lambda _obj, _info: None |
| 573 |
), |
| 574 |
"asyncNullError": GraphQLField( |
| 575 |
GraphQLNonNull(GraphQLString), resolve=async_resolver |
| 576 |
), |
| 577 |
}, |
| 578 |
) |
| 579 |
) |
| 580 |
|
| 581 |
document = parse(""" |
| 582 |
{ |
| 583 |
asyncNullError |
| 584 |
syncNullError |
| 585 |
} |
| 586 |
""") |
| 587 |
|
| 588 |
result = execute(schema, document) |
| 589 |
|
| 590 |
assert is_async_resolver_finished is False |
| 591 |
|
| 592 |
assert result == ( |
| 593 |
None, |
| 594 |
[ |
| 595 |
{ |
| 596 |
"message": "Cannot return null" |
| 597 |
" for non-nullable field Query.syncNullError.", |
| 598 |
"locations": [(4, 15)], |
| 599 |
"path": ["syncNullError"], |
| 600 |
} |
| 601 |
], |
| 602 |
) |
| 603 |
|
| 604 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 605 |
def full_response_path_is_included_for_non_nullable_fields(): |
| 606 |
def resolve_ok(*_args): |
| 607 |
return {} |
| 608 |
|
| 609 |
def resolve_error(*_args): |
| 610 |
raise Exception("Catch me if you can") |
| 611 |
|
| 612 |
A = GraphQLObjectType( |
| 613 |
"A", |
| 614 |
lambda: { |
| 615 |
"nullableA": GraphQLField(A, resolve=resolve_ok), |
| 616 |
"nonNullA": GraphQLField(GraphQLNonNull(A), resolve=resolve_ok), |
| 617 |
"throws": GraphQLField(GraphQLNonNull(A), resolve=resolve_error), |
| 618 |
}, |
| 619 |
) |
| 620 |
|
| 621 |
schema = GraphQLSchema( |
| 622 |
GraphQLObjectType( |
| 623 |
"query", lambda: {"nullableA": GraphQLField(A, resolve=resolve_ok)} |
| 624 |
) |
| 625 |
) |
| 626 |
|
| 627 |
document = parse(""" |
| 628 |
query { |
| 629 |
nullableA { |
| 630 |
aliasedA: nullableA { |
| 631 |
nonNullA { |
| 632 |
anotherA: nonNullA { |
| 633 |
throws |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
""") |
| 640 |
|
| 641 |
assert execute_sync(schema, document) == ( |
| 642 |
{"nullableA": {"aliasedA": None}}, |
| 643 |
[ |
| 644 |
{ |
| 645 |
"message": "Catch me if you can", |
| 646 |
"locations": [(7, 23)], |
| 647 |
"path": ["nullableA", "aliasedA", "nonNullA", "anotherA", "throws"], |
| 648 |
} |
| 649 |
], |
| 650 |
) |
| 651 |
|
| 652 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 653 |
def uses_the_inline_operation_if_no_operation_name_is_provided(): |
| 654 |
schema = GraphQLSchema( |
| 655 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 656 |
) |
| 657 |
|
| 658 |
document = parse("{ a }") |
| 659 |
|
| 660 |
class Data: |
| 661 |
a = "b" |
| 662 |
|
| 663 |
result = execute_sync(schema, document, Data()) |
| 664 |
assert result == ({"a": "b"}, None) |
| 665 |
|
| 666 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 667 |
def uses_the_only_operation_if_no_operation_name_is_provided(): |
| 668 |
schema = GraphQLSchema( |
| 669 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 670 |
) |
| 671 |
|
| 672 |
document = parse("query Example { a }") |
| 673 |
|
| 674 |
class Data: |
| 675 |
a = "b" |
| 676 |
|
| 677 |
result = execute_sync(schema, document, Data()) |
| 678 |
assert result == ({"a": "b"}, None) |
| 679 |
|
| 680 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 681 |
def uses_the_named_operation_if_operation_name_is_provided(): |
| 682 |
schema = GraphQLSchema( |
| 683 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 684 |
) |
| 685 |
|
| 686 |
document = parse(""" |
| 687 |
query Example { first: a } |
| 688 |
query OtherExample { second: a } |
| 689 |
""") |
| 690 |
|
| 691 |
class Data: |
| 692 |
a = "b" |
| 693 |
|
| 694 |
result = execute_sync(schema, document, Data(), operation_name="OtherExample") |
| 695 |
assert result == ({"second": "b"}, None) |
| 696 |
|
| 697 |
def provides_error_if_no_operation_is_provided(): |
| 698 |
schema = GraphQLSchema( |
| 699 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 700 |
) |
| 701 |
|
| 702 |
document = parse("fragment Example on Type { a }") |
| 703 |
|
| 704 |
class Data: |
| 705 |
a = "b" |
| 706 |
|
| 707 |
result = execute_sync(schema, document, Data()) |
| 708 |
assert result == (None, [{"message": "Must provide an operation."}]) |
| 709 |
|
| 710 |
def errors_if_no_operation_name_is_provided_with_multiple_operations(): |
| 711 |
schema = GraphQLSchema( |
| 712 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 713 |
) |
| 714 |
|
| 715 |
document = parse(""" |
| 716 |
query Example { a } |
| 717 |
query OtherExample { a } |
| 718 |
""") |
| 719 |
|
| 720 |
result = execute_sync(schema, document) |
| 721 |
assert result == ( |
| 722 |
None, |
| 723 |
[ |
| 724 |
{ |
| 725 |
"message": "Must provide operation name if query contains" |
| 726 |
" multiple operations." |
| 727 |
} |
| 728 |
], |
| 729 |
) |
| 730 |
|
| 731 |
def errors_if_unknown_operation_name_is_provided(): |
| 732 |
schema = GraphQLSchema( |
| 733 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 734 |
) |
| 735 |
|
| 736 |
document = parse(""" |
| 737 |
query Example { a } |
| 738 |
query OtherExample { a } |
| 739 |
""") |
| 740 |
|
| 741 |
result = execute_sync(schema, document, operation_name="UnknownExample") |
| 742 |
assert result == ( |
| 743 |
None, |
| 744 |
[{"message": "Unknown operation named 'UnknownExample'."}], |
| 745 |
) |
| 746 |
|
| 747 |
def errors_if_empty_string_is_provided_as_operation_name(): |
| 748 |
schema = GraphQLSchema( |
| 749 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 750 |
) |
| 751 |
|
| 752 |
document = parse("{ a }") |
| 753 |
|
| 754 |
result = execute_sync(schema, document, operation_name="") |
| 755 |
assert result == ( |
| 756 |
None, |
| 757 |
[{"message": "Unknown operation named ''."}], |
| 758 |
) |
| 759 |
|
| 760 |
def uses_the_query_schema_for_queries(): |
| 761 |
schema = GraphQLSchema( |
| 762 |
GraphQLObjectType("Q", {"a": GraphQLField(GraphQLString)}), |
| 763 |
GraphQLObjectType("M", {"c": GraphQLField(GraphQLString)}), |
| 764 |
GraphQLObjectType("S", {"a": GraphQLField(GraphQLString)}), |
| 765 |
) |
| 766 |
|
| 767 |
document = parse(""" |
| 768 |
query Q { a } |
| 769 |
mutation M { c } |
| 770 |
subscription S { a } |
| 771 |
""") |
| 772 |
|
| 773 |
class Data: |
| 774 |
a = "b" |
| 775 |
c = "d" |
| 776 |
|
| 777 |
result = execute_sync(schema, document, Data(), operation_name="Q") |
| 778 |
assert result == ({"a": "b"}, None) |
| 779 |
|
| 780 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 781 |
def uses_the_mutation_schema_for_mutations(): |
| 782 |
schema = GraphQLSchema( |
| 783 |
GraphQLObjectType("Q", {"a": GraphQLField(GraphQLString)}), |
| 784 |
GraphQLObjectType("M", {"c": GraphQLField(GraphQLString)}), |
| 785 |
) |
| 786 |
|
| 787 |
document = parse(""" |
| 788 |
query Q { a } |
| 789 |
mutation M { c } |
| 790 |
""") |
| 791 |
|
| 792 |
class Data: |
| 793 |
a = "b" |
| 794 |
c = "d" |
| 795 |
|
| 796 |
result = execute_sync(schema, document, Data(), operation_name="M") |
| 797 |
assert result == ({"c": "d"}, None) |
| 798 |
|
| 799 |
def uses_the_subscription_schema_for_subscriptions(): |
| 800 |
schema = GraphQLSchema( |
| 801 |
query=GraphQLObjectType("Q", {"a": GraphQLField(GraphQLString)}), |
| 802 |
subscription=GraphQLObjectType("S", {"a": GraphQLField(GraphQLString)}), |
| 803 |
) |
| 804 |
|
| 805 |
document = parse(""" |
| 806 |
query Q { a } |
| 807 |
subscription S { a } |
| 808 |
""") |
| 809 |
|
| 810 |
class Data: |
| 811 |
a = "b" |
| 812 |
c = "d" |
| 813 |
|
| 814 |
result = execute_sync(schema, document, Data(), operation_name="S") |
| 815 |
assert result == ({"a": "b"}, None) |
| 816 |
|
| 817 |
def resolves_to_an_error_if_schema_does_not_support_operation(): |
| 818 |
schema = GraphQLSchema(assume_valid=True) |
| 819 |
|
| 820 |
document = parse(""" |
| 821 |
query Q { __typename } |
| 822 |
mutation M { __typename } |
| 823 |
subscription S { __typename } |
| 824 |
""") |
| 825 |
|
| 826 |
assert execute_sync(schema, document, operation_name="Q") == ( |
| 827 |
None, |
| 828 |
[ |
| 829 |
{ |
| 830 |
"message": "Schema is not configured to execute query operation.", |
| 831 |
"locations": [(2, 13)], |
| 832 |
} |
| 833 |
], |
| 834 |
) |
| 835 |
|
| 836 |
assert execute_sync(schema, document, operation_name="M") == ( |
| 837 |
None, |
| 838 |
[ |
| 839 |
{ |
| 840 |
"message": "Schema is not configured to execute" |
| 841 |
" mutation operation.", |
| 842 |
"locations": [(3, 13)], |
| 843 |
} |
| 844 |
], |
| 845 |
) |
| 846 |
|
| 847 |
assert execute_sync(schema, document, operation_name="S") == ( |
| 848 |
None, |
| 849 |
[ |
| 850 |
{ |
| 851 |
"message": "Schema is not configured to execute" |
| 852 |
" subscription operation.", |
| 853 |
"locations": [(4, 13)], |
| 854 |
} |
| 855 |
], |
| 856 |
) |
| 857 |
|
| 858 |
@mark.asyncio |
| 859 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 860 |
async def correct_field_ordering_despite_execution_order(): |
| 861 |
schema = GraphQLSchema( |
| 862 |
GraphQLObjectType( |
| 863 |
"Type", |
| 864 |
{ |
| 865 |
"a": GraphQLField(GraphQLString), |
| 866 |
"b": GraphQLField(GraphQLString), |
| 867 |
"c": GraphQLField(GraphQLString), |
| 868 |
"d": GraphQLField(GraphQLString), |
| 869 |
"e": GraphQLField(GraphQLString), |
| 870 |
}, |
| 871 |
) |
| 872 |
) |
| 873 |
|
| 874 |
document = parse("{ a, b, c, d, e}") |
| 875 |
|
| 876 |
# noinspection PyMethodMayBeStatic,PyMethodMayBeStatic |
| 877 |
class Data: |
| 878 |
def a(self, _info): |
| 879 |
return "a" |
| 880 |
|
| 881 |
async def b(self, _info): |
| 882 |
return "b" |
| 883 |
|
| 884 |
def c(self, _info): |
| 885 |
return "c" |
| 886 |
|
| 887 |
async def d(self, _info): |
| 888 |
return "d" |
| 889 |
|
| 890 |
def e(self, _info): |
| 891 |
return "e" |
| 892 |
|
| 893 |
awaitable_result = execute(schema, document, Data()) |
| 894 |
assert isinstance(awaitable_result, Awaitable) |
| 895 |
result = await awaitable_result |
| 896 |
|
| 897 |
assert result == ({"a": "a", "b": "b", "c": "c", "d": "d", "e": "e"}, None) |
| 898 |
|
| 899 |
def avoids_recursion(): |
| 900 |
schema = GraphQLSchema( |
| 901 |
GraphQLObjectType("Type", {"a": GraphQLField(GraphQLString)}) |
| 902 |
) |
| 903 |
|
| 904 |
document = parse(""" |
| 905 |
query Q { |
| 906 |
a |
| 907 |
...Frag |
| 908 |
...Frag |
| 909 |
} |
| 910 |
|
| 911 |
fragment Frag on Type { |
| 912 |
a, |
| 913 |
...Frag |
| 914 |
} |
| 915 |
""") |
| 916 |
|
| 917 |
class Data: |
| 918 |
a = "b" |
| 919 |
|
| 920 |
result = execute_sync(schema, document, Data(), operation_name="Q") |
| 921 |
|
| 922 |
assert result == ({"a": "b"}, None) |
| 923 |
|
| 924 |
def ignores_missing_sub_selections_on_fields(): |
| 925 |
some_type = GraphQLObjectType("SomeType", {"b": GraphQLField(GraphQLString)}) |
| 926 |
schema = GraphQLSchema( |
| 927 |
GraphQLObjectType("Query", {"a": GraphQLField(some_type)}) |
| 928 |
) |
| 929 |
document = parse("{ a }") |
| 930 |
root_value = {"a": {"b": "c"}} |
| 931 |
|
| 932 |
result = execute_sync(schema, document, root_value) |
| 933 |
assert result == ({"a": {}}, None) |
| 934 |
|
| 935 |
def does_not_include_illegal_fields_in_output(): |
| 936 |
schema = GraphQLSchema( |
| 937 |
GraphQLObjectType("Q", {"a": GraphQLField(GraphQLString)}) |
| 938 |
) |
| 939 |
|
| 940 |
document = parse("{ thisIsIllegalDoNotIncludeMe }") |
| 941 |
|
| 942 |
result = execute_sync(schema, document) |
| 943 |
|
| 944 |
assert result == ({}, None) |
| 945 |
|
| 946 |
def does_not_include_arguments_that_were_not_set(): |
| 947 |
schema = GraphQLSchema( |
| 948 |
GraphQLObjectType( |
| 949 |
"Type", |
| 950 |
{ |
| 951 |
"field": GraphQLField( |
| 952 |
GraphQLString, |
| 953 |
args={ |
| 954 |
"a": GraphQLArgument(GraphQLBoolean), |
| 955 |
"b": GraphQLArgument(GraphQLBoolean), |
| 956 |
"c": GraphQLArgument(GraphQLBoolean), |
| 957 |
"d": GraphQLArgument(GraphQLInt), |
| 958 |
"e": GraphQLArgument(GraphQLInt), |
| 959 |
}, |
| 960 |
resolve=lambda _source, _info, **args: inspect(args), |
| 961 |
) |
| 962 |
}, |
| 963 |
) |
| 964 |
) |
| 965 |
|
| 966 |
document = parse("{ field(a: true, c: false, e: 0) }") |
| 967 |
|
| 968 |
assert execute_sync(schema, document) == ( |
| 969 |
{"field": "{'a': True, 'c': False, 'e': 0}"}, |
| 970 |
None, |
| 971 |
) |
| 972 |
|
| 973 |
@mark.asyncio |
| 974 |
async def fails_when_is_type_of_check_is_not_met(): |
| 975 |
class Special: |
| 976 |
value: str |
| 977 |
|
| 978 |
def __init__(self, value): |
| 979 |
self.value = value |
| 980 |
|
| 981 |
class NotSpecial: |
| 982 |
value: str |
| 983 |
|
| 984 |
def __init__(self, value): |
| 985 |
self.value = value |
| 986 |
|
| 987 |
def is_type_of_special(obj, _info): |
| 988 |
is_special = isinstance(obj, Special) |
| 989 |
if not _info.context["async"]: |
| 990 |
return is_special |
| 991 |
|
| 992 |
async def async_is_special(): |
| 993 |
return is_special |
| 994 |
|
| 995 |
return async_is_special() |
| 996 |
|
| 997 |
SpecialType = GraphQLObjectType( |
| 998 |
"SpecialType", |
| 999 |
{"value": GraphQLField(GraphQLString)}, |
| 1000 |
is_type_of=is_type_of_special, |
| 1001 |
) |
| 1002 |
|
| 1003 |
schema = GraphQLSchema( |
| 1004 |
GraphQLObjectType( |
| 1005 |
"Query", {"specials": GraphQLField(GraphQLList(SpecialType))} |
| 1006 |
) |
| 1007 |
) |
| 1008 |
|
| 1009 |
document = parse("{ specials { value } }") |
| 1010 |
root_value = {"specials": [Special("foo"), NotSpecial("bar")]} |
| 1011 |
|
| 1012 |
result = execute_sync(schema, document, root_value, {"async": False}) |
| 1013 |
assert not isinstance(result, Awaitable) |
| 1014 |
assert result == ( |
| 1015 |
{"specials": [{"value": "foo"}, None]}, |
| 1016 |
[ |
| 1017 |
{ |
| 1018 |
"message": "Expected value of type 'SpecialType' but got:" |
| 1019 |
" <NotSpecial instance>.", |
| 1020 |
"locations": [(1, 3)], |
| 1021 |
"path": ["specials", 1], |
| 1022 |
} |
| 1023 |
], |
| 1024 |
) |
| 1025 |
|
| 1026 |
async_result = execute(schema, document, root_value, {"async": True}) |
| 1027 |
assert isinstance(async_result, Awaitable) |
| 1028 |
awaited_result = await async_result |
| 1029 |
assert awaited_result == result |
| 1030 |
|
| 1031 |
def fails_when_serialize_of_custom_scalar_does_not_return_a_value(): |
| 1032 |
custom_scalar = GraphQLScalarType( |
| 1033 |
"CustomScalar", serialize=lambda _value: Undefined # returns nothing |
| 1034 |
) |
| 1035 |
schema = GraphQLSchema( |
| 1036 |
GraphQLObjectType( |
| 1037 |
"Query", |
| 1038 |
{ |
| 1039 |
"customScalar": GraphQLField( |
| 1040 |
custom_scalar, resolve=lambda *_args: "CUSTOM_VALUE" |
| 1041 |
) |
| 1042 |
}, |
| 1043 |
) |
| 1044 |
) |
| 1045 |
|
| 1046 |
result = execute_sync(schema, parse("{ customScalar }")) |
| 1047 |
assert result == ( |
| 1048 |
{"customScalar": None}, |
| 1049 |
[ |
| 1050 |
{ |
| 1051 |
"message": "Expected `CustomScalar.serialize('CUSTOM_VALUE')`" |
| 1052 |
" to return non-nullable value, returned: Undefined", |
| 1053 |
"locations": [(1, 3)], |
| 1054 |
"path": ["customScalar"], |
| 1055 |
} |
| 1056 |
], |
| 1057 |
) |
| 1058 |
|
| 1059 |
def executes_ignoring_invalid_non_executable_definitions(): |
| 1060 |
schema = GraphQLSchema( |
| 1061 |
GraphQLObjectType("Query", {"foo": GraphQLField(GraphQLString)}) |
| 1062 |
) |
| 1063 |
|
| 1064 |
document = parse(""" |
| 1065 |
{ foo } |
| 1066 |
|
| 1067 |
type Query { bar: String } |
| 1068 |
""") |
| 1069 |
|
| 1070 |
result = execute_sync(schema, document) |
| 1071 |
assert result == ({"foo": None}, None) |
| 1072 |
|
| 1073 |
def uses_a_custom_field_resolver(): |
| 1074 |
schema = GraphQLSchema( |
| 1075 |
GraphQLObjectType("Query", {"foo": GraphQLField(GraphQLString)}) |
| 1076 |
) |
| 1077 |
document = parse("{ foo }") |
| 1078 |
|
| 1079 |
def field_resolver(_source, info): |
| 1080 |
# For the purposes of test, just return the name of the field! |
| 1081 |
return info.field_name |
| 1082 |
|
| 1083 |
result = execute_sync(schema, document, field_resolver=field_resolver) |
| 1084 |
assert result == ({"foo": "foo"}, None) |
| 1085 |
|
| 1086 |
def uses_a_custom_type_resolver(): |
| 1087 |
document = parse("{ foo { bar } }") |
| 1088 |
|
| 1089 |
foo_interface = GraphQLInterfaceType( |
| 1090 |
"FooInterface", {"bar": GraphQLField(GraphQLString)} |
| 1091 |
) |
| 1092 |
|
| 1093 |
foo_object = GraphQLObjectType( |
| 1094 |
"FooObject", {"bar": GraphQLField(GraphQLString)}, [foo_interface] |
| 1095 |
) |
| 1096 |
|
| 1097 |
schema = GraphQLSchema( |
| 1098 |
GraphQLObjectType("Query", {"foo": GraphQLField(foo_interface)}), |
| 1099 |
types=[foo_object], |
| 1100 |
) |
| 1101 |
|
| 1102 |
possible_types = None |
| 1103 |
|
| 1104 |
def type_resolver(_source, info, abstract_type): |
| 1105 |
# Resolver should be able to figure out all possible types on its own |
| 1106 |
nonlocal possible_types |
| 1107 |
possible_types = info.schema.get_possible_types(abstract_type) |
| 1108 |
return "FooObject" |
| 1109 |
|
| 1110 |
root_value = {"foo": {"bar": "bar"}} |
| 1111 |
result = execute_sync(schema, document, root_value, type_resolver=type_resolver) |
| 1112 |
|
| 1113 |
assert result == ({"foo": {"bar": "bar"}}, None) |
| 1114 |
assert possible_types == [foo_object] |
| 1115 |
|
| 1116 |
def uses_a_different_number_of_max_coercion_errors(): |
| 1117 |
schema = GraphQLSchema( |
| 1118 |
query=GraphQLObjectType("Query", {"dummy": GraphQLField(GraphQLString)}), |
| 1119 |
mutation=GraphQLObjectType( |
| 1120 |
"Mutation", |
| 1121 |
{ |
| 1122 |
"updateUser": GraphQLField( |
| 1123 |
GraphQLString, |
| 1124 |
args={ |
| 1125 |
"data": GraphQLArgument( |
| 1126 |
GraphQLInputObjectType( |
| 1127 |
"User", |
| 1128 |
{ |
| 1129 |
"email": GraphQLInputField( |
| 1130 |
GraphQLNonNull(GraphQLString) |
| 1131 |
) |
| 1132 |
}, |
| 1133 |
) |
| 1134 |
) |
| 1135 |
}, |
| 1136 |
) |
| 1137 |
}, |
| 1138 |
), |
| 1139 |
) |
| 1140 |
|
| 1141 |
document = parse(""" |
| 1142 |
mutation ($data: User) { |
| 1143 |
updateUser(data: $data) |
| 1144 |
} |
| 1145 |
""") |
| 1146 |
|
| 1147 |
result = execute_sync( |
| 1148 |
schema, |
| 1149 |
document, |
| 1150 |
variable_values={ |
| 1151 |
"data": { |
| 1152 |
"email": "", |
| 1153 |
"wrongArg": "wrong", |
| 1154 |
"wrongArg2": "wrong", |
| 1155 |
"wrongArg3": "wrong", |
| 1156 |
} |
| 1157 |
}, |
| 1158 |
max_coercion_errors=1, |
| 1159 |
) |
| 1160 |
|
| 1161 |
# Returns at least 2 errors, one for the first 'wrongArg', and one for the |
| 1162 |
# coercion limit |
| 1163 |
assert result == ( |
| 1164 |
None, |
| 1165 |
[ |
| 1166 |
{ |
| 1167 |
"message": "Variable '$data' got invalid value" |
| 1168 |
" {'email': '', 'wrongArg': 'wrong'," |
| 1169 |
" 'wrongArg2': 'wrong', 'wrongArg3': 'wrong'};" |
| 1170 |
" Field 'wrongArg' is not defined by type 'User'.", |
| 1171 |
"locations": [(2, 23)], |
| 1172 |
}, |
| 1173 |
{ |
| 1174 |
"message": "Too many errors processing variables," |
| 1175 |
" error limit reached. Execution aborted.", |
| 1176 |
}, |
| 1177 |
], |
| 1178 |
) |