Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support native union return typing #467

Merged
merged 2 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/Mappers/Parameters/TypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ReflectionParameter;
use ReflectionProperty;
use ReflectionType;
use ReflectionUnionType;
use TheCodingMachine\GraphQLite\Annotations\HideParameter;
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotations;
use TheCodingMachine\GraphQLite\Annotations\UseInputType;
Expand All @@ -42,6 +43,7 @@
use TheCodingMachine\GraphQLite\Types\TypeResolver;
use Webmozart\Assert\Assert;

use function array_map;
use function array_merge;
use function array_unique;
use function assert;
Expand Down Expand Up @@ -408,17 +410,36 @@ private function appendTypes(Type $type, ?Type $docBlockType): Type
*/
private function reflectionTypeToPhpDocType(ReflectionType $type, ReflectionClass $reflectionClass): Type
{
assert($type instanceof ReflectionNamedType);
$phpdocType = $this->phpDocumentorTypeResolver->resolve($type->getName());
Assert::notNull($phpdocType);
assert($type instanceof ReflectionNamedType || $type instanceof ReflectionUnionType);
if ($type instanceof ReflectionNamedType) {
$phpdocType = $this->phpDocumentorTypeResolver->resolve($type->getName());
Assert::notNull($phpdocType);

$phpdocType = $this->resolveSelf($phpdocType, $reflectionClass);
$phpdocType = $this->resolveSelf($phpdocType, $reflectionClass);

if ($type->allowsNull()) {
$phpdocType = new Nullable($phpdocType);
}
if ($type->allowsNull()) {
$phpdocType = new Nullable($phpdocType);
}

return $phpdocType;
return $phpdocType;
}
return new Compound(
array_map(
function ($namedType) use ($reflectionClass): Type {
\assert($namedType instanceof ReflectionNamedType);
oojacoboo marked this conversation as resolved.
Show resolved Hide resolved
$phpdocType = $this->phpDocumentorTypeResolver->resolve($namedType->getName());
Assert::notNull($phpdocType);

$phpdocType = $this->resolveSelf($phpdocType, $reflectionClass);

if ($namedType->allowsNull()) {
$phpdocType = new Nullable($phpdocType);
}
return $phpdocType;
},
$type->getTypes()
)
);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures80/UnionOutputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace TheCodingMachine\GraphQLite\Fixtures80;

use TheCodingMachine\GraphQLite\Fixtures\TestObject;
use TheCodingMachine\GraphQLite\Fixtures\TestObject2;

class UnionOutputType
{
public function objectUnion(): TestObject|TestObject2 {
return new TestObject((''));
}
}
26 changes: 26 additions & 0 deletions tests/Mappers/Parameters/TypeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace TheCodingMachine\GraphQLite\Mappers\Parameters;

use DateTimeImmutable;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\UnionType;
use ReflectionMethod;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\Cache\Simple\ArrayCache;
use TheCodingMachine\GraphQLite\AbstractQueryProviderTest;
use TheCodingMachine\GraphQLite\Annotations\HideParameter;
use TheCodingMachine\GraphQLite\Fixtures80\UnionOutputType;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\Root\BaseTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\CompositeRootTypeMapper;
Expand All @@ -34,6 +37,29 @@ public function testMapScalarUnionException(): void
$typeMapper->mapReturnType($refMethod, $docBlockObj);
}

/**
* @requires PHP >= 8.0
*/
public function testMapObjectUnionWorks(): void
{
$typeMapper = new TypeHandler($this->getArgumentResolver(), $this->getRootTypeMapper(), $this->getTypeResolver());

$cachedDocBlockFactory = new CachedDocBlockFactory(new Psr16Cache(new ArrayAdapter()));

$refMethod = new ReflectionMethod(UnionOutputType::class, 'objectUnion');
$docBlockObj = $cachedDocBlockFactory->getDocBlock($refMethod);

$gqType = $typeMapper->mapReturnType($refMethod, $docBlockObj);
$this->assertInstanceOf(NonNull::class, $gqType);
assert($gqType instanceof NonNull);
$memberType = $gqType->getOfType();
$this->assertInstanceOf(UnionType::class, $memberType);
assert($memberType instanceof UnionType);
$unionTypes = $memberType->getTypes();
$this->assertEquals('TestObject', $unionTypes[0]->name);
$this->assertEquals('TestObject2', $unionTypes[1]->name);
}

public function testHideParameter(): void
{
$typeMapper = new TypeHandler($this->getArgumentResolver(), $this->getRootTypeMapper(), $this->getTypeResolver());
Expand Down