Skip to content

Commit eeb69c0

Browse files
committed
chore: Generalize the element loading / sharing by introducing a trait.
1 parent 10c4abd commit eeb69c0

File tree

6 files changed

+76
-21
lines changed

6 files changed

+76
-21
lines changed

src/GraphQL/FieldHelper/AbstractFieldHelper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
use GraphQL\Language\AST\NodeList;
2323
use GraphQL\Language\AST\SelectionSetNode;
2424
use GraphQL\Type\Definition\ResolveInfo;
25+
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait;
2526
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
2627
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
2728
use Pimcore\Model\Element\ElementInterface;
2829

2930
abstract class AbstractFieldHelper
3031
{
31-
use ServiceTrait;
32+
use ServiceTrait, ElementLoaderTrait;
3233

3334
public function __construct()
3435
{
@@ -122,10 +123,7 @@ public function extractData(&$data, $container, $args, $context = [], ResolveInf
122123
if ($container instanceof ElementInterface || $container instanceof DefaultMockup) {
123124
// we have to at least add the ID and pass it around even if not requested because we need it internally
124125
// to resolve fields of linked elements (such as asset image and so on)
125-
$data['id'] = $container->getId();
126-
// Register the element for downstream resolvers to avoid object
127-
// loads.
128-
$data[ElementInterface::class] = $container;
126+
$data = $this->setDataElement($data, $container);
129127
}
130128

131129
$resolveInfoArray = (array)$resolveInfo;

src/GraphQL/Resolver/AssetType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
use Pimcore\Bundle\DataHubBundle\GraphQL\BaseDescriptor;
2121
use Pimcore\Bundle\DataHubBundle\GraphQL\ElementDescriptor;
2222
use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
23+
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait;
2324
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementTagTrait;
2425
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
2526
use Pimcore\Bundle\DataHubBundle\WorkspaceHelper;
2627
use Pimcore\Model\Asset;
2728

2829
class AssetType
2930
{
30-
use ServiceTrait, ElementTagTrait;
31+
use ServiceTrait, ElementTagTrait, ElementLoaderTrait;
3132

3233
/**
3334
* @param ElementDescriptor|null $value
@@ -410,8 +411,7 @@ protected function getAssetFromValue($value, $context)
410411
if (!$value instanceof ElementDescriptor) {
411412
return null;
412413
}
413-
414-
$asset = Asset::getById($value['id']);
414+
$asset = $this->loadDataElement($value, 'asset');
415415

416416
if (!WorkspaceHelper::checkPermission($asset, 'read')) {
417417
return null;

src/GraphQL/Resolver/Base.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
use GraphQL\Executor\Promise\Adapter\SyncPromise;
2020
use GraphQL\Language\AST\FieldNode;
2121
use GraphQL\Type\Definition\ResolveInfo;
22+
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait;
2223
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
23-
use Pimcore\Model\DataObject\AbstractObject;
2424
use Pimcore\Model\DataObject\ClassDefinition;
2525

2626
class Base
2727
{
28-
use ServiceTrait;
28+
use ServiceTrait, ElementLoaderTrait;
2929

3030
/** @var string */
3131
protected $typeName;
@@ -87,7 +87,7 @@ public function resolve($value = null, $args = [], $context = [], ResolveInfo $r
8787
/** @var \Pimcore\Bundle\DataHubBundle\GraphQL\Query\Operator\AbstractOperator $operatorImpl */
8888
$operatorImpl = $this->getGraphQlService()->buildQueryOperator($this->typeName, $this->attributes);
8989

90-
$element = AbstractObject::getById($value['id']);
90+
$element = $this->loadDataElement($value, 'object');
9191
$valueFromOperator = $operatorImpl->getLabeledValue($element, $resolveInfo);
9292

9393
$value = $valueFromOperator->value ?? null;

src/GraphQL/Resolver/DataObject.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(Service $graphQlService)
4242
*/
4343
public function resolveTag($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
4444
{
45-
$object = \Pimcore\Model\DataObject::getById($value['id']);
45+
$object = $this->loadDataElement($value, 'object');
4646

4747
if ($object) {
4848
$result = $this->getTags('object', $object->getId());
@@ -68,7 +68,7 @@ public function resolveIndex($value = null, $args = [], $context = [], ResolveIn
6868
return null;
6969
}
7070

71-
$object = \Pimcore\Model\DataObject::getById($value['id']);
71+
$object = $this->loadDataElement($value, 'object');
7272

7373
if (!$object instanceof AbstractObject) {
7474
return null;
@@ -91,7 +91,7 @@ public function resolveChildrenSortBy($value = null, $args = [], $context = [],
9191
return null;
9292
}
9393

94-
$object = \Pimcore\Model\DataObject::getById($value['id']);
94+
$object = $this->loadDataElement($value, 'object');
9595

9696
if (!$object instanceof \Pimcore\Model\DataObject) {
9797
return null;

src/GraphQL/Resolver/Element.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Pimcore\Bundle\DataHubBundle\GraphQL\Exception\ClientSafeException;
2222
use Pimcore\Bundle\DataHubBundle\GraphQL\FieldHelper\AbstractFieldHelper;
2323
use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
24+
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait;
2425
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementTagTrait;
2526
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
2627
use Pimcore\Bundle\DataHubBundle\WorkspaceHelper;
@@ -29,12 +30,11 @@
2930
use Pimcore\Model\DataObject\AbstractObject;
3031
use Pimcore\Model\Document;
3132
use Pimcore\Model\Element\ElementInterface;
32-
use Pimcore\Model\Element\Service as ElementService;
3333
use Pimcore\Model\Property;
3434

3535
class Element
3636
{
37-
use ServiceTrait, ElementTagTrait;
37+
use ServiceTrait, ElementTagTrait, ElementLoaderTrait;
3838

3939
/** @var string */
4040
protected $elementType;
@@ -57,7 +57,7 @@ public function __construct(string $elementType, Service $graphQlService)
5757
*/
5858
public function resolveTag($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
5959
{
60-
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
60+
$element = $this->loadDataElement($value, $this->elementType);
6161

6262
if ($element) {
6363
$result = $this->getTags('document', $element->getId());
@@ -82,7 +82,7 @@ public function resolveTag($value = null, $args = [], $context = [], ResolveInfo
8282
public function resolveProperties($value = null, array $args = [], array $context = [], ResolveInfo $resolveInfo = null)
8383
{
8484
$elementId = $value['id'];
85-
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $elementId);
85+
$element = $this->loadDataElement($value, $this->elementType);
8686

8787
if (!$element) {
8888
throw new ClientSafeException('element ' . $this->elementType . ' ' . $elementId . ' not found');
@@ -116,7 +116,7 @@ public function resolveProperties($value = null, array $args = [], array $contex
116116
*/
117117
public function resolveParent($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
118118
{
119-
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
119+
$element = $this->loadDataElement($value, $this->elementType);
120120
if ($element) {
121121
$parent = $element->getParent();
122122
if ($parent) {
@@ -139,7 +139,7 @@ public function resolveParent($value = null, $args = [], $context = [], ResolveI
139139
*/
140140
public function resolveChildren($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
141141
{
142-
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
142+
$element = $this->loadDataElement($value, $this->elementType);
143143

144144
if ($element) {
145145
$arguments = $this->composeArguments($args);
@@ -162,7 +162,7 @@ public function resolveChildren($value = null, $args = [], $context = [], Resolv
162162
*/
163163
public function resolveSiblings($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
164164
{
165-
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
165+
$element = $this->loadDataElement($value, $this->elementType);
166166
if ($element) {
167167
$arguments = $this->composeArguments($args);
168168

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Pimcore
7+
*
8+
* This source file is available under two different licenses:
9+
* - GNU General Public License version 3 (GPLv3)
10+
* - Pimcore Commercial License (PCL)
11+
* Full copyright and license information is available in
12+
* LICENSE.md which is distributed with this source code.
13+
*
14+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
15+
* @license http://www.pimcore.org/license GPLv3 and PCL
16+
*/
17+
18+
namespace Pimcore\Bundle\DataHubBundle\GraphQL\Traits;
19+
20+
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
21+
use Pimcore\Model\Element\ElementInterface;
22+
use Pimcore\Model\Element\Service as ElementService;
23+
24+
trait ElementLoaderTrait
25+
{
26+
/**
27+
* Stores the "metadata" of a related element into the data storage which
28+
* then is passed around.
29+
*
30+
* It can be used with loadDataElement() which if possible will use the
31+
* already loaded element but will fallback to load the element using
32+
* the id.
33+
*
34+
* @return array
35+
*/
36+
protected function setDataElement(\ArrayAccess $data, ElementInterface | DefaultMockup $element): \ArrayAccess
37+
{
38+
$data['id'] = $element->getId();
39+
$data[ElementInterface::class . '_type'] = $element->getType();
40+
$data[ElementInterface::class . '_instance'] = $element->getId();
41+
42+
return $data;
43+
}
44+
45+
/**
46+
*
47+
* @return ElementInterface
48+
*/
49+
protected function loadDataElement(&$data, $type)
50+
{
51+
if (!isset($data[ElementInterface::class . '_instance'])) {
52+
$data[ElementInterface::class] = ElementService::getElementById($data['id'], $type);
53+
}
54+
55+
return $data[ElementInterface::class] ?? null;
56+
}
57+
}

0 commit comments

Comments
 (0)