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 e7d5f41
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/GraphQL/Query/Operator/AbstractOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
use Pimcore\DataObject\GridColumnConfig\ConfigElementInterface;

abstract class AbstractOperator implements OperatorInterface
abstract class AbstractOperator implements OperationContextAwareInterface, OperationArgumentsAwareInterface
{
use ServiceTrait;

Expand All @@ -37,6 +37,9 @@ abstract class AbstractOperator implements OperatorInterface
*/
protected $children;

protected array $operationContext = [];
protected array $operationArguments = [];

/**
* @param array $config
* @param array|null $context
Expand Down Expand Up @@ -99,4 +102,24 @@ public function setLabel($label)
{
$this->label = $label;
}

public function setOperationArguments(array $arguments): void
{
$this->operationArguments = $arguments;
}

public function getOperationArguments(): array
{
return $this->operationArguments;
}

public function setOperationContext(array $context): void
{
$this->operationContext = $context;
}

public function getOperationContext(): array
{
return $this->operationContext;
}
}
25 changes: 25 additions & 0 deletions src/GraphQL/Query/Operator/OperationArgumentsAwareInterface.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 OperationArgumentsAwareInterface extends OperatorInterface
{
public function setOperationArguments(array $arguments): void;

public function getOperationArguments(): array;
}
25 changes: 25 additions & 0 deletions src/GraphQL/Query/Operator/OperationContextAwareInterface.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 OperationContextAwareInterface extends OperatorInterface
{
public function setOperationContext(array $context): void;

public function getOperationContext(): array;
}
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\OperationArgumentsAwareInterface;
use Pimcore\Bundle\DataHubBundle\GraphQL\Query\Operator\OperationContextAwareInterface;
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 OperationArgumentsAwareInterface) {
$operatorImpl->setOperationArguments($args ?? []);
}
if ($operatorImpl instanceof OperationContextAwareInterface) {
$operatorImpl->setOperationContext($context ?? []);
}

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

0 comments on commit e7d5f41

Please sign in to comment.