From 0deee1ff67fdd8d0f1346e8be8701fea0a9b4822 Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Thu, 8 Sep 2022 16:34:54 +0200 Subject: [PATCH 1/3] fix(graphql): operation with disabled pagination --- src/GraphQl/Type/FieldsBuilder.php | 5 +- .../Resource/ResourceMetadataCollection.php | 68 +++++++++++++------ 2 files changed, 53 insertions(+), 20 deletions(-) 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..caea294d292 100644 --- a/src/Metadata/Resource/ResourceMetadataCollection.php +++ b/src/Metadata/Resource/ResourceMetadataCollection.php @@ -16,8 +16,10 @@ use ApiPlatform\Exception\OperationNotFoundException; use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\CollectionOperationInterface; +use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation; use ApiPlatform\Metadata\HttpOperation; use ApiPlatform\Metadata\Operation; +use ApiPlatform\Metadata\Operations; /** * @experimental @@ -35,7 +37,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 +55,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 +79,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): ?GraphQlOperation + { + 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 */ From 2de3979f9bed045c2996e9cb17540adf998b2cbc Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Thu, 8 Sep 2022 20:26:59 +0200 Subject: [PATCH 2/3] test(graphql): fixes --- src/Metadata/Resource/ResourceMetadataCollection.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Metadata/Resource/ResourceMetadataCollection.php b/src/Metadata/Resource/ResourceMetadataCollection.php index caea294d292..8dc796823b1 100644 --- a/src/Metadata/Resource/ResourceMetadataCollection.php +++ b/src/Metadata/Resource/ResourceMetadataCollection.php @@ -16,7 +16,6 @@ use ApiPlatform\Exception\OperationNotFoundException; use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\CollectionOperationInterface; -use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation; use ApiPlatform\Metadata\HttpOperation; use ApiPlatform\Metadata\Operation; use ApiPlatform\Metadata\Operations; @@ -99,7 +98,7 @@ private function findHttpOperation(Operations $operations, string $operationName return null; } - private function findGraphQlOperation(array $operations, string $operationName, bool $forceCollection, bool $httpOperation): ?GraphQlOperation + private function findGraphQlOperation(array $operations, string $operationName, bool $forceCollection, bool $httpOperation): ?Operation { foreach ($operations as $name => $operation) { $isCollection = $operation instanceof CollectionOperationInterface; From 05c0ab2558fa47b24c6e2aa80a13e7a9d11a6e45 Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Thu, 8 Sep 2022 20:41:06 +0200 Subject: [PATCH 3/3] fix(graphql): operation with disabled pagination --- src/Metadata/Resource/ResourceMetadataCollection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Metadata/Resource/ResourceMetadataCollection.php b/src/Metadata/Resource/ResourceMetadataCollection.php index 8dc796823b1..a5d67c9d7c9 100644 --- a/src/Metadata/Resource/ResourceMetadataCollection.php +++ b/src/Metadata/Resource/ResourceMetadataCollection.php @@ -54,7 +54,7 @@ public function getOperation(?string $operationName = null, bool $forceCollectio /** @var ApiResource $metadata */ $metadata = $it->current(); - if ($priorizeGraphQl && ([] !== $graphQlOperations = $metadata->getGraphQlOperations())) { + if ($priorizeGraphQl && ([] !== $graphQlOperations = ($metadata->getGraphQlOperations() ?? []))) { if (null !== $graphQlOperation = $this->findGraphQlOperation($graphQlOperations, $operationName, $forceCollection, $httpOperation)) { return $graphQlOperation; } @@ -66,7 +66,7 @@ public function getOperation(?string $operationName = null, bool $forceCollectio } } - if (!$priorizeGraphQl && ([] !== $graphQlOperations = $metadata->getGraphQlOperations())) { + if (!$priorizeGraphQl && ([] !== $graphQlOperations = ($metadata->getGraphQlOperations() ?? []))) { if (null !== $graphQlOperation = $this->findGraphQlOperation($graphQlOperations, $operationName, $forceCollection, $httpOperation)) { return $graphQlOperation; }