Skip to content

Commit ea111ae

Browse files
committed
Throw exception early for entity with composite identifiers
This is a well-known limitation that is now enforced early to avoid bad surprise when using the API. Fix #13
1 parent 4fe938b commit ea111ae

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

src/Factory/MetadataReader/MappingDriverChainAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use GraphQL\Doctrine\Exception;
1111
use ReflectionClass;
1212

13-
class MappingDriverChainAdapter implements Reader
13+
final class MappingDriverChainAdapter implements Reader
1414
{
1515
/**
1616
* @var MappingDriverChain

src/Factory/Type/EntityIDTypeFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace GraphQL\Doctrine\Factory\Type;
66

77
use GraphQL\Doctrine\Definition\EntityIDType;
8+
use GraphQL\Doctrine\Exception;
89
use GraphQL\Type\Definition\Type;
910

1011
/**
@@ -22,6 +23,11 @@ final class EntityIDTypeFactory extends AbstractTypeFactory
2223
*/
2324
public function create(string $className, string $typeName): Type
2425
{
26+
$identifiers = $this->entityManager->getClassMetadata($className)->getIdentifier();
27+
if (count($identifiers) > 1) {
28+
throw new Exception('Entities with composite identifiers are not supported by graphql-doctrine. The entity `' . $className . '` cannot be used as input type.');
29+
}
30+
2531
return new EntityIDType($this->entityManager, $className, $typeName);
2632
}
2733
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GraphQLTests\Doctrine\Blog\Model\Special;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
9+
/**
10+
* This is intended to be invalid for graphql-doctrine because it has composite identifiers.
11+
*
12+
* @ORM\Entity
13+
*/
14+
final class CompositeIdentifier
15+
{
16+
/**
17+
* @var int
18+
*
19+
* @ORM\Column(type="integer")
20+
* @ORM\Id
21+
*/
22+
private $id1;
23+
24+
/**
25+
* @var int
26+
*
27+
* @ORM\Column(type="integer")
28+
* @ORM\Id
29+
*/
30+
private $id2;
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GraphQLTests\Doctrine\Factory\Type;
6+
7+
use GraphQL\Doctrine\Definition\EntityIDType;
8+
use GraphQL\Doctrine\Factory\Type\EntityIDTypeFactory;
9+
use GraphQL\Doctrine\Types;
10+
use GraphQLTests\Doctrine\Blog\Model\Special\CompositeIdentifier;
11+
use GraphQLTests\Doctrine\Blog\Model\User;
12+
use GraphQLTests\Doctrine\EntityManagerTrait;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class EntityIDTypeFactoryTest extends TestCase
16+
{
17+
use EntityManagerTrait;
18+
19+
/**
20+
* @var EntityIDTypeFactory
21+
*/
22+
private $entityIDTypeFactory;
23+
24+
public function setUp(): void
25+
{
26+
$this->setUpEntityManager();
27+
28+
$types = new Types($this->entityManager);
29+
$this->entityIDTypeFactory = new EntityIDTypeFactory($types, $this->entityManager);
30+
}
31+
32+
public function testCreateEntityIDType(): void
33+
{
34+
self::assertInstanceOf(EntityIDType::class, $this->entityIDTypeFactory->create(User::class, 'foo'));
35+
}
36+
37+
public function testEntityWithCompositeIdentifierMustThrow(): void
38+
{
39+
$this->expectExceptionMessage('Entities with composite identifiers are not supported by graphql-doctrine. The entity `GraphQLTests\Doctrine\Blog\Model\Special\CompositeIdentifier` cannot be used as input type.');
40+
$this->entityIDTypeFactory->create(CompositeIdentifier::class, 'foo');
41+
}
42+
}

tests/MappingDriverChainAdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use GraphQL\Doctrine\Factory\MetadataReader\MappingDriverChainAdapter;
1212
use GraphQLTests\Doctrine\Blog\Model\Post;
1313

14-
class MappingDriverChainAdapterTest extends \PHPUnit\Framework\TestCase
14+
final class MappingDriverChainAdapterTest extends \PHPUnit\Framework\TestCase
1515
{
1616
use TypesTrait {
1717
setUp as typeSetup;

0 commit comments

Comments
 (0)