Skip to content

Commit

Permalink
[Improvement]: Retrieving thumbnail URLs via GraphQL API triggers thu…
Browse files Browse the repository at this point in the history
…mbnail generation (#902)

* Added `deferred` option

* Apply php-cs-fixer changes

* Fixed argument order

* Fix phpstan/sonar

* Apply php-cs-fixer changes

* Added documentation

* Wording

---------

Co-authored-by: mcop1 <[email protected]>
  • Loading branch information
mcop1 and mcop1 authored Oct 24, 2024
1 parent d534f7d commit d21e3f3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
32 changes: 32 additions & 0 deletions doc/10_GraphQL/04_Query/04_Asset_Queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,35 @@ For details see [filtering documentation page](./10_Filtering.md)
## Localization of Queries
Queries can be localized For details see the [localization documentation page](./08_Localization.md).

## Thumbnails

You can request thumbnails for assets using a simple query like this:

#### Video assets thumbnails

You need to specify the name of the video thumbnail configuration you want to use.
Additionally, you can use the `format` parameter to request a specific format.

```graphql
query {
getAsset(id:353) {
id,
thumbnail:fullpath(thumbnail: "content"),
}
}
```

#### Image assets thumbnails

You need to specify the name of the video thumbnail configuration you want to use.
Use the `format` parameter to request a specific format.
Specifying the `deferred` parameter will defer the thumbnail generation until it gets requested for the first time.

```graphql
query {
getAsset(id:289) {
id,
thumbnail:fullpath(thumbnail: "events_header", deferred:true)
}
}
```
2 changes: 2 additions & 0 deletions src/GraphQL/AssetType/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function build(&$config)
'args' => [
'thumbnail' => ['type' => Type::string()],
'format' => ['type' => Type::string()],
'deferred' => ['type' => Type::boolean(), 'defaultValue' => false],
],
'resolve' => [$resolver, 'resolvePath'],
],
Expand Down Expand Up @@ -140,6 +141,7 @@ public function build(&$config)
'args' => [
'thumbnail' => ['type' => Type::nonNull(Type::string())],
'format' => ['type' => Type::string()],
'deferred' => ['type' => Type::boolean(), 'defaultValue' => false],
],
'resolve' => [$resolver, 'resolveSrcSet'],
],
Expand Down
37 changes: 28 additions & 9 deletions src/GraphQL/FieldHelper/AssetFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,47 @@ public function getVideoThumbnail(Asset\Video $asset, string | Video\Thumbnail\C
return null;
}

public function getImageDocumentThumbnail(Asset $asset, string | Image\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null): mixed
{
public function getImageDocumentThumbnail(
Asset $asset,
string | Image\Thumbnail\Config $thumbNailConfig,
string $thumbNailFormat = null,
bool $deferred = false
): mixed {
$thumb = null;

if ($asset instanceof Asset\Document || $asset instanceof Asset\Video) {
if ($asset instanceof Asset\Document) {
$thumb = $asset->getImageThumbnail($thumbNailConfig, deferred: $deferred);
}

if ($asset instanceof Asset\Video) {
$thumb = $asset->getImageThumbnail($thumbNailConfig);
} elseif ($asset instanceof Asset\Image) {
$thumb = $asset->getThumbnail($thumbNailConfig, false);
}
if (isset($thumb, $thumbNailFormat) && method_exists($thumb, 'getAsFormat') && !($asset instanceof Asset\Video)) {

if ($asset instanceof Asset\Image) {
$thumb = $asset->getThumbnail($thumbNailConfig, $deferred);
}

if (
!($asset instanceof Asset\Video) &&
isset($thumb, $thumbNailFormat) &&
method_exists($thumb, 'getAsFormat')
) {
$thumb = $thumb->getAsFormat($thumbNailFormat);
}

return $thumb;
}

public function getAssetThumbnail(Asset $asset, string | Image\Thumbnail\Config | Video\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null): mixed
{
public function getAssetThumbnail(
Asset $asset,
string | Image\Thumbnail\Config | Video\Thumbnail\Config $thumbNailConfig,
string $thumbNailFormat = null,
bool $deferred = false
): mixed {
if (($asset instanceof Asset\Video) && (is_string($thumbNailConfig) || $thumbNailConfig instanceof Video\Thumbnail\Config)) {
return $this->getVideoThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
} else {
return $this->getImageDocumentThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
return $this->getImageDocumentThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/GraphQL/Resolver/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ public function resolvePath($value = null, $args = [], $context = [], ResolveInf
$asset = $this->getAssetFromValue($value, $context);
$thumbNailConfig = $args['thumbnail'] ?? null;
$thumbNailFormat = $args['format'] ?? null;
$deferred = $args['deferred'] ?? false;
$assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper();

if (!isset($thumbNailConfig)) {
return $asset->getFullPath();
}

return $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
return $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);
}

/**
Expand All @@ -170,12 +171,13 @@ public function resolveData($value = null, $args = [], $context = [], ResolveInf
$asset = $this->getAssetFromValue($value, $context);
$thumbNailConfig = $args['thumbnail'] ?? null;
$thumbNailFormat = $args['format'] ?? null;
$deferred = $args['deferred'] ?? false;
$assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper();

if (!isset($thumbNailConfig)) {
return base64_encode(stream_get_contents($asset->getStream()));
}
$thumb = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
$thumb = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);

return $thumb ? base64_encode(stream_get_contents($thumb->getStream())) : base64_encode(stream_get_contents($asset->getStream()));
}
Expand All @@ -194,12 +196,13 @@ public function resolveSrcSet($value = null, $args = [], $context = [], ResolveI
$asset = $this->getAssetFromValue($value, $context);
$thumbNailConfig = $args['thumbnail'] ?? null;
$thumbNailFormat = $args['format'] ?? null;
$deferred = $args['deferred'] ?? null;
$assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper();

if ($asset instanceof Asset\Image) {
$mediaQueries = [];
$thumbnail = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
$thumbnailConfig = $asset->getThumbnail($args['thumbnail'])->getConfig();
$thumbnail = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);
$thumbnailConfig = $asset->getThumbnail($args['thumbnail'], $deferred)->getConfig();
if ($thumbnailConfig) {
foreach ($thumbnailConfig->getMedias() as $key => $val) {
$mediaQueries[] = [
Expand Down

0 comments on commit d21e3f3

Please sign in to comment.