|
2 | 2 |
|
3 | 3 | namespace GraphQL\Tests\Type;
|
4 | 4 |
|
| 5 | +use GraphQL\Error\Error; |
5 | 6 | use GraphQL\GraphQL;
|
| 7 | +use GraphQL\Tests\Type\TestClasses\CustomWithObject; |
| 8 | +use GraphQL\Tests\Type\TestClasses\MyCustomType; |
| 9 | +use GraphQL\Tests\Type\TestClasses\OtherCustom; |
| 10 | +use GraphQL\Type\Definition\IntType; |
6 | 11 | use GraphQL\Type\Definition\ListOfType;
|
7 | 12 | use GraphQL\Type\Definition\ObjectType;
|
8 | 13 | use GraphQL\Type\Definition\ResolveInfo;
|
| 14 | +use GraphQL\Type\Definition\StringType; |
9 | 15 | use GraphQL\Type\Definition\Type;
|
| 16 | +use GraphQL\Type\Definition\UnionType; |
10 | 17 | use GraphQL\Type\Schema;
|
11 | 18 | use PHPUnit\Framework\TestCase;
|
12 | 19 |
|
|
456 | 463 | ++$aliasArgsNbTests;
|
457 | 464 | switch ($args['testName']) {
|
458 | 465 | case 'NoAlias':
|
| 466 | + $level2Type = $aliasArgs['level2']['level2']['type'] ?? null; |
| 467 | + self::assertInstanceOf(IntType::class, $level2Type); |
459 | 468 | self::assertSame([
|
460 | 469 | 'level2' => [
|
461 | 470 | 'level2' => [
|
462 | 471 | 'args' => [
|
463 | 472 | 'width' => 1,
|
464 | 473 | 'height' => 1,
|
465 | 474 | ],
|
| 475 | + 'type' => $level2Type, |
466 | 476 | ],
|
467 | 477 | ],
|
468 | 478 | ], $aliasArgs);
|
469 | 479 | break;
|
470 | 480 | case 'NoAliasFirst':
|
| 481 | + $level2Type = $aliasArgs['level2']['level2']['type'] ?? null; |
| 482 | + self::assertInstanceOf(IntType::class, $level2Type); |
471 | 483 | self::assertSame([
|
472 | 484 | 'level2' => [
|
473 | 485 | 'level2' => [
|
474 | 486 | 'args' => [
|
475 | 487 | 'width' => 1,
|
476 | 488 | 'height' => 1,
|
477 | 489 | ],
|
| 490 | + 'type' => $level2Type, |
478 | 491 | ],
|
479 | 492 | 'level1000' => [
|
480 | 493 | 'args' => [
|
481 | 494 | 'width' => 2,
|
482 | 495 | 'height' => 20,
|
483 | 496 | ],
|
| 497 | + 'type' => $level2Type, |
484 | 498 | ],
|
485 | 499 | ],
|
486 | 500 | ], $aliasArgs);
|
487 | 501 | break;
|
488 | 502 | case 'NoAliasLast':
|
| 503 | + $level2Type = $aliasArgs['level2']['level2000']['type'] ?? null; |
| 504 | + self::assertInstanceOf(IntType::class, $level2Type); |
489 | 505 | self::assertSame([
|
490 | 506 | 'level2' => [
|
491 | 507 | 'level2000' => [
|
492 | 508 | 'args' => [
|
493 | 509 | 'width' => 1,
|
494 | 510 | 'height' => 1,
|
495 | 511 | ],
|
| 512 | + 'type' => $level2Type, |
496 | 513 | ],
|
497 | 514 | 'level2' => [
|
498 | 515 | 'args' => [
|
499 | 516 | 'width' => 2,
|
500 | 517 | 'height' => 20,
|
501 | 518 | ],
|
| 519 | + 'type' => $level2Type, |
502 | 520 | ],
|
503 | 521 | ],
|
504 | 522 | ], $aliasArgs);
|
505 | 523 | break;
|
506 | 524 | case 'AllAliases':
|
| 525 | + $level2Type = $aliasArgs['level2']['level1000']['type'] ?? null; |
| 526 | + self::assertInstanceOf(IntType::class, $level2Type); |
507 | 527 | self::assertSame([
|
508 | 528 | 'level2' => [
|
509 | 529 | 'level1000' => [
|
510 | 530 | 'args' => [
|
511 | 531 | 'width' => 1,
|
512 | 532 | 'height' => 1,
|
513 | 533 | ],
|
| 534 | + 'type' => $level2Type, |
514 | 535 | ],
|
515 | 536 | 'level2000' => [
|
516 | 537 | 'args' => [
|
517 | 538 | 'width' => 2,
|
518 | 539 | 'height' => 20,
|
519 | 540 | ],
|
| 541 | + 'type' => $level2Type, |
520 | 542 | ],
|
521 | 543 | ],
|
522 | 544 | ], $aliasArgs);
|
523 | 545 | break;
|
524 | 546 | case 'MultiLvlSameAliasName':
|
525 | 547 | case 'WithFragments':
|
| 548 | + $level2Type = $aliasArgs['level2']['level3000']['type'] ?? null; |
| 549 | + $level2BisType = $aliasArgs['level2bis']['level2bis']['type'] ?? null; |
| 550 | + $level3Type = $aliasArgs['level2bis']['level2bis']['selectionSet']['level3']['level3000']['type'] ?? null; |
| 551 | + self::assertInstanceOf(IntType::class, $level2Type); |
| 552 | + self::assertInstanceOf(ObjectType::class, $level2BisType); |
| 553 | + self::assertInstanceOf(IntType::class, $level3Type); |
526 | 554 | self::assertSame([
|
527 | 555 | 'level2' => [
|
528 | 556 | 'level3000' => [
|
529 | 557 | 'args' => [
|
530 | 558 | 'width' => 1,
|
531 | 559 | 'height' => 1,
|
532 | 560 | ],
|
| 561 | + 'type' => $level2Type, |
533 | 562 | ],
|
534 | 563 | 'level2' => [
|
535 | 564 | 'args' => [
|
536 | 565 | 'width' => 3,
|
537 | 566 | 'height' => 30,
|
538 | 567 | ],
|
| 568 | + 'type' => $level2Type, |
539 | 569 | ],
|
540 | 570 | ],
|
541 | 571 | 'level2bis' => [
|
542 | 572 | 'level2bis' => [
|
543 | 573 | 'args' => [],
|
| 574 | + 'type' => $level2BisType, |
544 | 575 | 'selectionSet' => [
|
545 | 576 | 'level3' => [
|
546 | 577 | 'level3000' => [
|
547 | 578 | 'args' => [
|
548 | 579 | 'length' => 2,
|
549 | 580 | ],
|
| 581 | + 'type' => $level3Type, |
550 | 582 | ],
|
551 | 583 | 'level3' => [
|
552 | 584 | 'args' => [
|
553 | 585 | 'length' => 10,
|
554 | 586 | ],
|
| 587 | + 'type' => $level3Type, |
555 | 588 | ],
|
556 | 589 | ],
|
557 | 590 | ],
|
|
565 | 598 | case 'Deepest':
|
566 | 599 | $depth ??= 5;
|
567 | 600 | $aliasArgs = $info->getFieldSelectionWithAliases($depth);
|
| 601 | + |
| 602 | + $level2BisType = $aliasArgs['level2bis']['level2Alias']['type'] ?? null; |
| 603 | + $level3DeeperType = $aliasArgs['level2bis']['level2Alias']['selectionSet']['level3deeper']['level3deeper']['type'] ?? null; |
| 604 | + $level4evenmoreType = $aliasArgs['level2bis']['level2Alias']['selectionSet']['level3deeper']['level3deeper']['selectionSet']['level4evenmore']['level4evenmore']['type'] ?? null; |
| 605 | + $level5Type = $aliasArgs['level2bis']['level2Alias']['selectionSet']['level3deeper']['level3deeper']['selectionSet']['level4evenmore']['level4evenmore']['selectionSet']['level5']['level5']['type'] ?? null; |
| 606 | + $level4Type = $aliasArgs['level2bis']['level2Alias']['selectionSet']['level3deeper']['level3deeper']['selectionSet']['level4']['level4']['type'] ?? null; |
| 607 | + |
| 608 | + self::assertInstanceOf(ObjectType::class, $level2BisType); |
| 609 | + // Don't test the deepest types because we don't retrieve them with a low $depth |
| 610 | + if ($depth > 1) { |
| 611 | + self::assertInstanceOf(ObjectType::class, $level3DeeperType); |
| 612 | + self::assertInstanceOf(ObjectType::class, $level4evenmoreType); |
| 613 | + self::assertInstanceOf(StringType::class, $level5Type); |
| 614 | + self::assertInstanceOf(IntType::class, $level4Type); |
| 615 | + } |
| 616 | + |
568 | 617 | self::assertSame([
|
569 | 618 | 'level2bis' => [
|
570 | 619 | 'level2Alias' => [
|
571 | 620 | 'args' => [],
|
| 621 | + 'type' => $level2BisType, |
572 | 622 | 'selectionSet' => [
|
573 | 623 | 'level3deeper' => [
|
574 | 624 | 'level3deeper' => [
|
575 | 625 | 'args' => [],
|
| 626 | + 'type' => $level3DeeperType, |
576 | 627 | 'selectionSet' => [
|
577 | 628 | 'level4evenmore' => [
|
578 | 629 | 'level4evenmore' => [
|
579 | 630 | 'args' => [],
|
| 631 | + 'type' => $level4evenmoreType, |
580 | 632 | 'selectionSet' => [
|
581 | 633 | 'level5' => [
|
582 | 634 | 'level5' => [
|
583 | 635 | 'args' => [
|
584 | 636 | 'crazyness' => 0.124,
|
585 | 637 | ],
|
| 638 | + 'type' => $level5Type, |
586 | 639 | ],
|
587 | 640 | 'lastAlias' => [
|
588 | 641 | 'args' => [
|
589 | 642 | 'crazyness' => 0.758,
|
590 | 643 | ],
|
| 644 | + 'type' => $level5Type, |
591 | 645 | ],
|
592 | 646 | ],
|
593 | 647 | ],
|
|
598 | 652 | 'args' => [
|
599 | 653 | 'temperature' => -20,
|
600 | 654 | ],
|
| 655 | + 'type' => $level4Type, |
| 656 | + ], |
| 657 | + ], |
| 658 | + ], |
| 659 | + ], |
| 660 | + ], |
| 661 | + ], |
| 662 | + ], |
| 663 | + ], |
| 664 | + ], $aliasArgs); |
| 665 | + break; |
| 666 | + case 'WithUnion': |
| 667 | + $levelUnionType = $aliasArgs['levelUnion']['levelUnion']['type'] ?? null; |
| 668 | + $levelMyCustomType = $aliasArgs['levelUnion']['levelUnion']['unions']['MyCustom']['type'] ?? null; |
| 669 | + $levelAType = $aliasArgs['levelUnion']['levelUnion']['unions']['MyCustom']['selectionSet']['a']['a']['type'] ?? null; |
| 670 | + $levelCustomWithObjectType = $aliasArgs['levelUnion']['levelUnion']['unions']['CustomWithObject']['type'] ?? null; |
| 671 | + $levelOtherCustomType = $aliasArgs['levelUnion']['levelUnion']['unions']['CustomWithObject']['selectionSet']['customB']['customB']['type'] ?? null; |
| 672 | + $levelBType = $aliasArgs['levelUnion']['levelUnion']['unions']['CustomWithObject']['selectionSet']['customB']['customB']['selectionSet']['b']['b']['type'] ?? null; |
| 673 | + self::assertInstanceOf(UnionType::class, $levelUnionType); |
| 674 | + self::assertInstanceOf(MyCustomType::class, $levelMyCustomType); |
| 675 | + self::assertInstanceOf(StringType::class, $levelAType); |
| 676 | + self::assertInstanceOf(CustomWithObject::class, $levelCustomWithObjectType); |
| 677 | + self::assertInstanceOf(OtherCustom::class, $levelOtherCustomType); |
| 678 | + self::assertInstanceOf(StringType::class, $levelBType); |
| 679 | + self::assertSame([ |
| 680 | + 'levelUnion' => [ |
| 681 | + 'levelUnion' => [ |
| 682 | + 'args' => [], |
| 683 | + 'type' => $levelUnionType, |
| 684 | + 'unions' => [ |
| 685 | + 'MyCustom' => [ |
| 686 | + 'type' => $levelMyCustomType, |
| 687 | + 'selectionSet' => [ |
| 688 | + 'a' => [ |
| 689 | + 'a' => [ |
| 690 | + 'args' => [], |
| 691 | + 'type' => $levelAType, |
| 692 | + ], |
| 693 | + ], |
| 694 | + ], |
| 695 | + ], |
| 696 | + 'CustomWithObject' => [ |
| 697 | + 'type' => $levelCustomWithObjectType, |
| 698 | + 'selectionSet' => [ |
| 699 | + 'customA' => [ |
| 700 | + 'customA' => [ |
| 701 | + 'args' => [], |
| 702 | + 'type' => $levelMyCustomType, |
| 703 | + 'selectionSet' => [ |
| 704 | + 'a' => [ |
| 705 | + 'a' => [ |
| 706 | + 'args' => [], |
| 707 | + 'type' => $levelAType, |
| 708 | + ], |
| 709 | + ], |
| 710 | + ], |
| 711 | + ], |
| 712 | + ], |
| 713 | + 'customB' => [ |
| 714 | + 'customB' => [ |
| 715 | + 'args' => [], |
| 716 | + 'type' => $levelOtherCustomType, |
| 717 | + 'selectionSet' => [ |
| 718 | + 'b' => [ |
| 719 | + 'b' => [ |
| 720 | + 'args' => [], |
| 721 | + 'type' => $levelBType, |
| 722 | + ], |
| 723 | + ], |
601 | 724 | ],
|
602 | 725 | ],
|
603 | 726 | ],
|
|
613 | 736 | }
|
614 | 737 | };
|
615 | 738 |
|
| 739 | + $myCustomWithObjectType = new CustomWithObject(); |
| 740 | + // retrieve the instance from the parent type in order to don't instantiate twice the same type in the same schema |
| 741 | + $myCustomType = $myCustomWithObjectType->config['fields']['customA']; |
| 742 | + $levelUnion = new UnionType([ |
| 743 | + 'name' => 'CustomOrOther', |
| 744 | + 'types' => [ |
| 745 | + $myCustomType, |
| 746 | + $myCustomWithObjectType, |
| 747 | + ], |
| 748 | + 'resolveType' => function ($value) use ($myCustomType, $myCustomWithObjectType): ObjectType { |
| 749 | + switch (get_class($value)) { |
| 750 | + case MyCustomType::class: return $myCustomType; |
| 751 | + case CustomWithObject::class: return $myCustomWithObjectType; |
| 752 | + default: throw new Error('Unexpected union type'); |
| 753 | + } |
| 754 | + }, |
| 755 | + ]); |
| 756 | + |
616 | 757 | $level4EvenMore = new ObjectType([
|
617 | 758 | 'name' => 'Level4EvenMore',
|
618 | 759 | 'fields' => [
|
|
685 | 826 | 'type' => $level2Bis,
|
686 | 827 | 'resolve' => fn (): bool => true,
|
687 | 828 | ],
|
| 829 | + 'levelUnion' => [ |
| 830 | + 'type' => $levelUnion, |
| 831 | + 'resolve' => fn (): bool => true, |
| 832 | + ], |
688 | 833 | ],
|
689 | 834 | ]);
|
690 | 835 |
|
|
797 | 942 | }
|
798 | 943 | }
|
799 | 944 | }
|
800 |
| - |
| 945 | +
|
801 | 946 | fragment level3Frag on Level2bis {
|
802 | 947 | level3000: level3(length: 2)
|
803 | 948 | level3(length: 10)
|
|
855 | 1000 | GRAPHQL
|
856 | 1001 | );
|
857 | 1002 |
|
| 1003 | + $result10 = GraphQL::executeQuery( |
| 1004 | + new Schema(['query' => $query]), |
| 1005 | + <<<GRAPHQL |
| 1006 | + query { |
| 1007 | + level1(testName: "WithUnion") { |
| 1008 | + levelUnion { |
| 1009 | + ...on MyCustom { |
| 1010 | + a |
| 1011 | + } |
| 1012 | + ...on CustomWithObject { |
| 1013 | + customA { |
| 1014 | + a |
| 1015 | + } |
| 1016 | + customB { |
| 1017 | + b |
| 1018 | + } |
| 1019 | + } |
| 1020 | + } |
| 1021 | + } |
| 1022 | + } |
| 1023 | + GRAPHQL |
| 1024 | + ); |
| 1025 | + |
858 | 1026 | self::assertEmpty($result1->errors, 'Query NoAlias should have no errors');
|
859 | 1027 | self::assertEmpty($result2->errors, 'Query NoAliasFirst should have no errors');
|
860 | 1028 | self::assertEmpty($result3->errors, 'Query NoAliasLast should have no errors');
|
|
864 | 1032 | self::assertSame('Failed asserting that two arrays are identical.', $result7->errors[0]->getMessage(), 'Query DeepestTooLowDepth should have failed');
|
865 | 1033 | self::assertEmpty($result8->errors, 'Query Deepest should have no errors');
|
866 | 1034 | self::assertEmpty($result9->errors, 'Query With ListOf type should have no errors');
|
| 1035 | + self::assertEmpty($result10->errors, 'Query With Union type should have no errors'); |
867 | 1036 | }
|
868 | 1037 |
|
869 | 1038 | public function testPathAndUnaliasedPath(): void
|
|
0 commit comments