Skip to content

Commit

Permalink
chore: More work on ElementMockup handling. Alias has to be used to r…
Browse files Browse the repository at this point in the history
…esolve pre-processed data otherwise we might miss multiple call signatures of the same operation.
  • Loading branch information
das-peter committed Aug 16, 2024
1 parent f5fd99b commit 364a247
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 41 deletions.
19 changes: 1 addition & 18 deletions src/GraphQL/FieldHelper/AssetFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
17 changes: 2 additions & 15 deletions src/GraphQL/FieldHelper/DataObjectFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
33 changes: 25 additions & 8 deletions src/GraphQL/Resolver/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions src/GraphQL/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
}
}
}
}

0 comments on commit 364a247

Please sign in to comment.