Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
859 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Link | ||
|
||
* Status: accepted | ||
* Deciders: @dunglas, @soyuka, @alanpoulain | ||
|
||
Implementation: [#4536][pull/4536] | ||
|
||
## Context and Problem Statement | ||
|
||
The [URI Variables](0003-uri-variables.md) ADR introduces a new `UriVariable` POPO. | ||
In GraphQL, having URI variables make no sense: this object needs either an alias or needs to be named differently. | ||
|
||
## Considered Options | ||
|
||
* Create a `Traverser` alias for GraphQL. | ||
* Rename `UriVariable` to `Link`. | ||
|
||
## Decision Outcome | ||
|
||
We chose to rename `UriVariable` to `Link` in order to simplify the codebase. | ||
However the `uriVariables` parameter in the REST operations will not be renamed since it makes sense to have this name. | ||
GraphQL operations don't need to have links at the operation level, a `Link` attribute on the property will be used instead if necessary (the main use case is when an `inverseProperty` is necessary). | ||
|
||
### Example | ||
|
||
```php | ||
<?php | ||
|
||
#[Query] | ||
#[Get('/companies/{companyId}/employees', uriVariables: [ | ||
'companyId' => new Link( | ||
targetClass: Company::class, | ||
identifiers: ['id'], | ||
property: 'company' | ||
) | ||
])] | ||
class Employee | ||
{ | ||
public $id; | ||
|
||
public Company $company; | ||
} | ||
``` | ||
|
||
```php | ||
<?php | ||
|
||
#[Query] | ||
#[Get] | ||
class Company | ||
{ | ||
public $id; | ||
|
||
/** @var Employee[] */ | ||
#[Link(inverseProperty: 'company')] | ||
public iterable $employees; | ||
} | ||
``` | ||
|
||
```graphql | ||
{ | ||
companies(id: "/companies/2") { | ||
employees { | ||
edges { | ||
node { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
[pull/4536]: https://github.com/api-platform/core/pull/4536 "Link implementation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Bridge\Doctrine\Orm\State; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator; | ||
use ApiPlatform\Exception\RuntimeException; | ||
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation; | ||
use ApiPlatform\Metadata\Link; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Doctrine\Persistence\Mapping\ClassMetadata; | ||
|
||
trait LinksHandlerTrait | ||
{ | ||
private function handleLinks(QueryBuilder $queryBuilder, array $identifiers, QueryNameGenerator $queryNameGenerator, array $context, string $resourceClass, ?string $operationName = null): void | ||
{ | ||
$operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($operationName); | ||
$manager = $this->managerRegistry->getManagerForClass($resourceClass); | ||
$doctrineClassMetadata = $manager->getClassMetadata($resourceClass); | ||
$alias = $queryBuilder->getRootAliases()[0]; | ||
|
||
$links = $operation instanceof GraphQlOperation ? $operation->getLinks() : $operation->getUriVariables(); | ||
|
||
if ($linkClass = $context['linkClass'] ?? false) { | ||
foreach ($links as $link) { | ||
if ($linkClass === $link->getTargetClass()) { | ||
foreach ($identifiers as $identifier => $value) { | ||
$this->applyLink($queryBuilder, $queryNameGenerator, $doctrineClassMetadata, $alias, $link, $identifier, $value); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
$operation = $this->resourceMetadataCollectionFactory->create($linkClass)->getOperation($operationName); | ||
$links = $operation instanceof GraphQlOperation ? $operation->getLinks() : $operation->getUriVariables(); | ||
foreach ($links as $link) { | ||
if ($resourceClass === $link->getTargetClass()) { | ||
$link = $link->withInverseProperty($link->getProperty())->withTargetClass($linkClass); | ||
foreach ($identifiers as $identifier => $value) { | ||
$this->applyLink($queryBuilder, $queryNameGenerator, $doctrineClassMetadata, $alias, $link, $identifier, $value); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('The class "%s" cannot be retrieved from "%s".', $resourceClass, $linkClass)); | ||
} | ||
|
||
if (!$links) { | ||
return; | ||
} | ||
|
||
foreach ($identifiers as $identifier => $value) { | ||
$link = $links[$identifier] ?? $links['id']; | ||
|
||
$this->applyLink($queryBuilder, $queryNameGenerator, $doctrineClassMetadata, $alias, $link, $identifier, $value); | ||
} | ||
} | ||
|
||
private function applyLink(QueryBuilder $queryBuilder, QueryNameGenerator $queryNameGenerator, ClassMetadata $doctrineClassMetadata, string $alias, Link $link, string $identifier, $value) | ||
{ | ||
$placeholder = ':id_'.$identifier; | ||
if ($inverseProperty = $link->getInverseProperty()) { | ||
$propertyIdentifier = $link->getIdentifiers()[0]; | ||
$joinAlias = $queryNameGenerator->generateJoinAlias($inverseProperty); | ||
|
||
$queryBuilder->join( | ||
$link->getTargetClass(), | ||
$joinAlias, | ||
'with', | ||
"$alias.$propertyIdentifier = $joinAlias.$inverseProperty" | ||
); | ||
|
||
$expression = $queryBuilder->expr()->eq( | ||
"{$joinAlias}.{$propertyIdentifier}", | ||
$placeholder | ||
); | ||
} elseif ($property = $link->getProperty()) { | ||
$propertyIdentifier = $link->getIdentifiers()[0]; | ||
$joinAlias = $queryNameGenerator->generateJoinAlias($property); | ||
|
||
$queryBuilder->join( | ||
"$alias.$property", | ||
$joinAlias, | ||
); | ||
|
||
$expression = $queryBuilder->expr()->eq( | ||
"{$joinAlias}.{$propertyIdentifier}", | ||
$placeholder | ||
); | ||
} else { | ||
$expression = $queryBuilder->expr()->eq( | ||
"{$alias}.{$identifier}", $placeholder | ||
); | ||
} | ||
$queryBuilder->andWhere($expression); | ||
$queryBuilder->setParameter($placeholder, $value, $doctrineClassMetadata->getTypeOfField($identifier)); | ||
} | ||
} |
75 changes: 0 additions & 75 deletions
75
src/Bridge/Doctrine/Orm/State/UriVariablesHandlerTrait.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.