Skip to content
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
12 changes: 9 additions & 3 deletions src/Mappers/Root/CompoundTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use RuntimeException;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
use TheCodingMachine\GraphQLite\NamingStrategyInterface;
use TheCodingMachine\GraphQLite\TypeRegistry;
use TheCodingMachine\GraphQLite\Types\UnionType;

Expand All @@ -38,8 +39,13 @@
*/
class CompoundTypeMapper implements RootTypeMapperInterface
{
public function __construct(private RootTypeMapperInterface $next, private RootTypeMapperInterface $topRootTypeMapper, private TypeRegistry $typeRegistry, private RecursiveTypeMapperInterface $recursiveTypeMapper)
{
public function __construct(
private RootTypeMapperInterface $next,
private RootTypeMapperInterface $topRootTypeMapper,
private NamingStrategyInterface $namingStrategy,
private TypeRegistry $typeRegistry,
private RecursiveTypeMapperInterface $recursiveTypeMapper,
) {
}

public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType
Expand Down Expand Up @@ -144,7 +150,7 @@ private function getTypeFromUnion(array $unionTypes): GraphQLType
throw CannotMapTypeException::createForBadTypeInUnion($unionTypes);
}

$graphQlType = new UnionType($nonNullableUnionTypes, $this->recursiveTypeMapper);
$graphQlType = new UnionType($nonNullableUnionTypes, $this->recursiveTypeMapper, $this->namingStrategy);
$graphQlType = $this->typeRegistry->getOrRegisterType($graphQlType);
assert($graphQlType instanceof UnionType);
}
Expand Down
11 changes: 11 additions & 0 deletions src/NamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use TheCodingMachine\GraphQLite\Annotations\Input;
use TheCodingMachine\GraphQLite\Annotations\TypeInterface;

use function implode;
use function lcfirst;
use function str_ends_with;
use function str_replace;
Expand Down Expand Up @@ -109,4 +110,14 @@ public function getInputFieldNameFromMethodName(string $methodName): string

return $methodName;
}

/**
* Returns the name of a GraphQL union type based on the included types.
*
* @param string[] $typeNames The list of GraphQL type names
*/
public function getUnionTypeName(array $typeNames): string
{
return 'Union' . implode('', $typeNames);
}
}
7 changes: 7 additions & 0 deletions src/NamingStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ public function getFieldNameFromMethodName(string $methodName): string;
* Returns the name of a GraphQL input field from the name of the annotated method.
*/
public function getInputFieldNameFromMethodName(string $methodName): string;

/**
* Returns the name of a GraphQL union type based on the included types.
*
* @param string[] $typeNames The list of GraphQL type names
*/
public function getUnionTypeName(array $typeNames): string;
}
2 changes: 1 addition & 1 deletion src/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public function createSchema(): Schema
}
}

$rootTypeMapper = new CompoundTypeMapper($rootTypeMapper, $topRootTypeMapper, $typeRegistry, $recursiveTypeMapper);
$rootTypeMapper = new CompoundTypeMapper($rootTypeMapper, $topRootTypeMapper, $namingStrategy, $typeRegistry, $recursiveTypeMapper);
$rootTypeMapper = new IteratorTypeMapper($rootTypeMapper, $topRootTypeMapper);

$topRootTypeMapper->setNext($rootTypeMapper);
Expand Down
15 changes: 12 additions & 3 deletions src/Types/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@
use GraphQL\Type\Definition\ObjectType;
use InvalidArgumentException;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
use TheCodingMachine\GraphQLite\NamingStrategyInterface;

use function array_map;
use function gettype;
use function is_object;

class UnionType extends \GraphQL\Type\Definition\UnionType
{
/** @param array<int,ObjectType> $types */
public function __construct(array $types, RecursiveTypeMapperInterface $typeMapper)
public function __construct(
array $types,
RecursiveTypeMapperInterface $typeMapper,
NamingStrategyInterface $namingStrategy,
)
{
$name = 'Union';
// Make sure all types are object types
foreach ($types as $type) {
$name .= $type->name;
if (! $type instanceof ObjectType) {
throw InvalidTypesInUnionException::notObjectType();
}
}

$typeNames = array_map(static fn (ObjectType $type) => $type->name, $types);
$name = $namingStrategy->getUnionTypeName($typeNames);

parent::__construct([
'name' => $name,
'types' => $types,
Expand Down
1 change: 1 addition & 0 deletions tests/AbstractQueryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ protected function buildRootTypeMapper(): RootTypeMapperInterface
$rootTypeMapper = new CompoundTypeMapper(
$rootTypeMapper,
$topRootTypeMapper,
new NamingStrategy(),
$this->getTypeRegistry(),
$this->getTypeMapper()
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
if (interface_exists(UnitEnum::class)) {
$rootTypeMapper = new EnumTypeMapper($rootTypeMapper, $container->get(AnnotationReader::class), new ArrayAdapter(), [ $container->get(NamespaceFactory::class)->createNamespace('TheCodingMachine\\GraphQLite\\Fixtures81\\Integration\\Models') ]);
}
$rootTypeMapper = new CompoundTypeMapper($rootTypeMapper, $container->get(RootTypeMapperInterface::class), $container->get(TypeRegistry::class), $container->get(RecursiveTypeMapperInterface::class));
$rootTypeMapper = new CompoundTypeMapper($rootTypeMapper, $container->get(RootTypeMapperInterface::class), $container->get(NamingStrategyInterface::class), $container->get(TypeRegistry::class), $container->get(RecursiveTypeMapperInterface::class));
$rootTypeMapper = new IteratorTypeMapper($rootTypeMapper, $container->get(RootTypeMapperInterface::class));
return $rootTypeMapper;
},
Expand Down
3 changes: 3 additions & 0 deletions tests/Mappers/Root/CompoundTypeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use RuntimeException;
use TheCodingMachine\GraphQLite\AbstractQueryProviderTest;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\NamingStrategy;

class CompoundTypeMapperTest extends AbstractQueryProviderTest
{
Expand All @@ -19,6 +20,7 @@ public function testException1()
$compoundTypeMapper = new CompoundTypeMapper(
new FinalRootTypeMapper($this->getTypeMapper()),
new FinalRootTypeMapper($this->getTypeMapper()),
new NamingStrategy(),
$this->getTypeRegistry(),
$this->getTypeMapper()
);
Expand All @@ -32,6 +34,7 @@ public function testException2()
$compoundTypeMapper = new CompoundTypeMapper(
new FinalRootTypeMapper($this->getTypeMapper()),
new FinalRootTypeMapper($this->getTypeMapper()),
new NamingStrategy(),
$this->getTypeRegistry(),
$this->getTypeMapper()
);
Expand Down
10 changes: 9 additions & 1 deletion tests/NamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class NamingStrategyTest extends TestCase
{

public function testGetInputTypeName(): void
{
$namingStrategy = new NamingStrategy();
Expand Down Expand Up @@ -43,4 +42,13 @@ public function testGetFieldNameFromTypeAnnotation(): void
$name = $namingStrategy->getOutputTypeName(TestObject::class, $type);
$this->assertSame('foo', $name);
}

public function testGetUnionTypeName(): void
{
$namingStrategy = new NamingStrategy();

$typeNames = ['Some', 'Arbitrary', 'Type', 'Names'];
$name = $namingStrategy->getUnionTypeName($typeNames);
$this->assertSame('UnionSomeArbitraryTypeNames', $name);
}
}
7 changes: 4 additions & 3 deletions tests/Types/UnionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
use TheCodingMachine\GraphQLite\AbstractQueryProviderTest;
use TheCodingMachine\GraphQLite\Fixtures\TestObject;
use TheCodingMachine\GraphQLite\Fixtures\TestObject2;
use TheCodingMachine\GraphQLite\NamingStrategy;

class UnionTypeTest extends AbstractQueryProviderTest
{
public function testConstructor(): void
{
$unionType = new UnionType([$this->getTestObjectType(), $this->getTestObjectType2()], $this->getTypeMapper());
$unionType = new UnionType([$this->getTestObjectType(), $this->getTestObjectType2()], $this->getTypeMapper(), new NamingStrategy());
$resolveInfo = $this->getMockBuilder(ResolveInfo::class)->disableOriginalConstructor()->getMock();
$type = $unionType->resolveType(new TestObject('foo'), null, $resolveInfo);
$this->assertSame($this->getTestObjectType(), $type);
Expand All @@ -23,7 +24,7 @@ public function testConstructor(): void

public function testException(): void
{
$unionType = new UnionType([$this->getTestObjectType(), $this->getTestObjectType2()], $this->getTypeMapper());
$unionType = new UnionType([$this->getTestObjectType(), $this->getTestObjectType2()], $this->getTypeMapper(), new NamingStrategy());
$this->expectException(\InvalidArgumentException::class);
$resolveInfo = $this->getMockBuilder(ResolveInfo::class)->disableOriginalConstructor()->getMock();
$unionType->resolveType('foo', null, $resolveInfo);
Expand All @@ -32,6 +33,6 @@ public function testException(): void
public function testException2(): void
{
$this->expectException(\InvalidArgumentException::class);
new UnionType([new StringType()], $this->getTypeMapper());
new UnionType([new StringType()], $this->getTypeMapper(), new NamingStrategy());
}
}