|
13 | 13 | use GraphQL\Tests\Executor\TestClasses\Cat;
|
14 | 14 | use GraphQL\Tests\Executor\TestClasses\Dog;
|
15 | 15 | use GraphQL\Tests\Executor\TestClasses\Human;
|
| 16 | +use GraphQL\Tests\Executor\TestClasses\PetEntity; |
16 | 17 | use GraphQL\Type\Definition\InterfaceType;
|
17 | 18 | use GraphQL\Type\Definition\ObjectType;
|
18 | 19 | use GraphQL\Type\Definition\Type;
|
@@ -773,4 +774,100 @@ public function testHintsOnConflictingTypeInstancesInResolveType(): void
|
773 | 774 | $error->getMessage()
|
774 | 775 | );
|
775 | 776 | }
|
| 777 | + |
| 778 | + public function testResolveTypeAllowsModifyingObjectValue(): void |
| 779 | + { |
| 780 | + $PetType = new InterfaceType([ |
| 781 | + 'name' => 'Pet', |
| 782 | + 'resolveType' => static function (PetEntity $objectValue): string { |
| 783 | + if ($objectValue->type === 'dog') { |
| 784 | + return 'Dog'; |
| 785 | + } |
| 786 | + |
| 787 | + return 'Cat'; |
| 788 | + }, |
| 789 | + 'resolveValue' => static function (PetEntity $objectValue) { |
| 790 | + if ($objectValue->type === 'dog') { |
| 791 | + return new Dog($objectValue->name, $objectValue->woofs); |
| 792 | + } |
| 793 | + |
| 794 | + return new Cat($objectValue->name, $objectValue->woofs); |
| 795 | + }, |
| 796 | + 'fields' => [ |
| 797 | + 'name' => ['type' => Type::string()], |
| 798 | + ], |
| 799 | + ]); |
| 800 | + |
| 801 | + $DogType = new ObjectType([ |
| 802 | + 'name' => 'Dog', |
| 803 | + 'interfaces' => [$PetType], |
| 804 | + 'fields' => [ |
| 805 | + 'name' => [ |
| 806 | + 'type' => Type::string(), |
| 807 | + 'resolve' => fn (Dog $dog) => $dog->name, |
| 808 | + ], |
| 809 | + 'woofs' => [ |
| 810 | + 'type' => Type::boolean(), |
| 811 | + 'resolve' => fn (Dog $dog) => $dog->woofs, |
| 812 | + ], |
| 813 | + ], |
| 814 | + ]); |
| 815 | + |
| 816 | + $CatType = new ObjectType([ |
| 817 | + 'name' => 'Cat', |
| 818 | + 'interfaces' => [$PetType], |
| 819 | + 'fields' => [ |
| 820 | + 'name' => [ |
| 821 | + 'type' => Type::string(), |
| 822 | + 'resolve' => fn (Cat $cat) => $cat->name, |
| 823 | + ], |
| 824 | + 'meows' => [ |
| 825 | + 'type' => Type::boolean(), |
| 826 | + 'resolve' => fn (Cat $cat) => $cat->meows, |
| 827 | + ], |
| 828 | + ], |
| 829 | + ]); |
| 830 | + |
| 831 | + $schema = new Schema([ |
| 832 | + 'query' => new ObjectType([ |
| 833 | + 'name' => 'Query', |
| 834 | + 'fields' => [ |
| 835 | + 'pets' => [ |
| 836 | + 'type' => Type::listOf($PetType), |
| 837 | + 'resolve' => static fn (): array => [ |
| 838 | + new PetEntity('dog', 'Odie', true), |
| 839 | + new PetEntity('cat', 'Garfield', false), |
| 840 | + ], |
| 841 | + ], |
| 842 | + ], |
| 843 | + ]), |
| 844 | + 'types' => [$CatType, $DogType], |
| 845 | + ]); |
| 846 | + |
| 847 | + $query = '{ |
| 848 | + pets { |
| 849 | + name |
| 850 | + ... on Dog { |
| 851 | + woofs |
| 852 | + } |
| 853 | + ... on Cat { |
| 854 | + meows |
| 855 | + } |
| 856 | + } |
| 857 | + }'; |
| 858 | + |
| 859 | + $result = GraphQL::executeQuery($schema, $query)->toArray(); |
| 860 | + |
| 861 | + self::assertEquals( |
| 862 | + [ |
| 863 | + 'data' => [ |
| 864 | + 'pets' => [ |
| 865 | + ['name' => 'Odie', 'woofs' => true], |
| 866 | + ['name' => 'Garfield', 'meows' => false], |
| 867 | + ], |
| 868 | + ], |
| 869 | + ], |
| 870 | + $result |
| 871 | + ); |
| 872 | + } |
776 | 873 | }
|
0 commit comments