From 945842d3e6c060ea9d156a2e9d08bd0485df694f Mon Sep 17 00:00:00 2001 From: Peter Philipp Date: Fri, 19 Jul 2024 14:14:11 +0200 Subject: [PATCH] chore: Generalize the element loading / sharing by introducing a trait. --- .../FieldHelper/AbstractFieldHelper.php | 8 +-- src/GraphQL/Resolver/AssetType.php | 6 +- src/GraphQL/Resolver/Base.php | 6 +- src/GraphQL/Resolver/DataObject.php | 6 +- src/GraphQL/Resolver/Element.php | 14 ++--- src/GraphQL/Traits/ElementLoaderTrait.php | 58 +++++++++++++++++++ 6 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 src/GraphQL/Traits/ElementLoaderTrait.php diff --git a/src/GraphQL/FieldHelper/AbstractFieldHelper.php b/src/GraphQL/FieldHelper/AbstractFieldHelper.php index 59c56b98..33812bf3 100644 --- a/src/GraphQL/FieldHelper/AbstractFieldHelper.php +++ b/src/GraphQL/FieldHelper/AbstractFieldHelper.php @@ -22,13 +22,14 @@ use GraphQL\Language\AST\NodeList; use GraphQL\Language\AST\SelectionSetNode; use GraphQL\Type\Definition\ResolveInfo; +use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait; use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait; use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup; use Pimcore\Model\Element\ElementInterface; abstract class AbstractFieldHelper { - use ServiceTrait; + use ServiceTrait, ElementLoaderTrait; public function __construct() { @@ -122,10 +123,7 @@ public function extractData(&$data, $container, $args, $context = [], ResolveInf if ($container instanceof ElementInterface || $container instanceof DefaultMockup) { // we have to at least add the ID and pass it around even if not requested because we need it internally // to resolve fields of linked elements (such as asset image and so on) - $data['id'] = $container->getId(); - // Register the element for downstream resolvers to avoid object - // loads. - $data[ElementInterface::class] = $container; + $data = $this->setDataElement($data, $container); } $resolveInfoArray = (array)$resolveInfo; diff --git a/src/GraphQL/Resolver/AssetType.php b/src/GraphQL/Resolver/AssetType.php index 3502d943..e49880d8 100644 --- a/src/GraphQL/Resolver/AssetType.php +++ b/src/GraphQL/Resolver/AssetType.php @@ -20,6 +20,7 @@ use Pimcore\Bundle\DataHubBundle\GraphQL\BaseDescriptor; use Pimcore\Bundle\DataHubBundle\GraphQL\ElementDescriptor; use Pimcore\Bundle\DataHubBundle\GraphQL\Service; +use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait; use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementTagTrait; use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait; use Pimcore\Bundle\DataHubBundle\WorkspaceHelper; @@ -27,7 +28,7 @@ class AssetType { - use ServiceTrait, ElementTagTrait; + use ServiceTrait, ElementTagTrait, ElementLoaderTrait; /** * @param ElementDescriptor|null $value @@ -410,8 +411,7 @@ protected function getAssetFromValue($value, $context) if (!$value instanceof ElementDescriptor) { return null; } - - $asset = Asset::getById($value['id']); + $asset = $this->loadDataElement($value, 'asset'); if (!WorkspaceHelper::checkPermission($asset, 'read')) { return null; diff --git a/src/GraphQL/Resolver/Base.php b/src/GraphQL/Resolver/Base.php index e68216c8..af3215d1 100644 --- a/src/GraphQL/Resolver/Base.php +++ b/src/GraphQL/Resolver/Base.php @@ -19,13 +19,13 @@ use GraphQL\Executor\Promise\Adapter\SyncPromise; use GraphQL\Language\AST\FieldNode; use GraphQL\Type\Definition\ResolveInfo; +use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait; use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait; -use Pimcore\Model\DataObject\AbstractObject; use Pimcore\Model\DataObject\ClassDefinition; class Base { - use ServiceTrait; + use ServiceTrait, ElementLoaderTrait; /** @var string */ protected $typeName; @@ -87,7 +87,7 @@ public function resolve($value = null, $args = [], $context = [], ResolveInfo $r /** @var \Pimcore\Bundle\DataHubBundle\GraphQL\Query\Operator\AbstractOperator $operatorImpl */ $operatorImpl = $this->getGraphQlService()->buildQueryOperator($this->typeName, $this->attributes); - $element = AbstractObject::getById($value['id']); + $element = $this->loadDataElement($value, 'object'); $valueFromOperator = $operatorImpl->getLabeledValue($element, $resolveInfo); $value = $valueFromOperator->value ?? null; diff --git a/src/GraphQL/Resolver/DataObject.php b/src/GraphQL/Resolver/DataObject.php index d009a78f..e3126c82 100644 --- a/src/GraphQL/Resolver/DataObject.php +++ b/src/GraphQL/Resolver/DataObject.php @@ -42,7 +42,7 @@ public function __construct(Service $graphQlService) */ public function resolveTag($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null) { - $object = \Pimcore\Model\DataObject::getById($value['id']); + $object = $this->loadDataElement($value, 'object'); if ($object) { $result = $this->getTags('object', $object->getId()); @@ -68,7 +68,7 @@ public function resolveIndex($value = null, $args = [], $context = [], ResolveIn return null; } - $object = \Pimcore\Model\DataObject::getById($value['id']); + $object = $this->loadDataElement($value, 'object'); if (!$object instanceof AbstractObject) { return null; @@ -91,7 +91,7 @@ public function resolveChildrenSortBy($value = null, $args = [], $context = [], return null; } - $object = \Pimcore\Model\DataObject::getById($value['id']); + $object = $this->loadDataElement($value, 'object'); if (!$object instanceof \Pimcore\Model\DataObject) { return null; diff --git a/src/GraphQL/Resolver/Element.php b/src/GraphQL/Resolver/Element.php index 4be4b9c3..a843cb7b 100644 --- a/src/GraphQL/Resolver/Element.php +++ b/src/GraphQL/Resolver/Element.php @@ -21,6 +21,7 @@ use Pimcore\Bundle\DataHubBundle\GraphQL\Exception\ClientSafeException; use Pimcore\Bundle\DataHubBundle\GraphQL\FieldHelper\AbstractFieldHelper; use Pimcore\Bundle\DataHubBundle\GraphQL\Service; +use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait; use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementTagTrait; use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait; use Pimcore\Bundle\DataHubBundle\WorkspaceHelper; @@ -29,12 +30,11 @@ use Pimcore\Model\DataObject\AbstractObject; use Pimcore\Model\Document; use Pimcore\Model\Element\ElementInterface; -use Pimcore\Model\Element\Service as ElementService; use Pimcore\Model\Property; class Element { - use ServiceTrait, ElementTagTrait; + use ServiceTrait, ElementTagTrait, ElementLoaderTrait; /** @var string */ protected $elementType; @@ -57,7 +57,7 @@ public function __construct(string $elementType, Service $graphQlService) */ public function resolveTag($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null) { - $element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']); + $element = $this->loadDataElement($value, $this->elementType); if ($element) { $result = $this->getTags('document', $element->getId()); @@ -82,7 +82,7 @@ public function resolveTag($value = null, $args = [], $context = [], ResolveInfo public function resolveProperties($value = null, array $args = [], array $context = [], ResolveInfo $resolveInfo = null) { $elementId = $value['id']; - $element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $elementId); + $element = $this->loadDataElement($value, $this->elementType); if (!$element) { throw new ClientSafeException('element ' . $this->elementType . ' ' . $elementId . ' not found'); @@ -116,7 +116,7 @@ public function resolveProperties($value = null, array $args = [], array $contex */ public function resolveParent($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null) { - $element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']); + $element = $this->loadDataElement($value, $this->elementType); if ($element) { $parent = $element->getParent(); if ($parent) { @@ -139,7 +139,7 @@ public function resolveParent($value = null, $args = [], $context = [], ResolveI */ public function resolveChildren($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null) { - $element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']); + $element = $this->loadDataElement($value, $this->elementType); if ($element) { $arguments = $this->composeArguments($args); @@ -162,7 +162,7 @@ public function resolveChildren($value = null, $args = [], $context = [], Resolv */ public function resolveSiblings($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null) { - $element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']); + $element = $this->loadDataElement($value, $this->elementType); if ($element) { $arguments = $this->composeArguments($args); diff --git a/src/GraphQL/Traits/ElementLoaderTrait.php b/src/GraphQL/Traits/ElementLoaderTrait.php new file mode 100644 index 00000000..e5769436 --- /dev/null +++ b/src/GraphQL/Traits/ElementLoaderTrait.php @@ -0,0 +1,58 @@ +getId(); + $data[ElementInterface::class . '_type'] = $element->getType(); + $data[ElementInterface::class . '_instance'] = $element; + + return $data; + } + + /** + * + * @return ElementInterface + */ + protected function loadDataElement(&$data, $type) + { + if (!isset($data[ElementInterface::class . '_instance'])) { + $data[ElementInterface::class . '_type'] = $type; + $data[ElementInterface::class . '_instance'] = ElementService::getElementById($data['id'], $type); + } + + return $data[ElementInterface::class . '_instance']; + } +}