From bddfbfe1cc54fd9ad5edc62888d968a6b1d6abe4 Mon Sep 17 00:00:00 2001 From: Konrad Oboza Date: Thu, 28 Mar 2024 14:30:22 +0100 Subject: [PATCH] IBX-8006: Fixed matrix field resolving based on content's origin (#43) * IBX-8006: Fixed matrix field resolving based on content's origin * added thrown exceptions --- composer.json | 9 +++-- .../Resources/config/services/graphql.yaml | 8 +++- src/lib/GraphQL/FieldValueResolver.php | 37 +++++++++++++++++-- .../ContentResolvingStrategyInterface.php | 18 +++++++++ .../Strategy/ItemContentResolvingStrategy.php | 26 +++++++++++++ 5 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 src/lib/GraphQL/Strategy/ContentResolvingStrategyInterface.php create mode 100644 src/lib/GraphQL/Strategy/ItemContentResolvingStrategy.php diff --git a/composer.json b/composer.json index 4f30d92..19509e5 100644 --- a/composer.json +++ b/composer.json @@ -8,10 +8,10 @@ }, "autoload": { "psr-4": { - "EzSystems\\EzPlatformMatrixFieldtypeBundle\\": "src/bundle/", - "EzSystems\\EzPlatformMatrixFieldtype\\": "src/lib/", "Ibexa\\FieldTypeMatrix\\": "src/lib/", - "Ibexa\\Bundle\\FieldTypeMatrix\\": "src/bundle/" + "Ibexa\\Bundle\\FieldTypeMatrix\\": "src/bundle/", + "EzSystems\\EzPlatformMatrixFieldtypeBundle\\": "src/bundle/", + "EzSystems\\EzPlatformMatrixFieldtype\\": "src/lib/" } }, "autoload-dev": { @@ -57,5 +57,8 @@ "branch-alias": { "dev-main": "4.5.x-dev" } + }, + "config": { + "allow-plugins": false } } diff --git a/src/bundle/Resources/config/services/graphql.yaml b/src/bundle/Resources/config/services/graphql.yaml index b755d1a..d0ba7eb 100644 --- a/src/bundle/Resources/config/services/graphql.yaml +++ b/src/bundle/Resources/config/services/graphql.yaml @@ -25,5 +25,11 @@ services: - { name: ibexa.graphql.field_type.input.handler, fieldtype: ezmatrix } Ibexa\FieldTypeMatrix\GraphQL\FieldValueResolver: + arguments: + $strategies: !tagged_iterator ibexa.graphql.field_type.matrix_resolver.content.strategy + tags: + - { name: overblog_graphql.resolver, alias: "MatrixFieldValue", method: "resolveMatrixFieldValue" } + + Ibexa\FieldTypeMatrix\GraphQL\Strategy\ItemContentResolvingStrategy: tags: - - {name: overblog_graphql.resolver, alias: "MatrixFieldValue", method: "resolveMatrixFieldValue"} + - { name: ibexa.graphql.field_type.matrix_resolver.content.strategy, priority: -20 } diff --git a/src/lib/GraphQL/FieldValueResolver.php b/src/lib/GraphQL/FieldValueResolver.php index f6c419f..1495515 100644 --- a/src/lib/GraphQL/FieldValueResolver.php +++ b/src/lib/GraphQL/FieldValueResolver.php @@ -8,17 +8,48 @@ namespace Ibexa\FieldTypeMatrix\GraphQL; +use Ibexa\Core\Base\Exceptions\BadStateException; use Ibexa\FieldTypeMatrix\FieldType\Value\RowsCollection; -use Ibexa\GraphQL\Value\Item; class FieldValueResolver { - public function resolveMatrixFieldValue(Item $item, string $fieldDefIdentifier): RowsCollection + /** @var iterable<\Ibexa\FieldTypeMatrix\GraphQL\Strategy\ContentResolvingStrategyInterface> */ + private iterable $strategies; + + /** + * @param iterable<\Ibexa\FieldTypeMatrix\GraphQL\Strategy\ContentResolvingStrategyInterface> $strategies + */ + public function __construct(iterable $strategies) + { + $this->strategies = $strategies; + } + + /** + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + */ + public function resolveMatrixFieldValue(object $item, string $fieldDefIdentifier): RowsCollection { $silentRows = []; + $content = null; + + foreach ($this->strategies as $strategy) { + if (!$strategy->supports($item)) { + continue; + } + + $content = $strategy->resolveContent($item); + } + + if ($content === null) { + throw new BadStateException( + '$item', + 'GraphQL item cannot be resolved to a content.' + ); + } /** @var \Ibexa\FieldTypeMatrix\FieldType\Value\RowsCollection $rows $rows */ - $rows = $item->getContent()->getFieldValue($fieldDefIdentifier)->getRows(); + $rows = $content->getFieldValue($fieldDefIdentifier)->getRows(); foreach ($rows as $row) { $silentRows[] = new SilentRow($row->getCells()); } diff --git a/src/lib/GraphQL/Strategy/ContentResolvingStrategyInterface.php b/src/lib/GraphQL/Strategy/ContentResolvingStrategyInterface.php new file mode 100644 index 0000000..c84a7e2 --- /dev/null +++ b/src/lib/GraphQL/Strategy/ContentResolvingStrategyInterface.php @@ -0,0 +1,18 @@ +getContent(); + } + + public function supports(object $item): bool + { + return $item instanceof Item; + } +}