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: Graphql Cloudinary images #110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions Api/ProductGalleryManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public function addProductMedia($sku, $urls);
*/
public function getProductMedia($sku);

/**
* Get product gallery items.
* @method getProductMedia
* @param string $sku
* @return string
*/
public function getProductMediaData($sku);

/**
* Get products gallery items as Cloudinary URLs.
* @method getProductsMedia
Expand Down
46 changes: 42 additions & 4 deletions Model/Api/ProductGalleryManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,15 @@ public function addProductMedia($sku, $urls)
*/
public function getProductMedia($sku)
{
return $this->_getProductMedia($sku);
return $this->_getProductMedia($sku, true);
}

/**
* {@inheritdoc}
*/
public function getProductMediaData($sku)
{
return $this->_getProductMedia($sku, false);
}

/**
Expand All @@ -302,7 +310,7 @@ public function getProductsMedia($skus)
* @param mixed $sku
* @return string (json result)
*/
private function _getProductMedia($sku)
private function _getProductMedia($sku, $onlyUrls = true)
{
$result = ["data" => []];

Expand All @@ -311,10 +319,10 @@ private function _getProductMedia($sku)
$this->checkEnabled();
if (is_array($sku) || is_object($sku)) {
foreach ($sku as $key => $_sku) {
$result['data'][$_sku] = $this->getProductCldUrlsBySku($_sku);
$result['data'][$_sku] = ($onlyUrls ? $this->getProductCldUrlsBySku($_sku) : $this->getProductCldDataBySku($_sku));
}
} else {
$result['data'] = $this->getProductCldUrlsBySku($sku);
$result['data'] = ($onlyUrls ? $this->getProductCldUrlsBySku($sku) : $this->getProductCldDataBySku($sku));
}
} catch (\Exception $e) {
$result["error"] = 1;
Expand Down Expand Up @@ -768,6 +776,36 @@ private function getProductCldUrlsBySku($sku)
return $urls;
}

/**
* @method getProductCldDataBySku
* @param string $sku
* @return array
*/
private function getProductCldDataBySku($sku)
{
$data = [];
try {
$product = $this->productRepository->get($sku);
$attributes = $product->getAttributes();
foreach ($product->getMediaGalleryImages() as $gallItem) {
$gallItemData = $gallItem->getData();
foreach ($attributes as $attribute) {
$attrCode = $attribute->getAttributeCode();
if($product->getData($attrCode) === $gallItem->getFile()) {
$gallItemData['role'] = $attrCode;
}
}
array_push($data, $gallItemData);
}
} catch (\Exception $e) {
$data = [
'error' => 1,
'message' => $e->getMessage()
];
}
return $data;
}

///////////////////////////////
// App Environment Emulation //
///////////////////////////////
Expand Down
42 changes: 42 additions & 0 deletions Model/GraphQLResolver/ProductAttributeCldDataResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Cloudinary\Cloudinary\Model\GraphQLResolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Cloudinary\Cloudinary\Model\Api\ProductGalleryManagement;
use Magento\Framework\GraphQl\Type\Definition\ObjectType;

/**
* Class ProductAttributeCldResolver
**/
class ProductAttributeCldDataResolver implements ResolverInterface
{
/**
* @var ProductGalleryManagement
*/
private $productGalleryManagement;

/**
* ProductAttributeCldResolver constructor.
* @param ProductGalleryManagement $productGalleryManagement
*/
public function __construct(
ProductGalleryManagement $productGalleryManagement
) {
$this->productGalleryManagement = $productGalleryManagement;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$productId = $value['sku'];
$productMediaStr = $this->productGalleryManagement->getProductMediaData($productId);
$jsonDecoder = new \Magento\Framework\Serialize\Serializer\Json();
$productMedia = $jsonDecoder->unserialize($productMediaStr);
return $productMedia['data'];
}
}
30 changes: 30 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,38 @@ type CloudinaryData {
media_gallery: [String]
}

type CloudinaryImage {
role: String
disabled: String
disabled_default: String
entity_id: String
file: String
id: String
label: String
label_default: String
media_type: String
path: String
position: String
position_default: String
url: String
value_id: String
video_description: String
video_description_default: String
video_metadata: String
video_metadata_default: String
video_provider: String
video_provider_default: String
video_title: String
video_title_default: String
video_url: String
video_url_default: String
}

interface ProductInterface {
cld_data: CloudinaryData
@resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldResolver")
@doc(description: "Cloudinary urls generated for product images")
cloudinary_images: [CloudinaryImage]
@resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldDataResolver")
@doc(description: "Cloudinary urls generated for product images")
}