Skip to content

Commit 0a4dc35

Browse files
author
vitaliyboyko
committed
graphQl-44: added ProductTextAttribute and fixed api functional test
1 parent a18a087 commit 0a4dc35

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Query\ResolverInterface;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
14+
/**
15+
* Resolve rendered content for text attributes
16+
*/
17+
class ProductTextAttribute implements ResolverInterface
18+
{
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function resolve(
23+
Field $field,
24+
$context,
25+
ResolveInfo $info,
26+
array $value = null,
27+
array $args = null
28+
): array {
29+
$value['field'] = $field->getName();
30+
31+
return $value;
32+
}
33+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductTextAttribute/HtmlContent.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\Product;
1111
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1213
use Magento\Framework\GraphQl\Query\ResolverInterface;
1314
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1415
use Magento\Catalog\Helper\Output as OutputHelper;
@@ -41,14 +42,17 @@ public function resolve(
4142
ResolveInfo $info,
4243
array $value = null,
4344
array $args = null
44-
): array {
45+
): ?string {
4546
if (!isset($value['model'])) {
46-
return [];
47+
throw new GraphQlInputException(__('"model" value should be specified'));
48+
}
49+
if (!isset($value['field'])) {
50+
throw new GraphQlInputException(__('"field" value should be specified'));
4751
}
4852

4953
/* @var $product Product */
5054
$product = $value['model'];
51-
$fieldName = $field->getName();
55+
$fieldName = $value['field'];
5256
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
5357

5458
return $renderedValue;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
248248
id: Int @doc(description: "The ID number assigned to the product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\EntityIdToId")
249249
name: String @doc(description: "The product name. Customers use this name to identify the product.")
250250
sku: String @doc(description: "A number or code assigned to a product to identify the product, options, price, and manufacturer")
251-
description: ProductTextAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.")
252-
short_description: ProductTextAttribute @doc(description: "A short description of the product. Its use depends on the theme.")
251+
description: ProductTextAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextAttribute")
252+
short_description: ProductTextAttribute @doc(description: "A short description of the product. Its use depends on the theme.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextAttribute")
253253
special_price: Float @doc(description: "The discounted price of the product")
254254
special_from_date: String @doc(description: "The beginning date that a product has a special price")
255255
special_to_date: String @doc(description: "The end date that a product has a special price")

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

+32-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public function testQueryAllFieldsSimpleProduct()
4444
attribute_set_id
4545
country_of_manufacture
4646
created_at
47-
description
47+
description {
48+
html
49+
}
4850
gift_message_available
4951
id
5052
categories {
@@ -203,7 +205,9 @@ public function testQueryAllFieldsSimpleProduct()
203205
position
204206
sku
205207
}
206-
short_description
208+
short_description {
209+
html
210+
}
207211
sku
208212
small_image {
209213
path
@@ -262,6 +266,7 @@ public function testQueryAllFieldsSimpleProduct()
262266
$this->assertArrayHasKey(0, $response['products']['items']);
263267
$this->assertBaseFields($product, $response['products']['items'][0]);
264268
$this->assertEavAttributes($product, $response['products']['items'][0]);
269+
$this->assertTextEavAttributes($product, $response['products']['items'][0]);
265270
$this->assertOptions($product, $response['products']['items'][0]);
266271
$this->assertTierPrices($product, $response['products']['items'][0]);
267272
$this->assertArrayHasKey('websites', $response['products']['items'][0]);
@@ -917,11 +922,9 @@ private function assertEavAttributes($product, $actualResponse)
917922
{
918923
$eavAttributes = [
919924
'url_key',
920-
'description',
921925
'meta_description',
922926
'meta_keyword',
923927
'meta_title',
924-
'short_description',
925928
'country_of_manufacture',
926929
'gift_message_available',
927930
'news_from_date',
@@ -943,6 +946,31 @@ private function assertEavAttributes($product, $actualResponse)
943946
$this->assertResponseFields($actualResponse, $assertionMap);
944947
}
945948

949+
/**
950+
* @param ProductInterface $product
951+
* @param array $actualResponse
952+
*/
953+
private function assertTextEavAttributes($product, $actualResponse)
954+
{
955+
$eavAttributes = [
956+
'description',
957+
'short_description',
958+
];
959+
$assertionMap = [];
960+
foreach ($eavAttributes as $attributeCode) {
961+
$expectedAttribute = $product->getCustomAttribute($attributeCode);
962+
963+
$assertionMap[] = [
964+
'response_field' => $this->eavAttributesToGraphQlSchemaFieldTranslator($attributeCode),
965+
'expected_value' => $expectedAttribute ? [
966+
'html' => $expectedAttribute->getValue()
967+
] : null
968+
];
969+
}
970+
971+
$this->assertResponseFields($actualResponse, $assertionMap);
972+
}
973+
946974
/**
947975
* @param string $eavAttributeCode
948976
* @return string

0 commit comments

Comments
 (0)