|
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,192 @@ public function testHintsOnConflictingTypeInstancesInResolveType(): void
|
773 | 774 | $error->getMessage()
|
774 | 775 | );
|
775 | 776 | }
|
| 777 | + |
| 778 | + public function testResolveValueAllowsModifyingObjectValueForInterfaceType(): 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->vocalizes); |
| 792 | + } |
| 793 | + |
| 794 | + return new Cat($objectValue->name, $objectValue->vocalizes); |
| 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 | + } |
| 873 | + |
| 874 | + public function testResolveValueAllowsModifyingObjectValueForUnionType(): void |
| 875 | + { |
| 876 | + $DogType = new ObjectType([ |
| 877 | + 'name' => 'Dog', |
| 878 | + 'fields' => [ |
| 879 | + 'name' => [ |
| 880 | + 'type' => Type::string(), |
| 881 | + 'resolve' => fn (Dog $dog) => $dog->name, |
| 882 | + ], |
| 883 | + 'woofs' => [ |
| 884 | + 'type' => Type::boolean(), |
| 885 | + 'resolve' => fn (Dog $dog) => $dog->woofs, |
| 886 | + ], |
| 887 | + ], |
| 888 | + ]); |
| 889 | + |
| 890 | + $CatType = new ObjectType([ |
| 891 | + 'name' => 'Cat', |
| 892 | + 'fields' => [ |
| 893 | + 'name' => [ |
| 894 | + 'type' => Type::string(), |
| 895 | + 'resolve' => fn (Cat $cat) => $cat->name, |
| 896 | + ], |
| 897 | + 'meows' => [ |
| 898 | + 'type' => Type::boolean(), |
| 899 | + 'resolve' => fn (Cat $cat) => $cat->meows, |
| 900 | + ], |
| 901 | + ], |
| 902 | + ]); |
| 903 | + |
| 904 | + $PetType = new UnionType([ |
| 905 | + 'name' => 'Pet', |
| 906 | + 'types' => [$DogType, $CatType], |
| 907 | + 'resolveType' => static function (PetEntity $objectValue): string { |
| 908 | + if ($objectValue->type === 'dog') { |
| 909 | + return 'Dog'; |
| 910 | + } |
| 911 | + |
| 912 | + return 'Cat'; |
| 913 | + }, |
| 914 | + 'resolveValue' => static function (PetEntity $objectValue) { |
| 915 | + if ($objectValue->type === 'dog') { |
| 916 | + return new Dog($objectValue->name, $objectValue->vocalizes); |
| 917 | + } |
| 918 | + |
| 919 | + return new Cat($objectValue->name, $objectValue->vocalizes); |
| 920 | + }, |
| 921 | + ]); |
| 922 | + |
| 923 | + $schema = new Schema([ |
| 924 | + 'query' => new ObjectType([ |
| 925 | + 'name' => 'Query', |
| 926 | + 'fields' => [ |
| 927 | + 'pets' => [ |
| 928 | + 'type' => Type::listOf($PetType), |
| 929 | + 'resolve' => static fn (): array => [ |
| 930 | + new PetEntity('dog', 'Odie', true), |
| 931 | + new PetEntity('cat', 'Garfield', false), |
| 932 | + ], |
| 933 | + ], |
| 934 | + ], |
| 935 | + ]), |
| 936 | + ]); |
| 937 | + |
| 938 | + $query = '{ |
| 939 | + pets { |
| 940 | + ... on Dog { |
| 941 | + name |
| 942 | + woofs |
| 943 | + } |
| 944 | + ... on Cat { |
| 945 | + name |
| 946 | + meows |
| 947 | + } |
| 948 | + } |
| 949 | + }'; |
| 950 | + |
| 951 | + $result = GraphQL::executeQuery($schema, $query)->toArray(); |
| 952 | + |
| 953 | + self::assertEquals( |
| 954 | + [ |
| 955 | + 'data' => [ |
| 956 | + 'pets' => [ |
| 957 | + ['name' => 'Odie', 'woofs' => true], |
| 958 | + ['name' => 'Garfield', 'meows' => false], |
| 959 | + ], |
| 960 | + ], |
| 961 | + ], |
| 962 | + $result |
| 963 | + ); |
| 964 | + } |
776 | 965 | }
|
0 commit comments