|
17 | 17 | use GraphQL\Type\Definition\Type;
|
18 | 18 | use GraphQL\Type\Definition\UnionType;
|
19 | 19 | use GraphQL\Type\Schema;
|
| 20 | +use GraphQL\Utils\BuildSchema; |
20 | 21 | use PHPUnit\Framework\TestCase;
|
21 | 22 |
|
22 | 23 | /**
|
@@ -373,6 +374,130 @@ public function testResolveTypeOnUnionYieldsUsefulError(): void
|
373 | 374 | self::assertArraySubset($expected, $result);
|
374 | 375 | }
|
375 | 376 |
|
| 377 | + /** @see it('resolve Union type using __typename on source object') */ |
| 378 | + public function testResolveUnionTypeUsingTypenameOnSourceObject(): void |
| 379 | + { |
| 380 | + $schema = BuildSchema::build(' |
| 381 | + type Query { |
| 382 | + pets: [Pet] |
| 383 | + } |
| 384 | + |
| 385 | + union Pet = Cat | Dog |
| 386 | + |
| 387 | + type Cat { |
| 388 | + name: String |
| 389 | + meows: Boolean |
| 390 | + } |
| 391 | + |
| 392 | + type Dog { |
| 393 | + name: String |
| 394 | + woofs: Boolean |
| 395 | + } |
| 396 | + '); |
| 397 | + |
| 398 | + $query = ' |
| 399 | + { |
| 400 | + pets { |
| 401 | + name |
| 402 | + ... on Dog { |
| 403 | + woofs |
| 404 | + } |
| 405 | + ... on Cat { |
| 406 | + meows |
| 407 | + } |
| 408 | + } |
| 409 | + } |
| 410 | + '; |
| 411 | + |
| 412 | + $rootValue = [ |
| 413 | + 'pets' => [ |
| 414 | + [ |
| 415 | + '__typename' => 'Dog', |
| 416 | + 'name' => 'Odie', |
| 417 | + 'woofs' => true, |
| 418 | + ], |
| 419 | + [ |
| 420 | + '__typename' => 'Cat', |
| 421 | + 'name' => 'Garfield', |
| 422 | + 'meows' => false, |
| 423 | + ], |
| 424 | + ], |
| 425 | + ]; |
| 426 | + |
| 427 | + $expected = new ExecutionResult([ |
| 428 | + 'pets' => [ |
| 429 | + ['name' => 'Odie', 'woofs' => true], |
| 430 | + ['name' => 'Garfield', 'meows' => false], |
| 431 | + ], |
| 432 | + ]); |
| 433 | + |
| 434 | + $result = Executor::execute($schema, Parser::parse($query), $rootValue); |
| 435 | + self::assertEquals($expected, $result); |
| 436 | + } |
| 437 | + |
| 438 | + /** @see it('resolve Interface type using __typename on source object') */ |
| 439 | + public function testResolveInterfaceTypeUsingTypenameOnSourceObject(): void |
| 440 | + { |
| 441 | + $schema = BuildSchema::build(' |
| 442 | + type Query { |
| 443 | + pets: [Pet] |
| 444 | + } |
| 445 | + |
| 446 | + interface Pet { |
| 447 | + name: String |
| 448 | + } |
| 449 | + |
| 450 | + type Cat implements Pet { |
| 451 | + name: String |
| 452 | + meows: Boolean |
| 453 | + } |
| 454 | + |
| 455 | + type Dog implements Pet { |
| 456 | + name: String |
| 457 | + woofs: Boolean |
| 458 | + } |
| 459 | + '); |
| 460 | + |
| 461 | + $query = ' |
| 462 | + { |
| 463 | + pets { |
| 464 | + name |
| 465 | + ... on Dog { |
| 466 | + woofs |
| 467 | + } |
| 468 | + ... on Cat { |
| 469 | + meows |
| 470 | + } |
| 471 | + } |
| 472 | + } |
| 473 | + '; |
| 474 | + |
| 475 | + $rootValue = [ |
| 476 | + 'pets' => [ |
| 477 | + [ |
| 478 | + '__typename' => 'Dog', |
| 479 | + 'name' => 'Odie', |
| 480 | + 'woofs' => true, |
| 481 | + ], |
| 482 | + [ |
| 483 | + '__typename' => 'Cat', |
| 484 | + 'name' => 'Garfield', |
| 485 | + 'meows' => false, |
| 486 | + ], |
| 487 | + ], |
| 488 | + ]; |
| 489 | + |
| 490 | + $expected = new ExecutionResult([ |
| 491 | + 'pets' => [ |
| 492 | + ['name' => 'Odie', 'woofs' => true], |
| 493 | + ['name' => 'Garfield', 'meows' => false], |
| 494 | + ], |
| 495 | + ]); |
| 496 | + |
| 497 | + $result = Executor::execute($schema, Parser::parse($query), $rootValue); |
| 498 | + self::assertEquals($expected, $result); |
| 499 | + } |
| 500 | + |
376 | 501 | /**
|
377 | 502 | * @see it('returning invalid value from resolveType yields useful error')
|
378 | 503 | */
|
|
0 commit comments