statamic / ideas

đŸ’¡Discussions on ideas and feature requests for Statamic
https://statamic.dev
32 stars 1 forks source link

GraphQL: Image transforms via query #533

Open moacode opened 3 years ago

moacode commented 3 years ago

Are there any plans to add image transform support to GraphQL queries? I come from a Craft background and they have support for doing this sort of this:

{
  entries (section: "news", limit: 2, orderBy: "dateCreated DESC") {
    dateCreated @formatDateTime (format: "Y-m-d")
    title
    children {
      title
    }
    ... on news_article_Entry {
      shortDescription
      featuredImage {
        url @transform (width: 300, immediately: true)
      }
    }
  }
}

We've hacked together a simple solution for now using the following code in AppServiceProvider:

GraphQL::addField(
    'AssetInterface', 'transform', function () {
        return [
        'type'    => GraphQL::string(),
        'args'    => [
            'width' => [
                'type' => GraphQL::int(),
            ],
            'height' => [
                'type' => GraphQL::int(),
            ],
        ],
        'resolve' => function ($entry, $args) {
            $image = $entry->id();
            if (!$image) {
                return null;
            }

            $url = Image::manipulate($image)
                ->width($args['width'])
                ->height($args['height'])
                ->build();

            return \Illuminate\Support\Facades\URL::secure($url);
        }];
    }
);

but first party support would be awesome!

jasonvarga commented 3 years ago

Yup! What you've done is probably almost exactly what we'll end up doing.

grischaerbe commented 3 years ago

@thejoshsmith In the meantime, feel free to use my addon: https://packagist.org/packages/legrisch/statamic-graphql-thumbnails