Skip to content

Commit

Permalink
Add QueryType for Version on Assets (#838)
Browse files Browse the repository at this point in the history
* Extend Resolver

* Apply php-cs-fixer changes

* Update AssetType.php

* Apply suggestions from code review

Co-authored-by: Christian F. <kontakt+github@christian-feldkirchner.at>

* Apply php-cs-fixer changes

---------

Co-authored-by: leitge <leitge@users.noreply.github.com>
Co-authored-by: Christian F. <kontakt+github@christian-feldkirchner.at>
  • Loading branch information
3 people committed May 3, 2024
1 parent e5e1424 commit 3bb4b4f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/GraphQL/AssetType/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public function build(&$config)
'modificationDate' => Type::int(),
'type' => Type::string(),
'filesize' => Type::int(),
'version' => [
'type' => Type::int(),
'resolve' => [$resolver, 'resolveVersion'],
],
'data' => [
'type' => Type::string(),
'args' => [
Expand Down
17 changes: 17 additions & 0 deletions src/GraphQL/Resolver/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,23 @@ public function resolveDuration(ElementDescriptor | null $value = null, array $c
return $asset->getDuration();
}

/**
* @throws Exception
*/
public function resolveVersion($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
{
$asset = $this->getAssetFromValue($value, $context);
if ($asset) {
foreach (array_reverse($asset->getVersions()) as $version) {
if ($asset->getModificationDate() === $version->getDate()) {
return $version->getId();
}
}
}

return null;
}

/**
* @param ElementDescriptor $value
* @param array $context
Expand Down

0 comments on commit 3bb4b4f

Please sign in to comment.