Skip to content

Commit

Permalink
chore: Generalize the element loading / sharing by introducing a trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
das-peter committed Jul 19, 2024
1 parent 10c4abd commit abcdcf0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 21 deletions.
8 changes: 3 additions & 5 deletions src/GraphQL/FieldHelper/AbstractFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/GraphQL/Resolver/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
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;
use Pimcore\Model\Asset;

class AssetType
{
use ServiceTrait, ElementTagTrait;
use ServiceTrait, ElementTagTrait, ElementLoaderTrait;

/**
* @param ElementDescriptor|null $value
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/GraphQL/Resolver/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/GraphQL/Resolver/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/GraphQL/Resolver/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand All @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);

Expand Down
58 changes: 58 additions & 0 deletions src/GraphQL/Traits/ElementLoaderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\DataHubBundle\GraphQL\Traits;


use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Element\Service as ElementService;

trait ElementLoaderTrait
{
/**
* Stores the "metadata" of a related element into the data storage which
* then is passed around.
*
* It can be used with loadDataElement() which if possible will use the
* already loaded element but will fallback to load the element using
* the id.
*
* @return array
*/
protected function setDataElement(\ArrayAccess $data, ElementInterface|DefaultMockup $element): \ArrayAccess
{
$data['id'] = $element->getId();
$data[ElementInterface::class . '_type'] = $element->getType();
$data[ElementInterface::class . '_instance'] = $element->getId();

return $data;
}

/**
*
* @return ElementInterface
*/
protected function loadDataElement(&$data, $type)
{
if (!isset($data[ElementInterface::class . '_instance'])) {
$data[ElementInterface::class] = ElementService::getElementById($data['id'], $type);
}
return $data[ElementInterface::class] ?? null;
}

}

0 comments on commit abcdcf0

Please sign in to comment.