From 364a2477b4cbdfe04394820cf326233f67d1ade8 Mon Sep 17 00:00:00 2001 From: Peter Philipp Date: Fri, 16 Aug 2024 17:36:12 +0200 Subject: [PATCH] chore: More work on ElementMockup handling. Alias has to be used to resolve pre-processed data otherwise we might miss multiple call signatures of the same operation. --- src/GraphQL/FieldHelper/AssetFieldHelper.php | 19 +---------- .../FieldHelper/DataObjectFieldHelper.php | 17 ++-------- src/GraphQL/Resolver/AssetType.php | 33 ++++++++++++++----- src/GraphQL/Service.php | 25 ++++++++++++++ 4 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/GraphQL/FieldHelper/AssetFieldHelper.php b/src/GraphQL/FieldHelper/AssetFieldHelper.php index 4b1e0fa8..d8ed9259 100644 --- a/src/GraphQL/FieldHelper/AssetFieldHelper.php +++ b/src/GraphQL/FieldHelper/AssetFieldHelper.php @@ -129,24 +129,7 @@ public function doExtractData(FieldNode $ast, &$data, $container, $args, $contex } } } else { - if (Service::checkContainerMethodExists($container, $getter)) { - if ($languageArgument) { - if ($ast->alias) { - // defer it - $data[$realName] = function ($source, $args, $context, ResolveInfo $info) use ( - $container, - $getter, - $ast - ) { - return Service::callContainerGetterMethod($container, $getter, ['language' => $args['language'] ?? null], $info, $ast); - }; - } else { - $data[$realName] = Service::callContainerGetterMethod($container, $getter, ['language' => $languageArgument], $resolveInfo, $ast); - } - } else { - $data[$realName] = Service::callContainerGetterMethod($container, $getter, [], $resolveInfo, $ast); - } - } + Service::resolveContainerGetterData($container, $data, $getter, $resolveInfo, $ast, $languageArgument); } } } diff --git a/src/GraphQL/FieldHelper/DataObjectFieldHelper.php b/src/GraphQL/FieldHelper/DataObjectFieldHelper.php index e46dbc5f..07ad6953 100644 --- a/src/GraphQL/FieldHelper/DataObjectFieldHelper.php +++ b/src/GraphQL/FieldHelper/DataObjectFieldHelper.php @@ -373,21 +373,8 @@ public function doExtractData(FieldNode $ast, &$data, $container, $args, $contex // throw new MySafeException("fieldhelper", "TBD customized error message"); $getter = 'get' . ucfirst($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, - $ast - ) { - return Service::callContainerGetterMethod($container, $getter, ['language' => $args['language'] ?? null], $info, $ast); - }; - } else { - $data[$astName] = Service::callContainerGetterMethod($container, $getter, [], $resolveInfo, $ast); - } - } + $isLocalizedField = Service::isLocalizedField($container, $astName); + Service::resolveContainerGetterData($container, $data, $getter, $resolveInfo, $ast, null, $isLocalizedField); } /** diff --git a/src/GraphQL/Resolver/AssetType.php b/src/GraphQL/Resolver/AssetType.php index 7d9d9c20..bc361384 100644 --- a/src/GraphQL/Resolver/AssetType.php +++ b/src/GraphQL/Resolver/AssetType.php @@ -143,9 +143,14 @@ public function resolvePath($value = null, $args = [], $context = [], ResolveInf if ($value instanceof BaseDescriptor) { $asset = $this->getAssetFromValue($value, $context); // Check if the value was already resolved in a mockup object. - if ($asset instanceof ElementMockupInterface && isset($value[$resolveInfo->fieldName])) { - return $value[$resolveInfo->fieldName]; + $returnName = $resolveInfo->fieldNodes[0]?->alias?->value ?? $resolveInfo->fieldName; + if ($asset instanceof ElementMockupInterface) { + if (isset($value[$returnName])) { + return $value[$returnName]; + } + $asset = $asset->getOriginalObject(); } + $thumbNailConfig = $args['thumbnail'] ?? null; $thumbNailFormat = $args['format'] ?? null; $assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper(); @@ -263,8 +268,12 @@ public function resolveResolutions($value = null, $args = [], $context = [], Res $deferredThumbnail = true; } // Check if the value was already resolved in a mockup object. - if ($thumbnail instanceof ElementMockupInterface && isset($value[$resolveInfo->fieldName])) { - return $value[$resolveInfo->fieldName]; + $returnName = $resolveInfo->fieldNodes[0]?->alias?->value ?? $resolveInfo->fieldName; + if ($thumbnail instanceof ElementMockupInterface) { + if (isset($value[$returnName])) { + return $value[$returnName]; + } + $thumbnail = $thumbnail->getOriginalObject(); } if ($thumbnail instanceof Asset\Image\Thumbnail) { @@ -302,8 +311,12 @@ public function resolveResolutions($value = null, $args = [], $context = [], Res return []; } // Check if the value was already resolved in a mockup object. - if ($asset instanceof ElementMockupInterface && isset($value[$resolveInfo->fieldName])) { - return $value[$resolveInfo->fieldName]; + $returnName = $resolveInfo->fieldNodes[0]?->alias?->value ?? $resolveInfo->fieldName; + if ($asset instanceof ElementMockupInterface) { + if (isset($value[$returnName])) { + return $value[$returnName]; + } + $asset = $asset->getOriginalObject(); } $thumbnail = $assetFieldHelper->getAssetThumbnail($asset, $thumbnailName, $thumbnailFormat, $deferredThumbnail); if (isset($thumbnail)) { @@ -345,8 +358,12 @@ public function resolveDimensions($value = null, $args = [], $context = [], Reso $thumbnailName = $args['thumbnail'] ?? null; $asset = $this->getAssetFromValue($value, $context); // Check if the value was already resolved in a mockup object. - if ($asset instanceof ElementMockupInterface && isset($value[$resolveInfo->fieldName])) { - return $value[$resolveInfo->fieldName]; + $returnName = $resolveInfo->fieldNodes[0]?->alias?->value ?? $resolveInfo->fieldName; + if ($asset instanceof ElementMockupInterface) { + if (isset($value[$returnName])) { + return $value[$returnName]; + } + $asset = $asset->getOriginalObject(); } if ($asset instanceof Asset\Video) { diff --git a/src/GraphQL/Service.php b/src/GraphQL/Service.php index 6f09af67..608eda51 100644 --- a/src/GraphQL/Service.php +++ b/src/GraphQL/Service.php @@ -1313,6 +1313,7 @@ public static function getContainerClassDefinition(object $container): ClassDefi * @param string $getter * @param array $getterArgs * @param \GraphQL\Type\Definition\ResolveInfo|null $resolveInfo + * @param \GraphQL\Language\AST\FieldNode|null $ast * * @return mixed */ @@ -1336,4 +1337,28 @@ public static function callContainerGetterMethod( return $return; } + + public static function resolveContainerGetterData($container, &$data, $getter, ResolveInfo $resolveInfo, FieldNode $ast, $languageArgument = null , $defer = null) + { + if (static::checkContainerMethodExists($container, $getter)) { + $realName = $ast->name->value; + $outputName = $ast->alias?->value ?? $realName; + if ($languageArgument) { + if ($ast->alias || $defer) { + // defer it + $data[$realName] = function ($source, $args, $context, ResolveInfo $info) use ( + $container, + $getter, + $ast + ) { + return Service::callContainerGetterMethod($container, $getter, [$args['language'] ?? null], $info, $ast); + }; + } else { + $data[$outputName] = $data[$realName] = Service::callContainerGetterMethod($container, $getter, [$languageArgument], $resolveInfo, $ast); + } + } else { + $data[$outputName] = $data[$realName] = Service::callContainerGetterMethod($container, $getter, [], $resolveInfo, $ast); + } + } + } }