Skip to content

Commit e7d5f41

Browse files
committed
feat: Add a way to pass arguments to operators.
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.
1 parent 945842d commit e7d5f41

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

src/GraphQL/Query/Operator/AbstractOperator.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
1919
use Pimcore\DataObject\GridColumnConfig\ConfigElementInterface;
2020

21-
abstract class AbstractOperator implements OperatorInterface
21+
abstract class AbstractOperator implements OperationContextAwareInterface, OperationArgumentsAwareInterface
2222
{
2323
use ServiceTrait;
2424

@@ -37,6 +37,9 @@ abstract class AbstractOperator implements OperatorInterface
3737
*/
3838
protected $children;
3939

40+
protected array $operationContext = [];
41+
protected array $operationArguments = [];
42+
4043
/**
4144
* @param array $config
4245
* @param array|null $context
@@ -99,4 +102,24 @@ public function setLabel($label)
99102
{
100103
$this->label = $label;
101104
}
105+
106+
public function setOperationArguments(array $arguments): void
107+
{
108+
$this->operationArguments = $arguments;
109+
}
110+
111+
public function getOperationArguments(): array
112+
{
113+
return $this->operationArguments;
114+
}
115+
116+
public function setOperationContext(array $context): void
117+
{
118+
$this->operationContext = $context;
119+
}
120+
121+
public function getOperationContext(): array
122+
{
123+
return $this->operationContext;
124+
}
102125
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Query\Operator;
19+
20+
interface OperationArgumentsAwareInterface extends OperatorInterface
21+
{
22+
public function setOperationArguments(array $arguments): void;
23+
24+
public function getOperationArguments(): array;
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Query\Operator;
19+
20+
interface OperationContextAwareInterface extends OperatorInterface
21+
{
22+
public function setOperationContext(array $context): void;
23+
24+
public function getOperationContext(): array;
25+
}

src/GraphQL/Resolver/Base.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
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\Query\Operator\OperationArgumentsAwareInterface;
23+
use Pimcore\Bundle\DataHubBundle\GraphQL\Query\Operator\OperationContextAwareInterface;
2224
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait;
2325
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
2426
use Pimcore\Model\DataObject\ClassDefinition;
@@ -63,6 +65,9 @@ public function __construct($typeName, $attributes, $class, $container)
6365
*/
6466
public function resolve($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
6567
{
68+
// If value is an array it is assumed that the data were resolved
69+
// upstream and don't need further evaluation but simple delivery later
70+
// on. Which is achieved by using a deferred wrapper.
6671
if (is_array($value)) {
6772
$result = $value[$resolveInfo->fieldName] ?? null;
6873
// check for alias as we cache the properties with aliases
@@ -86,6 +91,12 @@ public function resolve($value = null, $args = [], $context = [], ResolveInfo $r
8691
}
8792
/** @var \Pimcore\Bundle\DataHubBundle\GraphQL\Query\Operator\AbstractOperator $operatorImpl */
8893
$operatorImpl = $this->getGraphQlService()->buildQueryOperator($this->typeName, $this->attributes);
94+
if ($operatorImpl instanceof OperationArgumentsAwareInterface) {
95+
$operatorImpl->setOperationArguments($args ?? []);
96+
}
97+
if ($operatorImpl instanceof OperationContextAwareInterface) {
98+
$operatorImpl->setOperationContext($context ?? []);
99+
}
89100

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

0 commit comments

Comments
 (0)