Skip to content

Commit 60902d9

Browse files
ENGCOM-2948: Small image URL added to the product data #132
- Merge Pull Request magento/graphql-ce#132 from magento/graphql-ce:88-small-image-url - Merged commits: 1. 1288659 2. ca13aa8 3. f41eb3e 4. fe6d7f8 5. a60e55d 6. 6877798 7. 646f248 8. f8b59e7 9. a027ae7 10. c53bb4f 11. 5a674c7 12. 4de3a9e 13. 0ea7d50 14. a4c941e 15. a6d7d45 16. ca8c7fc 17. d860874 18. 45cc658 19. dad6575
2 parents 8fd89cf + dad6575 commit 60902d9

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