Skip to content

Commit

Permalink
start fixing mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Aug 6, 2021
1 parent ae0d869 commit 2a173fb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Exception\OperationNotFoundException;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -74,7 +75,13 @@ public function getCollection(string $resourceClass, string $operationName = nul
}
}

$attribute = $this->resourceMetadataFactory->create($resourceClass)->getOperation($operationName)->getExtraProperties()['doctrine_mongodb'] ?? [];
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
} catch (OperationNotFoundException $e) {
$attribute = $resourceMetadata->getOperation(null, true)->getExtraProperties()['doctrine_mongodb'] ?? [];
}
$executeOptions = $attribute['execute_options'] ?? [];

return $aggregationBuilder->hydrate($resourceClass)->execute($executeOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Core\Api\FilterLocatorTrait;
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\FilterInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -54,7 +55,13 @@ public function __construct($resourceMetadataFactory, $filterLocator)
*/
public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = [])
{
$resourceFilters = $this->resourceMetadataFactory->create($resourceClass)->getOperation($operationName)->getFilters();
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$resourceFilters = $operation->getFilters();
} catch (OperationNotFoundException $e) {
$resourceFilters = $resourceMetadata->getOperation(null, true)->getFilters();
}

if (empty($resourceFilters)) {
return;
Expand Down
13 changes: 11 additions & 2 deletions src/Core/Bridge/Doctrine/MongoDbOdm/Extension/OrderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait;
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -59,8 +60,16 @@ public function applyToCollection(Builder $aggregationBuilder, string $resourceC
$identifiers = $classMetaData->getIdentifier();
if (null !== $this->resourceMetadataFactory) {
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
$metadata = $this->resourceMetadataFactory->create($resourceClass);
$defaultOrder = isset($context['graphql_operation_name']) ? $metadata->getGraphQlOperation($operationName)->getOrder() : $metadata->getOperation($operationName)->getOrder();
if (isset($context['operation'])) {
$defaultOrder = $context['operation']->getOrder();
} else {
$metadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$defaultOrder = isset($context['graphql_operation_name']) ? $metadata->getGraphQlOperation($operationName)->getOrder() : $metadata->getOperation($operationName)->getOrder();
} catch (OperationNotFoundException $e) {
$defaultOrder = $metadata->getOperation(null, true)->getOrder();
}
}
} else {
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Core\DataProvider\Pagination;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\DocumentManager;
Expand Down Expand Up @@ -122,7 +123,13 @@ public function getResult(Builder $aggregationBuilder, string $resourceClass, st
throw new RuntimeException(sprintf('The manager for "%s" must be an instance of "%s".', $resourceClass, DocumentManager::class));
}

$attribute = $this->resourceMetadataFactory->create($resourceClass)->getOperation($operationName)->getExtraProperties()['doctrine_mongodb'] ?? [];
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
} catch (OperationNotFoundException $e) {
$attribute = $resourceMetadata->getOperation(null, true)->getExtraProperties()['doctrine_mongodb'] ?? [];
}
$executeOptions = $attribute['execute_options'] ?? [];

return new Paginator($aggregationBuilder->execute($executeOptions), $manager->getUnitOfWork(), $resourceClass, $aggregationBuilder->getPipeline());
Expand Down
15 changes: 14 additions & 1 deletion src/Core/Bridge/Doctrine/MongoDbOdm/SubresourceDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\DocumentManager;
Expand Down Expand Up @@ -85,11 +86,23 @@ public function getSubresource(string $resourceClass, array $identifiers, array
throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class));
}

if (isset($context['identifiers'], $context['operation']) && !isset($context['property'])) {
$context['property'] = $context['operation']->getExtraProperties()['legacy_subresource_property'] ?? null;
$context['collection'] = $context['operation']->isCollection();
}

if (!isset($context['identifiers'], $context['property'])) {
throw new ResourceClassNotSupportedException('The given resource class is not a subresource.');
}

$attribute = $this->resourceMetadataFactory->create($resourceClass)->getOperation($operationName)->getExtraProperties()['doctrine_mongodb'] ?? [];
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
} catch (OperationNotFoundException $e) {
$attribute = $resourceMetadata->getOperation()->getExtraProperties()['doctrine_mongodb'] ?? [];
}

$executeOptions = $attribute['execute_options'] ?? [];

$aggregationBuilder = $this->buildAggregation($identifiers, $context, $executeOptions, $repository->createAggregationBuilder(), \count($context['identifiers']));
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Doctrine/Orm/Extension/FilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true);
} else {
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$resourceFilters = $operation->getFilters();
} catch (OperationNotFoundException $e) {
// In some cases the operation may not exist
Expand Down

0 comments on commit 2a173fb

Please sign in to comment.