Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/GraphQl/Type/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't you use an operation name instead of adding a flag?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? Not sure to understand your point sorry.


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())
Expand Down
67 changes: 48 additions & 19 deletions src/Metadata/Resource/ResourceMetadataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Operations;

/**
* @experimental
Expand All @@ -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])) {
Expand All @@ -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;
}
}

Expand All @@ -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
*/
Expand Down