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 \Fixtures80 \UnionOutputType ;
1316use TheCodingMachine \GraphQLite \Mappers \CannotMapTypeException ;
1417use TheCodingMachine \GraphQLite \Mappers \Root \BaseTypeMapper ;
1518use TheCodingMachine \GraphQLite \Mappers \Root \CompositeRootTypeMapper ;
@@ -34,6 +37,29 @@ public function testMapScalarUnionException(): void
3437 $ typeMapper ->mapReturnType ($ refMethod , $ docBlockObj );
3538 }
3639
40+ /**
41+ * @requires PHP >= 8.0
42+ */
43+ public function testMapObjectUnionWorks (): void
44+ {
45+ $ typeMapper = new TypeHandler ($ this ->getArgumentResolver (), $ this ->getRootTypeMapper (), $ this ->getTypeResolver ());
46+
47+ $ cachedDocBlockFactory = new CachedDocBlockFactory (new Psr16Cache (new ArrayAdapter ()));
48+
49+ $ refMethod = new ReflectionMethod (UnionOutputType::class, 'objectUnion ' );
50+ $ docBlockObj = $ cachedDocBlockFactory ->getDocBlock ($ refMethod );
51+
52+ $ gqType = $ typeMapper ->mapReturnType ($ refMethod , $ docBlockObj );
53+ $ this ->assertInstanceOf (NonNull::class, $ gqType );
54+ assert ($ gqType instanceof NonNull);
55+ $ memberType = $ gqType ->getOfType ();
56+ $ this ->assertInstanceOf (UnionType::class, $ memberType );
57+ assert ($ memberType instanceof UnionType);
58+ $ unionTypes = $ memberType ->getTypes ();
59+ $ this ->assertEquals ('TestObject ' , $ unionTypes [0 ]->name );
60+ $ this ->assertEquals ('TestObject2 ' , $ unionTypes [1 ]->name );
61+ }
62+
3763 public function testHideParameter (): void
3864 {
3965 $ typeMapper = new TypeHandler ($ this ->getArgumentResolver (), $ this ->getRootTypeMapper (), $ this ->getTypeResolver ());
0 commit comments