33namespace TheCodingMachine \GraphQLite \Mappers \Parameters ;
44
55use DateTimeImmutable ;
6+ use GraphQL \Type \Definition \NonNull ;
67use GraphQL \Type \Definition \ResolveInfo ;
8+ use GraphQL \Type \Definition \UnionType ;
79use ReflectionMethod ;
810use Symfony \Component \Cache \Adapter \ArrayAdapter ;
911use Symfony \Component \Cache \Psr16Cache ;
1012use Symfony \Component \Cache \Simple \ArrayCache ;
1113use TheCodingMachine \GraphQLite \AbstractQueryProviderTest ;
1214use TheCodingMachine \GraphQLite \Annotations \HideParameter ;
15+ use TheCodingMachine \GraphQLite \Fixtures \TestObject ;
16+ use TheCodingMachine \GraphQLite \Fixtures \TestObject2 ;
1317use TheCodingMachine \GraphQLite \Mappers \CannotMapTypeException ;
1418use TheCodingMachine \GraphQLite \Mappers \Root \BaseTypeMapper ;
1519use TheCodingMachine \GraphQLite \Mappers \Root \CompositeRootTypeMapper ;
@@ -34,6 +38,26 @@ public function testMapScalarUnionException(): void
3438 $ typeMapper ->mapReturnType ($ refMethod , $ docBlockObj );
3539 }
3640
41+ public function testMapObjectUnionWorks (): void
42+ {
43+ $ typeMapper = new TypeHandler ($ this ->getArgumentResolver (), $ this ->getRootTypeMapper (), $ this ->getTypeResolver ());
44+
45+ $ cachedDocBlockFactory = new CachedDocBlockFactory (new Psr16Cache (new ArrayAdapter ()));
46+
47+ $ refMethod = new ReflectionMethod ($ this , 'objectUnion ' );
48+ $ docBlockObj = $ cachedDocBlockFactory ->getDocBlock ($ refMethod );
49+
50+ $ gqType = $ typeMapper ->mapReturnType ($ refMethod , $ docBlockObj );
51+ $ this ->assertInstanceOf (NonNull::class, $ gqType );
52+ assert ($ gqType instanceof NonNull);
53+ $ memberType = $ gqType ->getOfType ();
54+ $ this ->assertInstanceOf (UnionType::class, $ memberType );
55+ assert ($ memberType instanceof UnionType);
56+ $ unionTypes = $ memberType ->getTypes ();
57+ $ this ->assertEquals ('TestObject ' , $ unionTypes [0 ]->name );
58+ $ this ->assertEquals ('TestObject2 ' , $ unionTypes [1 ]->name );
59+ }
60+
3761 public function testHideParameter (): void
3862 {
3963 $ typeMapper = new TypeHandler ($ this ->getArgumentResolver (), $ this ->getRootTypeMapper (), $ this ->getTypeResolver ());
@@ -77,6 +101,10 @@ private function dummy() {
77101
78102 }
79103
104+ private function objectUnion (): TestObject |TestObject2 {
105+ return new TestObject (('' ));
106+ }
107+
80108 /**
81109 * @HideParameter(for="$foo")
82110 */
0 commit comments