Skip to content

Commit

Permalink
Merge pull request #12521 from craftcms/feature/asset-format-graphql
Browse files Browse the repository at this point in the history
Feature/asset format graphql
  • Loading branch information
brandonkelly authored Jan 30, 2023
2 parents ea969d2 + e626eea commit 3c90630
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
- The `dump()` Twig function now utilizes `Craft::dump()`, and no longer requires Dev Mode to be active. ([#12486](https://github.com/craftcms/cms/pull/12486), [#12479](https://github.com/craftcms/cms/discussions/12479))
- The `{% dd %}` Twig tag can now output the entire `context` array, if no variable is passed to it. ([#12486](https://github.com/craftcms/cms/pull/12486))
- Added `ancestors` and `descendants` fields to categories queried via GraphQL. ([#12427](https://github.com/craftcms/cms/issues/12427))
- Added `craft\elements\Asset::getFormat()`. ([#12398](https://github.com/craftcms/cms/pull/12398))
- Added `craft\elements\Asset::getFormat()` and the `format` field for assets queried via GraphQL. ([#12398](https://github.com/craftcms/cms/pull/12398), [#12521](https://github.com/craftcms/cms/pull/12521))
- `Craft::dump()`, `Craft::dd()`, the `dump()` Twig function, and the `{% dd %}` Twig tag now use Symfony’s VarDumper. ([#12479](https://github.com/craftcms/cms/discussions/12479))
- `Craft::dump()` now has a `$return` argument, which will cause the resulting dump to be returned as a string rather than output, if `true`.
- `craft\elements\Asset::getMimeType()` now has a `$transform` argument, and will now return the MIME type of the asset with a transform applied, when appropriate. ([#12269](https://github.com/craftcms/cms/discussions/12269), [#12397](https://github.com/craftcms/cms/pull/12397))
Expand Down
10 changes: 9 additions & 1 deletion src/gql/directives/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,16 @@ public static function name(): string
*/
public static function apply(mixed $source, mixed $value, array $arguments, ResolveInfo $resolveInfo): mixed
{
$applicableFields = [
'height',
'width',
'format',
'url',
];

$onAssetElement = $value instanceof Asset;
$onAssetElementList = $value instanceof Collection && !$value->isEmpty();
$onApplicableAssetField = $source instanceof Asset && in_array($resolveInfo->fieldName, ['height', 'width', 'url']);
$onApplicableAssetField = $source instanceof Asset && in_array($resolveInfo->fieldName, $applicableFields);

if (!($onAssetElement || $onAssetElementList || $onApplicableAssetField) || empty($arguments)) {
return $value;
Expand Down Expand Up @@ -99,6 +106,7 @@ public static function apply(mixed $source, mixed $value, array $arguments, Reso
'height' => $source->getHeight($transform),
'width' => $source->getWidth($transform),
'url' => $source->getUrl($transform),
'format' => $source->getFormat($transform),
default => $value,
};
}
Expand Down
6 changes: 6 additions & 0 deletions src/gql/interfaces/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ public static function getFieldDefinitions(): array
'type' => Type::string(),
'description' => 'The file’s MIME type, if it can be determined.',
],
'format' => [
'name' => 'format',
'args' => Transform::getArguments(),
'type' => Type::string(),
'description' => 'Returns the file’s format.',
],
'path' => [
'name' => 'path',
'type' => Type::nonNull(Type::string()),
Expand Down

0 comments on commit 3c90630

Please sign in to comment.