Skip to content

Commit

Permalink
Merge pull request #3195 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
Valeriy Naida authored Sep 26, 2018
2 parents 547ad4a + 25285a3 commit 76e18b9
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 14 deletions.
65 changes: 65 additions & 0 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\ImageFactory;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Returns product's image. If the image is not set, returns a placeholder
*/
class Image implements ResolverInterface
{
/**
* Product image factory
*
* @var ImageFactory
*/
private $productImageFactory;

/**
* @param ImageFactory $productImageFactory
*/
public function __construct(
ImageFactory $productImageFactory
) {
$this->productImageFactory = $productImageFactory;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): array {
if (!isset($value['model'])) {
throw new \LogicException(__('"model" value should be specified'));
}
/** @var Product $product */
$product = $value['model'];
$imageType = $field->getName();
$path = $product->getData($imageType);

$image = $this->productImageFactory->create();
$image->setDestinationSubdir($imageType)
->setBaseFile($path);
$imageUrl = $image->getUrl();

return [
'url' => $imageUrl,
'path' => $path,
];
}
}
7 changes: 6 additions & 1 deletion app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
meta_keyword: String @doc(description: "A comma-separated list of keywords that are visible only to search engines")
meta_description: String @doc(description: "A brief overview of the product for search results listings, maximum 255 characters")
image: String @doc(description: "The relative path to the main image on the product page")
small_image: String @doc(description: "The relative path to the small image, which is used on catalog pages")
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")
thumbnail: String @doc(description: "The relative path to the product's thumbnail image")
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")
new_to_date: String @doc(description: "The end date for new product listings") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\NewFromTo")
Expand Down Expand Up @@ -352,6 +352,11 @@ type CustomizableFileValue @doc(description: "CustomizableFileValue defines the
image_size_y: Int @doc(description: "The maximum height of an image")
}

type ProductImage @doc(description: "Product image information. Contains image relative path and URL") {
url: String
path: String
}

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.") {
title: String @doc(description: "The display name for this option")
required: Boolean @doc(description: "Indicates whether the option is required")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ public function testCategoryProducts()
}
short_description
sku
small_image
small_image {
path
}
small_image_label
special_from_date
special_price
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Catalog;

use Magento\TestFramework\TestCase\GraphQlAbstract;

class MediaGalleryTest extends GraphQlAbstract
{
/**
* @var \Magento\TestFramework\ObjectManager
*/
private $objectManager;

protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php
*/
public function testProductSmallImageUrlWithExistingImage()
{
$productSku = 'simple';
$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}) {
items {
small_image {
url
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertArrayHasKey('url', $response['products']['items'][0]['small_image']);
self::assertContains('magento_image.jpg', $response['products']['items'][0]['small_image']['url']);
self::assertTrue($this->checkImageExists($response['products']['items'][0]['small_image']['url']));
}

/**
* @param string $url
* @return bool
*/
private function checkImageExists(string $url): bool
{
$connection = curl_init($url);
curl_setopt($connection, CURLOPT_HEADER, true);
curl_setopt($connection, CURLOPT_NOBODY, true);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
curl_exec($connection);
$responseStatus = curl_getinfo($connection, CURLINFO_HTTP_CODE);

return $responseStatus === 200 ? true : false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ public function testQueryAllFieldsSimpleProduct()
}
short_description
sku
small_image
small_image {
path
}
small_image_label
special_from_date
special_price
Expand Down Expand Up @@ -484,7 +486,7 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
QUERY;

$response = $this->graphQlQuery($query);

/**
* @var ProductRepositoryInterface $productRepository
*/
Expand Down
Loading

0 comments on commit 76e18b9

Please sign in to comment.