| 1 |
from inspect import isawaitable |
| 2 |
from typing import Any, Callable, NamedTuple, Optional |
| 3 |
|
| 4 |
from pytest import mark |
| 5 |
|
| 6 |
from graphql.execution import ExecutionResult, execute, execute_sync |
| 7 |
from graphql.language import parse |
| 8 |
from graphql.type import ( |
| 9 |
GraphQLBoolean, |
| 10 |
GraphQLField, |
| 11 |
GraphQLInterfaceType, |
| 12 |
GraphQLList, |
| 13 |
GraphQLObjectType, |
| 14 |
GraphQLSchema, |
| 15 |
GraphQLString, |
| 16 |
GraphQLUnionType, |
| 17 |
) |
| 18 |
from graphql.utilities import build_schema |
| 19 |
|
| 20 |
|
| 21 |
def sync_and_async(spec): |
| 22 |
"""Decorator for running a test synchronously and asynchronously.""" |
| 23 |
return mark.asyncio( |
| 24 |
mark.parametrize("sync", (True, False), ids=("sync", "async"))(spec) |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def access_variants(spec): |
| 29 |
"""Decorator for tests with dict and object access, including inheritance.""" |
| 30 |
return mark.asyncio( |
| 31 |
mark.parametrize("access", ("dict", "object", "inheritance"))(spec) |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
async def execute_query( |
| 36 |
sync: bool, schema: GraphQLSchema, query: str, root_value: Any = None |
| 37 |
) -> ExecutionResult: |
| 38 |
"""Execute the query against the given schema synchronously or asynchronously.""" |
| 39 |
assert isinstance(sync, bool) |
| 40 |
assert isinstance(schema, GraphQLSchema) |
| 41 |
assert isinstance(query, str) |
| 42 |
document = parse(query) |
| 43 |
result = (execute_sync if sync else execute)(schema, document, root_value) |
| 44 |
if not sync and isawaitable(result): |
| 45 |
result = await result |
| 46 |
assert isinstance(result, ExecutionResult) |
| 47 |
return result |
| 48 |
|
| 49 |
|
| 50 |
def get_is_type_of(type_, sync=True): |
| 51 |
"""Get a sync or async is_type_of function for the given type.""" |
| 52 |
is_type_of: Callable[[Any, Any], Any] |
| 53 |
if sync: |
| 54 |
|
| 55 |
def is_type_of(obj, _info): |
| 56 |
return isinstance(obj, type_) |
| 57 |
|
| 58 |
else: |
| 59 |
|
| 60 |
async def is_type_of(obj, _info): |
| 61 |
return isinstance(obj, type_) |
| 62 |
|
| 63 |
return is_type_of |
| 64 |
|
| 65 |
|
| 66 |
def get_type_error(sync=True): |
| 67 |
"""Get a sync or async is_type_of or type resolver function that raises an error.""" |
| 68 |
error = RuntimeError("We are testing this error") |
| 69 |
type_error: Callable[..., Any] |
| 70 |
if sync: |
| 71 |
|
| 72 |
def type_error(*_args): |
| 73 |
raise error |
| 74 |
|
| 75 |
else: |
| 76 |
|
| 77 |
async def type_error(*_args): |
| 78 |
raise error |
| 79 |
|
| 80 |
return type_error |
| 81 |
|
| 82 |
|
| 83 |
class Dog(NamedTuple): |
| 84 |
|
| 85 |
name: str |
| 86 |
woofs: bool |
| 87 |
|
| 88 |
|
| 89 |
class Cat(NamedTuple): |
| 90 |
|
| 91 |
name: str |
| 92 |
meows: bool |
| 93 |
|
| 94 |
|
| 95 |
def describe_execute_handles_synchronous_execution_of_abstract_types(): |
| 96 |
@sync_and_async |
| 97 |
async def is_type_of_used_to_resolve_runtime_type_for_interface(sync): |
| 98 |
pet_type = GraphQLInterfaceType("Pet", {"name": GraphQLField(GraphQLString)}) |
| 99 |
|
| 100 |
dog_type = GraphQLObjectType( |
| 101 |
"Dog", |
| 102 |
{ |
| 103 |
"name": GraphQLField(GraphQLString), |
| 104 |
"woofs": GraphQLField(GraphQLBoolean), |
| 105 |
}, |
| 106 |
interfaces=[pet_type], |
| 107 |
is_type_of=get_is_type_of(Dog, sync), |
| 108 |
) |
| 109 |
|
| 110 |
cat_type = GraphQLObjectType( |
| 111 |
"Cat", |
| 112 |
{ |
| 113 |
"name": GraphQLField(GraphQLString), |
| 114 |
"meows": GraphQLField(GraphQLBoolean), |
| 115 |
}, |
| 116 |
interfaces=[pet_type], |
| 117 |
is_type_of=get_is_type_of(Cat, sync), |
| 118 |
) |
| 119 |
|
| 120 |
schema = GraphQLSchema( |
| 121 |
GraphQLObjectType( |
| 122 |
"Query", |
| 123 |
{ |
| 124 |
"pets": GraphQLField( |
| 125 |
GraphQLList(pet_type), |
| 126 |
resolve=lambda *_args: [ |
| 127 |
Dog("Odie", True), |
| 128 |
Cat("Garfield", False), |
| 129 |
], |
| 130 |
) |
| 131 |
}, |
| 132 |
), |
| 133 |
types=[cat_type, dog_type], |
| 134 |
) |
| 135 |
|
| 136 |
query = """ |
| 137 |
{ |
| 138 |
pets { |
| 139 |
name |
| 140 |
... on Dog { |
| 141 |
woofs |
| 142 |
} |
| 143 |
... on Cat { |
| 144 |
meows |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
""" |
| 149 |
|
| 150 |
assert await execute_query(sync, schema, query) == ( |
| 151 |
{ |
| 152 |
"pets": [ |
| 153 |
{"name": "Odie", "woofs": True}, |
| 154 |
{"name": "Garfield", "meows": False}, |
| 155 |
] |
| 156 |
}, |
| 157 |
None, |
| 158 |
) |
| 159 |
|
| 160 |
@sync_and_async |
| 161 |
async def is_type_of_can_throw(sync): |
| 162 |
pet_type = GraphQLInterfaceType("Pet", {"name": GraphQLField(GraphQLString)}) |
| 163 |
|
| 164 |
dog_type = GraphQLObjectType( |
| 165 |
"Dog", |
| 166 |
{ |
| 167 |
"name": GraphQLField(GraphQLString), |
| 168 |
"woofs": GraphQLField(GraphQLBoolean), |
| 169 |
}, |
| 170 |
interfaces=[pet_type], |
| 171 |
is_type_of=get_type_error(sync), |
| 172 |
) |
| 173 |
|
| 174 |
cat_type = GraphQLObjectType( |
| 175 |
"Cat", |
| 176 |
{ |
| 177 |
"name": GraphQLField(GraphQLString), |
| 178 |
"meows": GraphQLField(GraphQLBoolean), |
| 179 |
}, |
| 180 |
interfaces=[pet_type], |
| 181 |
is_type_of=None, |
| 182 |
) |
| 183 |
|
| 184 |
schema = GraphQLSchema( |
| 185 |
GraphQLObjectType( |
| 186 |
"Query", |
| 187 |
{ |
| 188 |
"pets": GraphQLField( |
| 189 |
GraphQLList(pet_type), |
| 190 |
resolve=lambda *_args: [ |
| 191 |
Dog("Odie", True), |
| 192 |
Cat("Garfield", False), |
| 193 |
], |
| 194 |
) |
| 195 |
}, |
| 196 |
), |
| 197 |
types=[dog_type, cat_type], |
| 198 |
) |
| 199 |
|
| 200 |
query = """ |
| 201 |
{ |
| 202 |
pets { |
| 203 |
name |
| 204 |
... on Dog { |
| 205 |
woofs |
| 206 |
} |
| 207 |
... on Cat { |
| 208 |
meows |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
""" |
| 213 |
|
| 214 |
assert await execute_query(sync, schema, query) == ( |
| 215 |
{"pets": [None, None]}, |
| 216 |
[ |
| 217 |
{ |
| 218 |
"message": "We are testing this error", |
| 219 |
"locations": [(3, 15)], |
| 220 |
"path": ["pets", 0], |
| 221 |
}, |
| 222 |
{ |
| 223 |
"message": "We are testing this error", |
| 224 |
"locations": [(3, 15)], |
| 225 |
"path": ["pets", 1], |
| 226 |
}, |
| 227 |
], |
| 228 |
) |
| 229 |
|
| 230 |
@sync_and_async |
| 231 |
async def is_type_of_with_no_suitable_type(sync): |
| 232 |
# added in GraphQL-core to improve coverage |
| 233 |
pet_type = GraphQLInterfaceType("Pet", {"name": GraphQLField(GraphQLString)}) |
| 234 |
|
| 235 |
dog_type = GraphQLObjectType( |
| 236 |
"Dog", |
| 237 |
{ |
| 238 |
"name": GraphQLField(GraphQLString), |
| 239 |
"woofs": GraphQLField(GraphQLBoolean), |
| 240 |
}, |
| 241 |
interfaces=[pet_type], |
| 242 |
is_type_of=get_is_type_of(Cat, sync), |
| 243 |
) |
| 244 |
|
| 245 |
schema = GraphQLSchema( |
| 246 |
GraphQLObjectType( |
| 247 |
"Query", |
| 248 |
{ |
| 249 |
"pets": GraphQLField( |
| 250 |
GraphQLList(pet_type), |
| 251 |
resolve=lambda *_args: [Dog("Odie", True)], |
| 252 |
) |
| 253 |
}, |
| 254 |
), |
| 255 |
types=[dog_type], |
| 256 |
) |
| 257 |
|
| 258 |
query = """ |
| 259 |
{ |
| 260 |
pets { |
| 261 |
name |
| 262 |
... on Dog { |
| 263 |
woofs |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
""" |
| 268 |
|
| 269 |
message = ( |
| 270 |
"Abstract type 'Pet' must resolve to an Object type at runtime" |
| 271 |
" for field 'Query.pets'." |
| 272 |
" Either the 'Pet' type should provide a 'resolve_type' function" |
| 273 |
" or each possible type should provide an 'is_type_of' function." |
| 274 |
) |
| 275 |
assert await execute_query(sync, schema, query) == ( |
| 276 |
{"pets": [None]}, |
| 277 |
[{"message": message, "locations": [(3, 15)], "path": ["pets", 0]}], |
| 278 |
) |
| 279 |
|
| 280 |
@sync_and_async |
| 281 |
async def is_type_of_used_to_resolve_runtime_type_for_union(sync): |
| 282 |
dog_type = GraphQLObjectType( |
| 283 |
"Dog", |
| 284 |
{ |
| 285 |
"name": GraphQLField(GraphQLString), |
| 286 |
"woofs": GraphQLField(GraphQLBoolean), |
| 287 |
}, |
| 288 |
is_type_of=get_is_type_of(Dog, sync), |
| 289 |
) |
| 290 |
|
| 291 |
cat_type = GraphQLObjectType( |
| 292 |
"Cat", |
| 293 |
{ |
| 294 |
"name": GraphQLField(GraphQLString), |
| 295 |
"meows": GraphQLField(GraphQLBoolean), |
| 296 |
}, |
| 297 |
is_type_of=get_is_type_of(Cat, sync), |
| 298 |
) |
| 299 |
|
| 300 |
pet_type = GraphQLUnionType("Pet", [cat_type, dog_type]) |
| 301 |
|
| 302 |
schema = GraphQLSchema( |
| 303 |
GraphQLObjectType( |
| 304 |
"Query", |
| 305 |
{ |
| 306 |
"pets": GraphQLField( |
| 307 |
GraphQLList(pet_type), |
| 308 |
resolve=lambda *_args: [ |
| 309 |
Dog("Odie", True), |
| 310 |
Cat("Garfield", False), |
| 311 |
], |
| 312 |
) |
| 313 |
}, |
| 314 |
) |
| 315 |
) |
| 316 |
|
| 317 |
query = """ |
| 318 |
{ |
| 319 |
pets { |
| 320 |
... on Dog { |
| 321 |
name |
| 322 |
woofs |
| 323 |
} |
| 324 |
... on Cat { |
| 325 |
name |
| 326 |
meows |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
""" |
| 331 |
|
| 332 |
assert await execute_query(sync, schema, query) == ( |
| 333 |
{ |
| 334 |
"pets": [ |
| 335 |
{"name": "Odie", "woofs": True}, |
| 336 |
{"name": "Garfield", "meows": False}, |
| 337 |
] |
| 338 |
}, |
| 339 |
None, |
| 340 |
) |
| 341 |
|
| 342 |
@sync_and_async |
| 343 |
async def resolve_type_can_throw(sync): |
| 344 |
pet_type = GraphQLInterfaceType( |
| 345 |
"Pet", |
| 346 |
{"name": GraphQLField(GraphQLString)}, |
| 347 |
resolve_type=get_type_error(sync), |
| 348 |
) |
| 349 |
|
| 350 |
dog_type = GraphQLObjectType( |
| 351 |
"Dog", |
| 352 |
{ |
| 353 |
"name": GraphQLField(GraphQLString), |
| 354 |
"woofs": GraphQLField(GraphQLBoolean), |
| 355 |
}, |
| 356 |
interfaces=[pet_type], |
| 357 |
) |
| 358 |
|
| 359 |
cat_type = GraphQLObjectType( |
| 360 |
"Cat", |
| 361 |
{ |
| 362 |
"name": GraphQLField(GraphQLString), |
| 363 |
"meows": GraphQLField(GraphQLBoolean), |
| 364 |
}, |
| 365 |
interfaces=[pet_type], |
| 366 |
) |
| 367 |
|
| 368 |
schema = GraphQLSchema( |
| 369 |
GraphQLObjectType( |
| 370 |
"Query", |
| 371 |
{ |
| 372 |
"pets": GraphQLField( |
| 373 |
GraphQLList(pet_type), |
| 374 |
resolve=lambda *_args: [ |
| 375 |
Dog("Odie", True), |
| 376 |
Cat("Garfield", False), |
| 377 |
], |
| 378 |
) |
| 379 |
}, |
| 380 |
), |
| 381 |
types=[dog_type, cat_type], |
| 382 |
) |
| 383 |
|
| 384 |
query = """ |
| 385 |
{ |
| 386 |
pets { |
| 387 |
name |
| 388 |
... on Dog { |
| 389 |
woofs |
| 390 |
} |
| 391 |
... on Cat { |
| 392 |
meows |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
""" |
| 397 |
|
| 398 |
assert await execute_query(sync, schema, query) == ( |
| 399 |
{"pets": [None, None]}, |
| 400 |
[ |
| 401 |
{ |
| 402 |
"message": "We are testing this error", |
| 403 |
"locations": [(3, 15)], |
| 404 |
"path": ["pets", 0], |
| 405 |
}, |
| 406 |
{ |
| 407 |
"message": "We are testing this error", |
| 408 |
"locations": [(3, 15)], |
| 409 |
"path": ["pets", 1], |
| 410 |
}, |
| 411 |
], |
| 412 |
) |
| 413 |
|
| 414 |
def describe_using_typename_on_source_object(): |
| 415 |
|
| 416 |
expected = ( |
| 417 |
{ |
| 418 |
"pets": [ |
| 419 |
{"name": "Odie", "woofs": True}, |
| 420 |
{"name": "Garfield", "meows": False}, |
| 421 |
] |
| 422 |
}, |
| 423 |
None, |
| 424 |
) |
| 425 |
|
| 426 |
# noinspection PyShadowingNames |
| 427 |
def _root_value(access: str) -> Any: |
| 428 |
if access == "dict": |
| 429 |
return { |
| 430 |
"pets": [ |
| 431 |
{"__typename": "Dog", "name": "Odie", "woofs": True}, |
| 432 |
{"__typename": "Cat", "name": "Garfield", "meows": False}, |
| 433 |
], |
| 434 |
} |
| 435 |
|
| 436 |
if access == "object": |
| 437 |
|
| 438 |
class DogObject: |
| 439 |
__typename = "Dog" |
| 440 |
name = "Odie" |
| 441 |
woofs = True |
| 442 |
|
| 443 |
class CatObject: |
| 444 |
__typename = "Cat" |
| 445 |
name = "Garfield" |
| 446 |
meows = False |
| 447 |
|
| 448 |
class RootValueAsObject: |
| 449 |
pets = [DogObject(), CatObject()] |
| 450 |
|
| 451 |
return RootValueAsObject() |
| 452 |
|
| 453 |
if access == "inheritance": |
| 454 |
|
| 455 |
class Pet: |
| 456 |
__typename = "Pet" |
| 457 |
name: Optional[str] = None |
| 458 |
|
| 459 |
class DogPet(Pet): |
| 460 |
__typename = "Dog" |
| 461 |
woofs: Optional[bool] = None |
| 462 |
|
| 463 |
class Odie(DogPet): |
| 464 |
name = "Odie" |
| 465 |
woofs = True |
| 466 |
|
| 467 |
class CatPet(Pet): |
| 468 |
__typename = "Cat" |
| 469 |
meows: Optional[bool] = None |
| 470 |
|
| 471 |
class Tabby(CatPet): |
| 472 |
pass |
| 473 |
|
| 474 |
class Garfield(Tabby): |
| 475 |
name = "Garfield" |
| 476 |
meows = False |
| 477 |
|
| 478 |
class RootValueWithInheritance: |
| 479 |
pets = [Odie(), Garfield()] |
| 480 |
|
| 481 |
return RootValueWithInheritance() |
| 482 |
|
| 483 |
assert False, f"Unknown access variant: {access}" # pragma: no cover |
| 484 |
|
| 485 |
def describe_union_type(): |
| 486 |
|
| 487 |
schema = build_schema(""" |
| 488 |
type Query { |
| 489 |
pets: [Pet] |
| 490 |
} |
| 491 |
|
| 492 |
union Pet = Cat | Dog |
| 493 |
|
| 494 |
type Cat { |
| 495 |
name: String |
| 496 |
meows: Boolean |
| 497 |
} |
| 498 |
|
| 499 |
type Dog { |
| 500 |
name: String |
| 501 |
woofs: Boolean |
| 502 |
} |
| 503 |
""") |
| 504 |
|
| 505 |
query = """ |
| 506 |
{ |
| 507 |
pets { |
| 508 |
name |
| 509 |
... on Dog { |
| 510 |
woofs |
| 511 |
} |
| 512 |
... on Cat { |
| 513 |
meows |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
""" |
| 518 |
|
| 519 |
@sync_and_async |
| 520 |
@access_variants |
| 521 |
async def resolve(sync, access): |
| 522 |
root_value = _root_value(access) |
| 523 |
assert await execute_query(sync, schema, query, root_value) == expected |
| 524 |
|
| 525 |
def describe_interface_type(): |
| 526 |
schema = build_schema(""" |
| 527 |
type Query { |
| 528 |
pets: [Pet] |
| 529 |
} |
| 530 |
|
| 531 |
interface Pet { |
| 532 |
name: String |
| 533 |
} |
| 534 |
|
| 535 |
type Cat implements Pet { |
| 536 |
name: String |
| 537 |
meows: Boolean |
| 538 |
} |
| 539 |
|
| 540 |
type Dog implements Pet { |
| 541 |
name: String |
| 542 |
woofs: Boolean |
| 543 |
} |
| 544 |
""") |
| 545 |
|
| 546 |
query = """ |
| 547 |
{ |
| 548 |
pets { |
| 549 |
name |
| 550 |
... on Dog { |
| 551 |
woofs |
| 552 |
} |
| 553 |
... on Cat { |
| 554 |
meows |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
""" |
| 559 |
|
| 560 |
@sync_and_async |
| 561 |
@access_variants |
| 562 |
async def resolve(sync, access): |
| 563 |
root_value = _root_value(access) |
| 564 |
assert await execute_query(sync, schema, query, root_value) == expected |
| 565 |
|
| 566 |
def resolve_type_on_interface_yields_useful_error(): |
| 567 |
schema = build_schema(""" |
| 568 |
type Query { |
| 569 |
pet: Pet |
| 570 |
} |
| 571 |
|
| 572 |
interface Pet { |
| 573 |
name: String |
| 574 |
} |
| 575 |
|
| 576 |
type Cat implements Pet { |
| 577 |
name: String |
| 578 |
} |
| 579 |
|
| 580 |
type Dog implements Pet { |
| 581 |
name: String |
| 582 |
} |
| 583 |
""") |
| 584 |
|
| 585 |
document = parse(""" |
| 586 |
{ |
| 587 |
pet { |
| 588 |
name |
| 589 |
} |
| 590 |
} |
| 591 |
""") |
| 592 |
|
| 593 |
def expect_error(for_type_name: Any, message: str) -> None: |
| 594 |
root_value = {"pet": {"__typename": for_type_name}} |
| 595 |
result = execute_sync(schema, document, root_value) |
| 596 |
expected = ( |
| 597 |
{"pet": None}, |
| 598 |
[{"message": message, "locations": [(3, 15)], "path": ["pet"]}], |
| 599 |
) |
| 600 |
assert result == expected |
| 601 |
|
| 602 |
expect_error( |
| 603 |
None, |
| 604 |
"Abstract type 'Pet' must resolve" |
| 605 |
" to an Object type at runtime for field 'Query.pet'." |
| 606 |
" Either the 'Pet' type should provide a 'resolve_type' function" |
| 607 |
" or each possible type should provide an 'is_type_of' function.", |
| 608 |
) |
| 609 |
|
| 610 |
expect_error( |
| 611 |
"Human", |
| 612 |
"Abstract type 'Pet' was resolved to a type 'Human'" |
| 613 |
" that does not exist inside the schema.", |
| 614 |
) |
| 615 |
|
| 616 |
expect_error( |
| 617 |
"String", "Abstract type 'Pet' was resolved to a non-object type 'String'." |
| 618 |
) |
| 619 |
|
| 620 |
expect_error( |
| 621 |
"__Schema", |
| 622 |
"Runtime Object type '__Schema' is not a possible type for 'Pet'.", |
| 623 |
) |
| 624 |
|
| 625 |
# workaround since we can't inject resolve_type into SDL |
| 626 |
schema.get_type("Pet").resolve_type = lambda *_args: [] # type: ignore |
| 627 |
|
| 628 |
expect_error( |
| 629 |
None, |
| 630 |
"Abstract type 'Pet' must resolve" |
| 631 |
" to an Object type at runtime for field 'Query.pet'" |
| 632 |
" with value {'__typename': None}, received '[]'.", |
| 633 |
) |