Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/asset format graphql #12521

Merged
merged 5 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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