Skip to content

Commit 10c4abd

Browse files
committed
chore: Forward element instance to the element resolver to allow further usage of mock objects avoiding db loads.
1 parent b5f0b32 commit 10c4abd

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/GraphQL/FieldHelper/AbstractFieldHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use GraphQL\Language\AST\SelectionSetNode;
2424
use GraphQL\Type\Definition\ResolveInfo;
2525
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
26+
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
2627
use Pimcore\Model\Element\ElementInterface;
2728

2829
abstract class AbstractFieldHelper
@@ -118,10 +119,13 @@ public function getArguments(FieldNode $ast)
118119
*/
119120
public function extractData(&$data, $container, $args, $context = [], ResolveInfo $resolveInfo = null)
120121
{
121-
if ($container instanceof ElementInterface) {
122+
if ($container instanceof ElementInterface || $container instanceof DefaultMockup) {
122123
// we have to at least add the ID and pass it around even if not requested because we need it internally
123124
// to resolve fields of linked elements (such as asset image and so on)
124125
$data['id'] = $container->getId();
126+
// Register the element for downstream resolvers to avoid object
127+
// loads.
128+
$data[ElementInterface::class] = $container;
125129
}
126130

127131
$resolveInfoArray = (array)$resolveInfo;

src/GraphQL/Resolver/Element.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = ElementService::getElementById($this->elementType, $value['id']);
60+
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
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 = ElementService::getElementById($this->elementType, $elementId);
85+
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $elementId);
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 = ElementService::getElementById($this->elementType, $value['id']);
119+
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
120120
if ($element) {
121121
$parent = $element->getParent();
122122
if ($parent) {
@@ -139,7 +139,8 @@ public function resolveParent($value = null, $args = [], $context = [], ResolveI
139139
*/
140140
public function resolveChildren($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
141141
{
142-
$element = ElementService::getElementById($this->elementType, $value['id']);
142+
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
143+
143144
if ($element) {
144145
$arguments = $this->composeArguments($args);
145146

@@ -161,7 +162,7 @@ public function resolveChildren($value = null, $args = [], $context = [], Resolv
161162
*/
162163
public function resolveSiblings($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
163164
{
164-
$element = ElementService::getElementById($this->elementType, $value['id']);
165+
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
165166
if ($element) {
166167
$arguments = $this->composeArguments($args);
167168

0 commit comments

Comments
 (0)