Skip to content

Commit

Permalink
feat: Add a way to pass arguments to operators.
Browse files Browse the repository at this point in the history
Why this wasn't added to the operator call in \Pimcore\Bundle\DataHubBundle\GraphQL\Resolver\Base::resolve() in the first place is a mystery - dropping information just like that is a shame. And now we can't simply adjusting the interface without breaking stuff but have to create more interfaces.
  • Loading branch information
das-peter committed Jul 23, 2024
1 parent 945842d commit 106fe27
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/GraphQL/Query/Operator/ArgumentsAwareOperatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Query\Operator;

interface ArgumentsAwareOperatorInterface extends OperatorInterface
{
public function setArguments(array $arguments);

public function getArguments();
}
25 changes: 25 additions & 0 deletions src/GraphQL/Query/Operator/ContextAwareOperatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Query\Operator;

interface ContextAwareOperatorInterface extends OperatorInterface
{
public function setContext(array $context);

public function getContext();
}
11 changes: 11 additions & 0 deletions src/GraphQL/Resolver/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use GraphQL\Executor\Promise\Adapter\SyncPromise;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Type\Definition\ResolveInfo;
use Pimcore\Bundle\DataHubBundle\GraphQL\Query\Operator\ArgumentsAwareOperatorInterface;
use Pimcore\Bundle\DataHubBundle\GraphQL\Query\Operator\ContextAwareOperatorInterface;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
use Pimcore\Model\DataObject\ClassDefinition;
Expand Down Expand Up @@ -63,6 +65,9 @@ public function __construct($typeName, $attributes, $class, $container)
*/
public function resolve($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
{
// If value is an array it is assumed that the data were resolved
// upstream and don't need further evaluation but simple delivery later
// on. Which is achieved by using a deferred wrapper.
if (is_array($value)) {
$result = $value[$resolveInfo->fieldName] ?? null;
// check for alias as we cache the properties with aliases
Expand All @@ -86,6 +91,12 @@ 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);
if ($operatorImpl instanceof ArgumentsAwareOperatorInterface) {
$operatorImpl->setArguments($args ?? []);
}
if ($operatorImpl instanceof ContextAwareOperatorInterface) {
$operatorImpl->setContext($context ?? []);
}

$element = $this->loadDataElement($value, 'object');
$valueFromOperator = $operatorImpl->getLabeledValue($element, $resolveInfo);
Expand Down

0 comments on commit 106fe27

Please sign in to comment.