Skip to content

Commit

Permalink
Merge pull request BitBagCommerce#32 from tgrochowski/feature/extenda…
Browse files Browse the repository at this point in the history
…ble-data-providers

Add context aware extension interface chacking to pass context to ite…
  • Loading branch information
tbuczen authored Nov 19, 2021
2 parents 312293e + e5ed6ca commit c988a39
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/DataProvider/CountryCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace BitBag\SyliusGraphqlPlugin\DataProvider;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
Expand Down Expand Up @@ -54,7 +55,11 @@ public function getCollection(string $resourceClass, string $operationName = nul

/** @var QueryCollectionExtensionInterface $extension */
foreach ($this->collectionExtensions as $extension) {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName);
if ($extension instanceof ContextAwareQueryCollectionExtensionInterface) {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName, $context);
} else {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName);
}

if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
Expand Down
8 changes: 6 additions & 2 deletions src/DataProvider/ShippingMethodCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public function getCollection(string $resourceClass, string $operationName = nul
{
$queryBuilder = $this->shippingMethodRepository->createQueryBuilder('o');

/** @var ContextAwareQueryCollectionExtensionInterface $extension */
/** @var QueryCollectionExtensionInterface $extension */
foreach ($this->collectionExtensions as $extension) {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName, $context);
if ($extension instanceof ContextAwareQueryCollectionExtensionInterface) {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName, $context);
} else {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName);
}

if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
Expand Down
7 changes: 6 additions & 1 deletion src/DataProvider/TaxonCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace BitBag\SyliusGraphqlPlugin\DataProvider;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\PaginationExtension;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
Expand Down Expand Up @@ -74,7 +75,11 @@ public function getCollection(string $resourceClass, string $operationName = nul

/** @var QueryCollectionExtensionInterface $extension */
foreach ($this->collectionExtensions as $extension) {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName);
if ($extension instanceof ContextAwareQueryCollectionExtensionInterface) {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName, $context);
} else {
$extension->applyToCollection($queryBuilder, $this->queryNameGenerator, $resourceClass, $operationName);
}

if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
/** @experimental */
final class InterfaceExtractorResourceMetadataFactory implements ResourceMetadataFactoryInterface
{
const GRAPHQL_PROPERTIES = [
public const GRAPHQL_PROPERTIES = [
'shortName',
'description',
'iri',
'itemOperations',
'collectionOperations',
'subresourceOperations',
'graphql',
'attributes'
'attributes',
];

private ?ResourceMetadataFactoryInterface $decoratedResourceMetadataFactory;
Expand All @@ -48,7 +48,8 @@ public function create(string $resourceClass): ResourceMetadata
if (null !== $this->decoratedResourceMetadataFactory) {
try {
$parentResourceMetadata = $this->decoratedResourceMetadataFactory->create($resourceClass);
} catch (ResourceClassNotFoundException $resourceNotFoundException) {}
} catch (ResourceClassNotFoundException $resourceNotFoundException) {
}
}

if (null !== $parentResourceMetadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function create(string $resourceClass): ResourceMetadata
if (null !== $this->decoratedResourceMetadataFactory) {
try {
$parentResourceMetadata = $this->decoratedResourceMetadataFactory->create($resourceClass);
} catch (ResourceClassNotFoundException $resourceNotFoundException) {}
} catch (ResourceClassNotFoundException $resourceNotFoundException) {
}
}

if (null !== $parentResourceMetadata) {
Expand Down

0 comments on commit c988a39

Please sign in to comment.