Skip to content

Commit fc350e8

Browse files
author
Stanislav Idolov
committed
Merge remote-tracking branch 'mainline/2.3-develop' into 2.3-develop-prs
2 parents c4e056c + 76e18b9 commit fc350e8

File tree

6 files changed

+151
-14
lines changed

6 files changed

+151
-14
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Model\Product\ImageFactory;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
16+
/**
17+
* Returns product's image. If the image is not set, returns a placeholder
18+
*/
19+
class Image implements ResolverInterface
20+
{
21+
/**
22+
* Product image factory
23+
*
24+
* @var ImageFactory
25+
*/
26+
private $productImageFactory;
27+
28+
/**
29+
* @param ImageFactory $productImageFactory
30+
*/
31+
public function __construct(
32+
ImageFactory $productImageFactory
33+
) {
34+
$this->productImageFactory = $productImageFactory;
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function resolve(
41+
Field $field,
42+
$context,
43+
ResolveInfo $info,
44+
array $value = null,
45+
array $args = null
46+
): array {
47+
if (!isset($value['model'])) {
48+
throw new \LogicException(__('"model" value should be specified'));
49+
}
50+
/** @var Product $product */
51+
$product = $value['model'];
52+
$imageType = $field->getName();
53+
$path = $product->getData($imageType);
54+
55+
$image = $this->productImageFactory->create();
56+
$image->setDestinationSubdir($imageType)
57+
->setBaseFile($path);
58+
$imageUrl = $image->getUrl();
59+
60+
return [
61+
'url' => $imageUrl,
62+
'path' => $path,
63+
];
64+
}
65+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
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")
260260
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")
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\\Image")
262262
thumbnail: String @doc(description: "The relative path to the product's thumbnail image")
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")
@@ -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")

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoryTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ public function testCategoryProducts()
225225
}
226226
short_description
227227
sku
228-
small_image
228+
small_image {
229+
path
230+
}
229231
small_image_label
230232
special_from_date
231233
special_price
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\GraphQl\Catalog;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
12+
class MediaGalleryTest extends GraphQlAbstract
13+
{
14+
/**
15+
* @var \Magento\TestFramework\ObjectManager
16+
*/
17+
private $objectManager;
18+
19+
protected function setUp()
20+
{
21+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
22+
}
23+
24+
/**
25+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php
26+
*/
27+
public function testProductSmallImageUrlWithExistingImage()
28+
{
29+
$productSku = 'simple';
30+
$query = <<<QUERY
31+
{
32+
products(filter: {sku: {eq: "{$productSku}"}}) {
33+
items {
34+
small_image {
35+
url
36+
}
37+
}
38+
}
39+
}
40+
QUERY;
41+
$response = $this->graphQlQuery($query);
42+
43+
self::assertArrayHasKey('url', $response['products']['items'][0]['small_image']);
44+
self::assertContains('magento_image.jpg', $response['products']['items'][0]['small_image']['url']);
45+
self::assertTrue($this->checkImageExists($response['products']['items'][0]['small_image']['url']));
46+
}
47+
48+
/**
49+
* @param string $url
50+
* @return bool
51+
*/
52+
private function checkImageExists(string $url): bool
53+
{
54+
$connection = curl_init($url);
55+
curl_setopt($connection, CURLOPT_HEADER, true);
56+
curl_setopt($connection, CURLOPT_NOBODY, true);
57+
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
58+
curl_exec($connection);
59+
$responseStatus = curl_getinfo($connection, CURLINFO_HTTP_CODE);
60+
61+
return $responseStatus === 200 ? true : false;
62+
}
63+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductViewTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ public function testQueryAllFieldsSimpleProduct()
205205
}
206206
short_description
207207
sku
208-
small_image
208+
small_image {
209+
path
210+
}
209211
small_image_label
210212
special_from_date
211213
special_price
@@ -484,7 +486,7 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
484486
QUERY;
485487

486488
$response = $this->graphQlQuery($query);
487-
489+
488490
/**
489491
* @var ProductRepositoryInterface $productRepository
490492
*/

0 commit comments

Comments
 (0)