From 6aa7ab2ccb110366d4c58f1c4b9374ad2aeb7da3 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Fri, 22 Mar 2024 08:09:49 +0100 Subject: [PATCH 1/2] IBX-8006: Fixed matrix field resolving based on content's origin --- composer.json | 9 +++-- .../Resources/config/services/graphql.yaml | 8 ++++- src/lib/GraphQL/FieldValueResolver.php | 33 +++++++++++++++++-- .../ContentResolvingStrategyInterface.php | 18 ++++++++++ .../Strategy/ItemContentResolvingStrategy.php | 26 +++++++++++++++ 5 files changed, 87 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..6f20dde 100644 --- a/src/lib/GraphQL/FieldValueResolver.php +++ b/src/lib/GraphQL/FieldValueResolver.php @@ -8,17 +8,44 @@ 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; + } + + 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; + } +} From 9e76007746c3faf6a0d17506c8a2002e812a7891 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Fri, 22 Mar 2024 09:05:50 +0100 Subject: [PATCH 2/2] added thrown exceptions --- src/lib/GraphQL/FieldValueResolver.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/GraphQL/FieldValueResolver.php b/src/lib/GraphQL/FieldValueResolver.php index 6f20dde..1495515 100644 --- a/src/lib/GraphQL/FieldValueResolver.php +++ b/src/lib/GraphQL/FieldValueResolver.php @@ -24,6 +24,10 @@ 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 = [];