Skip to content

Commit

Permalink
perf(metadata): get graphql operation when available (#4522)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 25, 2021
1 parent e9d0139 commit 1a2ba1d
Show file tree
Hide file tree
Showing 32 changed files with 62 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Odm/State/CollectionProvider.php
Expand Up @@ -63,7 +63,7 @@ public function provide(string $resourceClass, array $identifiers = [], ?string

$resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
try {
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
} catch (OperationNotFoundException $e) {
$attribute = $resourceMetadata->getOperation(null, true)->getExtraProperties()['doctrine_mongodb'] ?? [];
Expand Down
Expand Up @@ -78,7 +78,7 @@ public function getCollection(string $resourceClass, string $operationName = nul

$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
} catch (OperationNotFoundException $e) {
$attribute = $resourceMetadata->getOperation(null, true)->getExtraProperties()['doctrine_mongodb'] ?? [];
Expand Down
Expand Up @@ -58,7 +58,7 @@ public function applyToCollection(Builder $aggregationBuilder, string $resourceC
{
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);
$resourceFilters = $operation->getFilters();
} catch (OperationNotFoundException $e) {
$resourceFilters = $resourceMetadata->getOperation(null, true)->getFilters();
Expand Down
Expand Up @@ -72,7 +72,7 @@ public function applyToCollection(Builder $aggregationBuilder, string $resourceC
} else {
$metadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$defaultOrder = isset($context['graphql_operation_name']) ? $metadata->getGraphQlOperation($operationName)->getOrder() : $metadata->getOperation($operationName)->getOrder();
$defaultOrder = $metadata->getOperation($operationName)->getOrder();
} catch (OperationNotFoundException $e) {
$defaultOrder = $metadata->getOperation(null, true)->getOrder();
}
Expand Down
Expand Up @@ -125,7 +125,7 @@ public function getResult(Builder $aggregationBuilder, string $resourceClass, st

$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
} catch (OperationNotFoundException $e) {
$attribute = $resourceMetadata->getOperation(null, true)->getExtraProperties()['doctrine_mongodb'] ?? [];
Expand Down
Expand Up @@ -98,7 +98,7 @@ public function getSubresource(string $resourceClass, array $identifiers, array

$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
} catch (OperationNotFoundException $e) {
$attribute = $resourceMetadata->getOperation()->getExtraProperties()['doctrine_mongodb'] ?? [];
Expand Down
Expand Up @@ -121,7 +121,7 @@ private function apply(bool $collection, QueryBuilder $queryBuilder, QueryNameGe
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
$resourceMetadataCollection = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadataCollection->getGraphQlOperation($operationName) : $resourceMetadataCollection->getOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);
$forceEager = $operation->getForceEager() ?? $this->forceEager;
$fetchPartial = $operation->getFetchPartial() ?? $this->fetchPartial;
} catch (OperationNotFoundException $e) {
Expand Down
Expand Up @@ -65,7 +65,7 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
$resourceMetadataCollection = $this->resourceMetadataFactory->create($resourceClass);
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadataCollection->getGraphQlOperation($operationName) : $resourceMetadataCollection->getOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);
$forceEager = $operation->getForceEager() ?? $this->forceEager;
} catch (OperationNotFoundException $e) {
// In some cases the operation may not exist
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Doctrine/Orm/Extension/FilterExtension.php
Expand Up @@ -70,7 +70,7 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true);
} else {
try {
$operation = $context['operation'] ?? (isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName));
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
$resourceFilters = $operation->getFilters();
} catch (OperationNotFoundException $e) {
// In some cases the operation may not exist
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Doctrine/Orm/Extension/OrderExtension.php
Expand Up @@ -70,7 +70,7 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
$resourceMetadataCollection = $this->resourceMetadataFactory->create($resourceClass);
try {
$defaultOrder = isset($context['graphql_operation_name']) ? ($resourceMetadataCollection->getGraphQlOperation($operationName)->getOrder() ?? []) : ($resourceMetadataCollection->getOperation($operationName)->getOrder() ?? []);
$defaultOrder = $resourceMetadataCollection->getOperation($operationName)->getOrder() ?? [];
} catch (OperationNotFoundException $e) {
// In some cases the operation may not exist
$defaultOrder = [];
Expand Down
Expand Up @@ -242,7 +242,7 @@ private function getPagination(QueryBuilder $queryBuilder, string $resourceClass
}
} elseif ($resourceMetadata instanceof ResourceMetadataCollection) {
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);
$itemsPerPage = $operation->getPaginationItemsPerPage();
if ($operation->getPaginationClientItemsPerPage()) {
$maxItemsPerPage = $operation->getPaginationMaximumItemsPerPage() ?? $this->maximumItemPerPage;
Expand Down Expand Up @@ -373,7 +373,7 @@ private function shouldDoctrinePaginatorFetchJoinCollection(QueryBuilder $queryB
$fetchJoinCollection = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_fetch_join_collection', null, true);
} elseif ($resourceMetadata instanceof ResourceMetadataCollection) {
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);
$fetchJoinCollection = $operation->getPaginationFetchJoinCollection();
} catch (OperationNotFoundException $e) {
// In some cases the operation may not exist
Expand Down Expand Up @@ -427,7 +427,7 @@ private function shouldDoctrinePaginatorUseOutputWalkers(QueryBuilder $queryBuil
$useOutputWalkers = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_use_output_walkers', null, true);
} elseif ($resourceMetadata instanceof ResourceMetadataCollection) {
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);
$useOutputWalkers = $operation->getPaginationUseOutputWalkers();
} catch (OperationNotFoundException $e) {
// In some cases the operation may not exist
Expand Down
10 changes: 5 additions & 5 deletions src/Core/DataProvider/Pagination.php
Expand Up @@ -136,7 +136,7 @@ public function getLimit(string $resourceClass = null, string $operationName = n
$clientLimit = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $clientLimit, true);
} else {
try {
$operation = $graphql ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);
$limit = $operation->getPaginationItemsPerPage() ?? $limit;
$clientLimit = $operation->getPaginationClientItemsPerPage() ?? $clientLimit;
} catch (OperationNotFoundException $e) {
Expand Down Expand Up @@ -170,7 +170,7 @@ public function getLimit(string $resourceClass = null, string $operationName = n
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_maximum_items_per_page', $maxItemsPerPage ?? $this->options['maximum_items_per_page'], true);
} elseif ($resourceMetadata instanceof ResourceMetadataCollection) {
try {
$operation = $graphql ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);
$maxItemsPerPage = $operation->getPaginationMaximumItemsPerPage() ?? $this->options['maximum_items_per_page'];
} catch (OperationNotFoundException $e) {
$maxItemsPerPage = $this->options['maximum_items_per_page'];
Expand Down Expand Up @@ -246,7 +246,7 @@ public function getGraphQlPaginationType(string $resourceClass, string $operatio
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

if ($resourceMetadata instanceof ResourceMetadataCollection) {
$operation = $resourceMetadata->getGraphQlOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);

return $operation->getPaginationType() ?? 'cursor';
}
Expand All @@ -272,7 +272,7 @@ private function getEnabled(array $context, string $resourceClass = null, string

if ($resourceMetadata instanceof ResourceMetadataCollection) {
try {
$operation = isset($context['graphql_operation_name']) ? $resourceMetadata->getGraphQlOperation($operationName) : $resourceMetadata->getOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);
$enabled = ($partial ? $operation->getPaginationPartial() : $operation->getPaginationEnabled()) ?? $enabled;
$clientEnabled = ($partial ? $operation->getPaginationClientPartial() : $operation->getPaginationClientEnabled()) ?? $clientEnabled;
} catch (OperationNotFoundException $e) {
Expand Down Expand Up @@ -300,7 +300,7 @@ private function getGraphQlEnabled(?string $resourceClass, ?string $operationNam
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

if ($resourceMetadata instanceof ResourceMetadataCollection) {
$operation = $resourceMetadata->getGraphQlOperation($operationName);
$operation = $resourceMetadata->getOperation($operationName);

return $operation->getPaginationEnabled() ?? $enabled;
}
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Factory/CollectionResolverFactory.php
Expand Up @@ -80,7 +80,7 @@ public function __invoke(?string $resourceClass = null, ?string $rootClass = nul

$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
try {
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);
$queryResolverId = $operation->getResolver();
} catch (OperationNotFoundException $e) {
$queryResolverId = null;
Expand Down
Expand Up @@ -99,7 +99,7 @@ public function __invoke(?string $resourceClass = null, ?string $rootClass = nul
$item = ($this->deserializeStage)($item, $resourceClass, $operationName, $resolverContext);

$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);

$mutationResolverId = $operation->getResolver();
if (null !== $mutationResolverId) {
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Factory/ItemResolverFactory.php
Expand Up @@ -71,7 +71,7 @@ public function __invoke(?string $resourceClass = null, ?string $rootClass = nul

$resourceClass = $this->getResourceClass($item, $resourceClass);
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);

$queryResolverId = $operation->getResolver();
if (null !== $queryResolverId) {
Expand Down
Expand Up @@ -74,7 +74,7 @@ public function __invoke(?string $resourceClass = null, ?string $rootClass = nul
$subscriptionId = $this->subscriptionManager->retrieveSubscriptionId($resolverContext, $result);

$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);

if ($subscriptionId && ($mercure = $operation->getMercure())) {
if (!$this->mercureSubscriptionIriGenerator) {
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Stage/DeserializeStage.php
Expand Up @@ -43,7 +43,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource
public function __invoke($objectToPopulate, string $resourceClass, string $operationName, array $context)
{
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);
if (!($operation->canDeserialize() ?? true)) {
return $objectToPopulate;
}
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Stage/ReadStage.php
Expand Up @@ -62,7 +62,7 @@ public function __invoke(?string $resourceClass, ?string $rootClass, string $ope
{
$operation = null;
try {
$operation = $resourceClass ? $this->resourceMetadataCollectionFactory->create($resourceClass)->getGraphQlOperation($operationName) : null;
$operation = $resourceClass ? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($operationName) : null;
} catch (OperationNotFoundException $e) {
// ReadStage may be invoked without an existing operation
}
Expand Down
Expand Up @@ -39,7 +39,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource
public function __invoke(string $resourceClass, string $operationName, array $context): void
{
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);
$isGranted = $operation->getSecurityPostDenormalize();

if (null !== $isGranted && null === $this->resourceAccessChecker) {
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Stage/SecurityPostValidationStage.php
Expand Up @@ -40,7 +40,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource
public function __invoke(string $resourceClass, string $operationName, array $context): void
{
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);
$isGranted = $operation->getSecurityPostValidation();

if (null !== $isGranted && null === $this->resourceAccessChecker) {
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Stage/SecurityStage.php
Expand Up @@ -39,7 +39,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource
public function __invoke(string $resourceClass, string $operationName, array $context): void
{
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);
$isGranted = $operation->getSecurity();

if (null !== $isGranted && null === $this->resourceAccessChecker) {
Expand Down
12 changes: 3 additions & 9 deletions src/GraphQl/Resolver/Stage/SerializeStage.php
Expand Up @@ -56,18 +56,12 @@ public function __invoke($itemOrCollection, string $resourceClass, string $opera
$isSubscription = $context['is_subscription'];

$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
try {
$shortName = $resourceMetadataCollection->getGraphQlOperation($operationName)->getShortName();
} catch (OperationNotFoundException $e) {
$shortName = $resourceMetadataCollection->getOperation()->getShortName();
}

$operation = null;

try {
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
$operation = $resourceMetadataCollection->getOperation($operationName);
$shortName = $operation->getShortName();
} catch (OperationNotFoundException $e) {
// In some cases the operation may not exist
$shortName = $resourceMetadataCollection->getOperation()->getShortName();
}

if ($operation && !($operation->canSerialize() ?? true)) {
Expand Down

0 comments on commit 1a2ba1d

Please sign in to comment.