Skip to content

Commit

Permalink
Merge pull request API-Skeletons#70 from TomHAnderson/feature/orm3
Browse files Browse the repository at this point in the history
Feature/orm3
  • Loading branch information
TomHAnderson authored Feb 24, 2024
2 parents 4651fe4 + ae124f5 commit 3435c85
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ GraphQL Type Driver for Doctrine ORM
[![Total Downloads](https://poser.pugx.org/api-skeletons/doctrine-orm-graphql/downloads)](//packagist.org/packages/api-skeletons/doctrine-orm-graphql)
[![License](https://poser.pugx.org/api-skeletons/doctrine-orm-graphql/license)](//packagist.org/packages/api-skeletons/doctrine-orm-graphql)

* 2024-02-24 - Support for ORM version 3
* 2023-12-12 - New version 9.0 is released dropping support for PHP 8.0

This library provides a GraphQL driver for Doctrine ORM version 2 for use with [webonyx/graphql-php](https://github.com/webonyx/graphql-php).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^8.1",
"doctrine/orm": "^2.11",
"doctrine/orm": "^2.18 || ^3.0",
"doctrine/doctrine-laminas-hydrator": "^3.2",
"webonyx/graphql-php": "^v15.0",
"psr/container": "^1.1||^2.0",
Expand Down
8 changes: 4 additions & 4 deletions src/Filter/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use ApiSkeletons\Doctrine\ORM\GraphQL\Type\Entity;
use ApiSkeletons\Doctrine\ORM\GraphQL\Type\TypeManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use GraphQL\Type\Definition\InputObjectType as GraphQLInputObjectType;
use GraphQL\Type\Definition\Type;
use League\Event\EventDispatcher;
Expand Down Expand Up @@ -167,9 +167,9 @@ protected function addAssociations(Entity $targetEntity, string $typeName, array

if (
in_array($associationMetadata['type'], [
ClassMetadataInfo::TO_MANY,
ClassMetadataInfo::MANY_TO_MANY,
ClassMetadataInfo::ONE_TO_MANY,
ClassMetadata::TO_MANY,
ClassMetadata::MANY_TO_MANY,
ClassMetadata::ONE_TO_MANY,
])
|| ! in_array(Filters::EQ, $allowedFilters)
) {
Expand Down
6 changes: 4 additions & 2 deletions src/Resolve/FieldResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use ApiSkeletons\Doctrine\ORM\GraphQL\Config;
use ApiSkeletons\Doctrine\ORM\GraphQL\Type\Entity;
use ApiSkeletons\Doctrine\ORM\GraphQL\Type\TypeManager;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\ResolveInfo;

Expand Down Expand Up @@ -39,7 +39,9 @@ public function __invoke(mixed $source, mixed $args, mixed $context, ResolveInfo
assert(is_object($source), 'A non-object was passed to the FieldResolver. '
. 'Verify you\'re wrapping your Doctrine GraohQL type() call in a connection.');

$entityClass = ClassUtils::getRealClass($source::class);
$defaultProxyClassNameResolver = new DefaultProxyClassNameResolver();

$entityClass = $defaultProxyClassNameResolver->getClass($source);
$splObjectHash = spl_object_hash($source);

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Resolve/ResolveCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use Closure;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
use GraphQL\Type\Definition\ResolveInfo;
use League\Event\EventDispatcher;

Expand Down Expand Up @@ -44,7 +44,8 @@ public function get(Entity $entity): Closure
$fieldResolver = $this->fieldResolver;
$collection = $fieldResolver($source, $args, $context, $info);

$entityClassName = ClassUtils::getRealClass($source::class);
$defaultProxyClassNameResolver = new DefaultProxyClassNameResolver();
$entityClassName = $defaultProxyClassNameResolver->getClass($source);

$targetClassName = (string) $this->entityManager->getMetadataFactory()
->getMetadataFor($entityClassName)
Expand Down
8 changes: 4 additions & 4 deletions src/Type/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use ArrayObject;
use Closure;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\ObjectType;
Expand Down Expand Up @@ -180,9 +180,9 @@ protected function addAssociations(array &$fields): void
$associationMetadata = $classMetadata->getAssociationMapping($associationName);
if (
in_array($associationMetadata['type'], [
ClassMetadataInfo::ONE_TO_ONE,
ClassMetadataInfo::MANY_TO_ONE,
ClassMetadataInfo::TO_ONE,
ClassMetadata::ONE_TO_ONE,
ClassMetadata::MANY_TO_ONE,
ClassMetadata::TO_ONE,
])
) {
$targetEntity = $associationMetadata['targetEntity'];
Expand Down
19 changes: 11 additions & 8 deletions test/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

use DateTime;
use DateTimeImmutable;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;

Expand All @@ -28,23 +29,25 @@ abstract class AbstractTest extends TestCase
public function setUp(): void
{
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = Setup::createAttributeMetadataConfiguration([__DIR__ . '/Entity'], $isDevMode);
$config = ORMSetup::createAttributeMetadataConfiguration(
paths: [__DIR__ . '/Entity'],
isDevMode: true,
);

// database configuration parameters
$conn = [
// database connection
$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'memory' => true,
];
]);

if (! Type::hasType('uuid')) {
Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');
}

// obtaining the entity manager
$this->entityManager = EntityManager::create($conn, $config);
$this->entityManager = new EntityManager($connection, $config);
$tool = new SchemaTool($this->entityManager);
$res = $tool->createSchema($this->entityManager->getMetadataFactory()->getAllMetadata());
$tool->createSchema($this->entityManager->getMetadataFactory()->getAllMetadata());

$this->populateData();
}
Expand Down
33 changes: 30 additions & 3 deletions test/Feature/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,36 @@ public function testUseHydratorCache(): void
]);

$query = '{
artist (filter: { name: { contains: "dead" } })
{ edges { node { id name performances { edges { node { venue recordings { edges { node { source } } } } } } } } }
}';
artist (
filter: {
name: {
contains: "dead"
}
}
) {
edges {
node {
id
name
performances {
edges {
node {
venue
recordings {
edges {
node {
source
}
}
}
}
}
}
}
}
}
}
';

$result = GraphQL::executeQuery($schema, $query);
$output = $result->toArray();
Expand Down

0 comments on commit 3435c85

Please sign in to comment.