Skip to content

Commit

Permalink
chore: Forward element instance to the element resolver to allow furt…
Browse files Browse the repository at this point in the history
…her usage of mock objects avoiding db loads.
  • Loading branch information
das-peter committed Jul 19, 2024
1 parent b5f0b32 commit 10c4abd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/GraphQL/FieldHelper/AbstractFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Type\Definition\ResolveInfo;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
use Pimcore\Model\Element\ElementInterface;

abstract class AbstractFieldHelper
Expand Down Expand Up @@ -118,10 +119,13 @@ public function getArguments(FieldNode $ast)
*/
public function extractData(&$data, $container, $args, $context = [], ResolveInfo $resolveInfo = null)
{
if ($container instanceof ElementInterface) {
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;
}

$resolveInfoArray = (array)$resolveInfo;
Expand Down
11 changes: 6 additions & 5 deletions src/GraphQL/Resolver/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(string $elementType, Service $graphQlService)
*/
public function resolveTag($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
{
$element = ElementService::getElementById($this->elementType, $value['id']);
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);

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 = ElementService::getElementById($this->elementType, $elementId);
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $elementId);

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 = ElementService::getElementById($this->elementType, $value['id']);
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);
if ($element) {
$parent = $element->getParent();
if ($parent) {
Expand All @@ -139,7 +139,8 @@ public function resolveParent($value = null, $args = [], $context = [], ResolveI
*/
public function resolveChildren($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
{
$element = ElementService::getElementById($this->elementType, $value['id']);
$element = $value[ElementInterface::class] ?? ElementService::getElementById($this->elementType, $value['id']);

if ($element) {
$arguments = $this->composeArguments($args);

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

Expand Down

0 comments on commit 10c4abd

Please sign in to comment.