diff --git a/CHANGELOG.md b/CHANGELOG.md index 17df8672..90f3f9ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # ImageOptimize Changelog +## 1.4.32 - 2018.05.09 +### Added +* Improved CraftQL support by allowing parameter passing to `src` and `srcUrls` + ## 1.4.31 - 2018.04.22 ### Added * Added CraftQL support diff --git a/composer.json b/composer.json index 9342051a..923a50c4 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-imageoptimize", "description": "Automatically create & optimize responsive image transforms, using either native Craft transforms or a service like Imgix, with zero template changes.", "type": "craft-plugin", - "version": "1.4.31", + "version": "1.4.32", "keywords": [ "craft", "cms", diff --git a/src/listeners/GetCraftQLSchema.php b/src/listeners/GetCraftQLSchema.php index cb0de108..816e74da 100644 --- a/src/listeners/GetCraftQLSchema.php +++ b/src/listeners/GetCraftQLSchema.php @@ -33,8 +33,28 @@ public function handle($event) $fieldObject = $event->schema->createObjectType('OptimizedImagesData'); + $srcObject = $event->schema->createObjectType('OptimizedImageSrc'); + $srcObject->addStringField('url'); + $srcObject->addIntField('width'); + // Primary getter functions - $fieldObject->addStringField('src'); + $fieldObject->addStringField('src') + ->arguments(function (\markhuot\CraftQL\Builders\Field $field) { + $field->addIntArgument('width'); + }) + ->resolve(function ($root, $args) { + return $root->src(@$args['width'] ?: 0); + }); + $fieldObject->addField('srcUrls') + ->lists() + ->type($srcObject) + ->resolve(function ($root, $args) { + $result = []; + foreach ($root->optimizedImageUrls as $width => $url) { + $result[] = ['width' => $width, 'url' => $url]; + } + return $result; + }); $fieldObject->addStringField('srcset'); $fieldObject->addStringField('srcWebp'); $fieldObject->addStringField('srcsetWebp'); @@ -44,22 +64,14 @@ public function handle($event) $fieldObject->addStringField('placeholderSilhouette'); // Object properties - $fieldObject->addStringField('optimizedImageUrls') - ->lists() - ->type(Type::string()); - $fieldObject->addStringField('optimizedWebPImageUrls') - ->lists() - ->type(Type::string()); - $fieldObject->addIntField('variantSourceWidths') - ->lists() - ->type(Type::int()); + $fieldObject->addStringField('optimizedImageUrls')->lists(); + $fieldObject->addStringField('optimizedWebPImageUrls')->lists(); + $fieldObject->addIntField('variantSourceWidths')->lists(); $fieldObject->addIntField('originalImageWidth'); $fieldObject->addIntField('originalImageHeight'); $fieldObject->addStringField('placeholder'); $fieldObject->addStringField('placeholderSvg'); - $fieldObject->addStringField('colorPalette') - ->lists() - ->type(Type::string()); + $fieldObject->addStringField('colorPalette')->lists(); $fieldObject->addIntField('placeholderWidth'); $fieldObject->addIntField('placeholderHeight');