diff --git a/src/GraphQl/Type/FieldsBuilder.php b/src/GraphQl/Type/FieldsBuilder.php index db68a307a96..acfef25d546 100644 --- a/src/GraphQl/Type/FieldsBuilder.php +++ b/src/GraphQl/Type/FieldsBuilder.php @@ -522,7 +522,10 @@ private function convertType(Type $type, bool $input, Operation $rootOperation, } if ($this->typeBuilder->isCollection($type)) { - return $this->pagination->isGraphQlEnabled($rootOperation) && !$input ? $this->typeBuilder->getResourcePaginatedCollectionType($graphqlType, $resourceClass, $rootOperation) : GraphQLType::listOf($graphqlType); + $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass); + $resolverOperation = $resourceMetadataCollection->getOperation(null, true, false, true); + + return $this->pagination->isGraphQlEnabled($resolverOperation) && !$input ? $this->typeBuilder->getResourcePaginatedCollectionType($graphqlType, $resourceClass, $rootOperation) : GraphQLType::listOf($graphqlType); } return $forceNullable || !$graphqlType instanceof NullableType || $type->isNullable() || ($rootOperation instanceof Mutation && 'update' === $rootOperation->getName()) diff --git a/src/Metadata/Resource/ResourceMetadataCollection.php b/src/Metadata/Resource/ResourceMetadataCollection.php index 7590c074fb6..a5d67c9d7c9 100644 --- a/src/Metadata/Resource/ResourceMetadataCollection.php +++ b/src/Metadata/Resource/ResourceMetadataCollection.php @@ -18,6 +18,7 @@ use ApiPlatform\Metadata\CollectionOperationInterface; use ApiPlatform\Metadata\HttpOperation; use ApiPlatform\Metadata\Operation; +use ApiPlatform\Metadata\Operations; /** * @experimental @@ -35,7 +36,7 @@ public function __construct(string $resourceClass, array $input = []) parent::__construct($input); } - public function getOperation(?string $operationName = null, bool $forceCollection = false, bool $httpOperation = false): Operation + public function getOperation(?string $operationName = null, bool $forceCollection = false, bool $httpOperation = false, bool $priorizeGraphQl = false): Operation { $operationName = $operationName ?? ''; if (isset($this->operationCache[$operationName])) { @@ -53,29 +54,21 @@ public function getOperation(?string $operationName = null, bool $forceCollectio /** @var ApiResource $metadata */ $metadata = $it->current(); - foreach ($metadata->getOperations() ?? [] as $name => $operation) { - $isCollection = $operation instanceof CollectionOperationInterface; - if ('' === $operationName && \in_array($operation->getMethod() ?? HttpOperation::METHOD_GET, [HttpOperation::METHOD_GET, HttpOperation::METHOD_OPTIONS, HttpOperation::METHOD_HEAD], true) && ($forceCollection ? $isCollection : !$isCollection)) { - return $this->operationCache[$operationName] = $operation; - } - - if ($name === $operationName) { - return $this->operationCache[$operationName] = $operation; - } - - if ($operation->getUriTemplate() === $operationName) { - return $this->operationCache[$operationName] = $operation; + if ($priorizeGraphQl && ([] !== $graphQlOperations = ($metadata->getGraphQlOperations() ?? []))) { + if (null !== $graphQlOperation = $this->findGraphQlOperation($graphQlOperations, $operationName, $forceCollection, $httpOperation)) { + return $graphQlOperation; } } - foreach ($metadata->getGraphQlOperations() ?? [] as $name => $operation) { - $isCollection = $operation instanceof CollectionOperationInterface; - if ('' === $operationName && ($forceCollection ? $isCollection : !$isCollection) && false === $httpOperation) { - return $this->operationCache['graphql_'.$operationName] = $operation; + if (null !== $operations = $metadata->getOperations()) { + if (null !== $operation = $this->findHttpOperation($operations, $operationName, $forceCollection, $httpOperation)) { + return $operation; } + } - if ($name === $operationName) { - return $this->operationCache['graphql_'.$operationName] = $operation; + if (!$priorizeGraphQl && ([] !== $graphQlOperations = ($metadata->getGraphQlOperations() ?? []))) { + if (null !== $graphQlOperation = $this->findGraphQlOperation($graphQlOperations, $operationName, $forceCollection, $httpOperation)) { + return $graphQlOperation; } } @@ -85,6 +78,42 @@ public function getOperation(?string $operationName = null, bool $forceCollectio $this->handleNotFound($operationName, $metadata); } + private function findHttpOperation(Operations $operations, string $operationName, bool $forceCollection, bool $httpOperation): ?Operation + { + foreach ($operations as $name => $operation) { + $isCollection = $operation instanceof CollectionOperationInterface; + if ('' === $operationName && \in_array($operation->getMethod() ?? HttpOperation::METHOD_GET, [HttpOperation::METHOD_GET, HttpOperation::METHOD_OPTIONS, HttpOperation::METHOD_HEAD], true) && ($forceCollection ? $isCollection : !$isCollection)) { + return $this->operationCache[$operationName] = $operation; + } + + if ($name === $operationName) { + return $this->operationCache[$operationName] = $operation; + } + + if ($operation->getUriTemplate() === $operationName) { + return $this->operationCache[$operationName] = $operation; + } + } + + return null; + } + + private function findGraphQlOperation(array $operations, string $operationName, bool $forceCollection, bool $httpOperation): ?Operation + { + foreach ($operations as $name => $operation) { + $isCollection = $operation instanceof CollectionOperationInterface; + if ('' === $operationName && ($forceCollection ? $isCollection : !$isCollection) && false === $httpOperation) { + return $this->operationCache['graphql_'.$operationName] = $operation; + } + + if ($name === $operationName) { + return $this->operationCache['graphql_'.$operationName] = $operation; + } + } + + return null; + } + /** * @throws OperationNotFoundException */