Skip to content

Commit 0400e32

Browse files
committed
Added ProductImage reslover to images.
1 parent 7411604 commit 0400e32

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Catalog\Helper\ImageFactory as CatalogImageHelperFactory;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\Resolver\Value;
14+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
19+
/**
20+
* Returns product image.
21+
*/
22+
class ProductImage implements ResolverInterface
23+
{
24+
/**
25+
* @var CatalogImageHelperFactory
26+
*/
27+
private $catalogImageHelperFactory;
28+
29+
/**
30+
* @var ValueFactory
31+
*/
32+
private $valueFactory;
33+
34+
/**
35+
* @var StoreManagerInterface
36+
*/
37+
private $storeManager;
38+
39+
/**
40+
* @param ValueFactory $valueFactory
41+
* @param CatalogImageHelperFactory $catalogImageHelperFactory,
42+
* @param StoreManagerInterface $storeManager
43+
*/
44+
public function __construct(
45+
ValueFactory $valueFactory,
46+
CatalogImageHelperFactory $catalogImageHelperFactory,
47+
StoreManagerInterface $storeManager
48+
)
49+
{
50+
$this->valueFactory = $valueFactory;
51+
$this->catalogImageHelperFactory = $catalogImageHelperFactory;
52+
$this->storeManager = $storeManager;
53+
}
54+
55+
/**
56+
* Get product's image by type.
57+
*
58+
* {@inheritdoc}
59+
*/
60+
public function resolve(
61+
Field $field,
62+
$context,
63+
ResolveInfo $info,
64+
array $value = null,
65+
array $args = null
66+
): Value
67+
{
68+
if (!isset($value['model'])) {
69+
$result = function () {
70+
return null;
71+
};
72+
return $this->valueFactory->create($result);
73+
}
74+
/** @var Product $product */
75+
$product = $value['model'];
76+
$imageType = $field->getName();
77+
78+
$catalogImageHelper = $this->catalogImageHelperFactory->create();
79+
80+
$imageUrl = $catalogImageHelper->init(
81+
$product,
82+
'product_' . $imageType,
83+
['type' => $imageType]
84+
)->getUrl();
85+
86+
$imageData = [
87+
'url' => $imageUrl,
88+
'path' => $product->getData($imageType)
89+
];
90+
91+
$result = function () use ($imageData) {
92+
return $imageData;
93+
};
94+
95+
return $this->valueFactory->create($result);
96+
}
97+
}

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
257257
meta_title: String @doc(description: "A string that is displayed in the title bar and tab of the browser and in search results lists")
258258
meta_keyword: String @doc(description: "A comma-separated list of keywords that are visible only to search engines")
259259
meta_description: String @doc(description: "A brief overview of the product for search results listings, maximum 255 characters")
260-
image: String @doc(description: "The relative path to the main image on the product page")
261-
small_image: String @doc(description: "The relative path to the small image, which is used on catalog pages")
262-
thumbnail: String @doc(description: "The relative path to the product's thumbnail image")
260+
image: ProductImage @doc(description: "The relative path to the main image on the product page") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductImage")
261+
small_image: ProductImage @doc(description: "The relative path to the small image, which is used on catalog pages") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductImage")
262+
thumbnail: ProductImage @doc(description: "The relative path to the product's thumbnail image") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductImage")
263263
new_from_date: String @doc(description: "The beginning date for new product listings, and determines if the product is featured as a new product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\NewFromTo")
264264
new_to_date: String @doc(description: "The end date for new product listings") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\NewFromTo")
265265
tier_price: Float @doc(description: "The price when tier pricing is in effect and the items purchased threshold has been reached")
@@ -352,6 +352,11 @@ type CustomizableFileValue @doc(description: "CustomizableFileValue defines the
352352
image_size_y: Int @doc(description: "The maximum height of an image")
353353
}
354354

355+
type ProductImage @doc(description: "Product image information. Contains image relative path and URL") {
356+
url: String
357+
path: String
358+
}
359+
355360
interface CustomizableOptionInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\CustomizableOptionTypeResolver") @doc(description: "The CustomizableOptionInterface contains basic information about a customizable option. It can be implemented by several types of configurable options.") {
356361
title: String @doc(description: "The display name for this option")
357362
required: Boolean @doc(description: "Indicates whether the option is required")
@@ -548,6 +553,6 @@ type SortField {
548553
}
549554

550555
type SortFields @doc(description: "SortFields contains a default value for sort fields and all available sort fields") {
551-
default: String @doc(description: "Default value of sort fields")
556+
default: String @doc(description: "Default value of sort fields")
552557
options: [SortField] @doc(description: "Available sort fields")
553558
}

0 commit comments

Comments
 (0)