Skip to content

Commit

Permalink
chore: Added context data handling for ElementMockupInterface. This w…
Browse files Browse the repository at this point in the history
…ill allow to select appropriate pre-processed data depending on execution context.
  • Loading branch information
das-peter committed Aug 16, 2024
1 parent 4f99a28 commit dc102f4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/GraphQL/FieldHelper/AssetFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use GraphQL\Language\AST\FieldNode;
use GraphQL\Type\Definition\ResolveInfo;
use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Asset\Video;
Expand Down Expand Up @@ -128,21 +129,22 @@ public function doExtractData(FieldNode $ast, &$data, $container, $args, $contex
}
}
} else {
if ($this->getGraphQlService()::checkContainerMethodExists($container, $getter)) {
if (Service::checkContainerMethodExists($container, $getter)) {
if ($languageArgument) {
if ($ast->alias) {
// defer it
$data[$realName] = function ($source, $args, $context, ResolveInfo $info) use (
$container,
$getter
$getter,
$ast
) {
return $container->$getter($args['language'] ?? null);
return Service::callContainerGetterMethod($container, $getter, ['language' => $args['language'] ?? null], $info, $ast);
};
} else {
$data[$realName] = $container->$getter($languageArgument);
$data[$realName] = Service::callContainerGetterMethod($container, $getter, ['language' => $languageArgument], $resolveInfo, $ast);
}
} else {
$data[$realName] = $container->$getter();
$data[$realName] = Service::callContainerGetterMethod($container, $getter, [], $resolveInfo, $ast);
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/GraphQL/FieldHelper/DataObjectFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Pimcore\Bundle\DataHubBundle\GraphQL\Exception\ClientSafeException;
use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
use Pimcore\File;
use Pimcore\Logger;
use Pimcore\Model\DataObject\ClassDefinition;
Expand Down Expand Up @@ -372,18 +373,19 @@ public function doExtractData(FieldNode $ast, &$data, $container, $args, $contex
// throw new MySafeException("fieldhelper", "TBD customized error message");

$getter = 'get' . ucfirst($astName);
if ($this->getGraphQlService()::checkContainerMethodExists($container, $getter)) {
$isLocalizedField = $this->getGraphQlService()::isLocalizedField($container, $astName);
if (Service::checkContainerMethodExists($container, $getter)) {
$isLocalizedField = Service::isLocalizedField($container, $astName);
if ($isLocalizedField) {
// defer it
$data[$astName] = function ($source, $args, $context, ResolveInfo $info) use (
$container,
$getter
$getter,
$ast
) {
return $container->$getter($args['language'] ?? null);
return Service::callContainerGetterMethod($container, $getter, ['language' => $args['language'] ?? null], $info, $ast);
};
} else {
$data[$astName] = $container->$getter();
$data[$astName] = Service::callContainerGetterMethod($container, $getter, [], $resolveInfo, $ast);
}
}
}
Expand Down
44 changes: 37 additions & 7 deletions src/GraphQL/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@
use Pimcore\Cache\RuntimeCache;
use Pimcore\DataObject\GridColumnConfig\ConfigElementInterface;
use Pimcore\Localization\LocaleServiceInterface;
use Pimcore\Model\AbstractModel;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Fieldcollection\Definition;
use Pimcore\Model\DataObject\Objectbrick\Data\AbstractData;
use Pimcore\Model\DataObject\Objectbrick\Definition;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Factory;
use Pimcore\Translation\Translator;
Expand Down Expand Up @@ -1078,9 +1077,9 @@ public static function resolveValue(BaseDescriptor $descriptor, Data $fieldDefin
} elseif (static::checkContainerMethodExists($container, $getter)) {
$isLocalizedField = self::isLocalizedField($container, $fieldDefinition->getName());
if ($isLocalizedField) {
$result = $container->$getter($args['language'] ?? null);
$result = Service::callContainerGetterMethod($container, $getter, ['language' => $args['language'] ?? null]);
} else {
$result = $container->$getter();
$result = Service::callContainerGetterMethod($container, $getter);
}
}

Expand Down Expand Up @@ -1286,11 +1285,10 @@ public static function checkContainerMethodExists(object $container, string $met
*
* @param object $container
*
* @return \Pimcore\Model\AbstractModel|null
* @return \Pimcore\Model\DataObject\ClassDefinition|\Pimcore\Model\DataObject\Fieldcollection\Definition|null
*
* @throws \Exception
*/
public static function getContainerClassDefinition(object $container): ?AbstractModel
public static function getContainerClassDefinition(object $container): ClassDefinition | Definition | null
{
// Adjust meta data for data handling on type of the data container.
switch (true) {
Expand All @@ -1305,4 +1303,36 @@ public static function getContainerClassDefinition(object $container): ?Abstract

return null;
}

/**
* Call the getter function on a container.
*
* Passes on execution context to containers with the ElementMockupInterface.
*
* @param object $container
* @param string $getter
* @param array $getterArgs
* @param \GraphQL\Type\Definition\ResolveInfo|null $resolveInfo
*
* @return mixed
*/
public static function callContainerGetterMethod(
object $container,
string $getter,
array $getterArgs = [],
?ResolveInfo $resolveInfo = null,
?FieldNode $ast = null
): mixed {
if ($container instanceof ElementMockupInterface) {
$container->setGraphQLContext($getter, $getterArgs, $resolveInfo, $ast);
}
try {
$return = call_user_func_array([$container, $getter], $getterArgs);
} finally {
if ($container instanceof ElementMockupInterface) {
$container->setGraphQLContext(null);
}
}
return $return;
}
}
20 changes: 20 additions & 0 deletions src/Model/ElementMockupInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace Pimcore\Bundle\DataHubBundle\Model;

use GraphQL\Language\AST\FieldNode;
use GraphQL\Type\Definition\ResolveInfo;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\Element\ElementInterface;
Expand Down Expand Up @@ -57,4 +59,22 @@ public function getId();
* @return \Pimcore\Model\Element\ElementInterface|null
*/
public function getOriginalObject(): ElementInterface | null;

/**
* Set the GraphQL context.
*
* This can e.g. allow to react on operation arguments and select the
* appropriate pre-processed data from a mock object.
*
* @param string|null $getter
* @param array|null $getterArgs
* @param \GraphQL\Type\Definition\ResolveInfo|null $resolveInfo
* @param FieldNode $ast
*/
public function setGraphQLContext(
?string $getter,
?array $getterArgs = null,
?ResolveInfo $resolveInfo = null,
?FieldNode $ast = null
): void;
}

0 comments on commit dc102f4

Please sign in to comment.