Skip to content

Commit 43804b9

Browse files
author
Valeriy Nayda
committed
Merge branch 'graphQl-44-product-textarea-field-format' of github.com:magento/graphql-ce into graphQl-44-product-textarea-field-format
2 parents cac1016 + f82912d commit 43804b9

File tree

9 files changed

+89
-157
lines changed

9 files changed

+89
-157
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductTextAttribute.php renamed to app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductComplexTextAttribute.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,51 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
99

10-
use Magento\Catalog\Model\Product;
11-
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\FormatList;
1210
use Magento\Framework\GraphQl\Config\Element\Field;
13-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1411
use Magento\Framework\GraphQl\Query\ResolverInterface;
1512
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\Catalog\Model\Product;
14+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15+
use Magento\Catalog\Helper\Output as OutputHelper;
1616

1717
/**
1818
* Resolve rendered content for attributes where HTML content is allowed
1919
*/
20-
class ProductTextAttribute implements ResolverInterface
20+
class ProductComplexTextAttribute implements ResolverInterface
2121
{
2222
/**
23-
* @var FormatList
23+
* @var OutputHelper
2424
*/
25-
private $formatList;
25+
private $outputHelper;
2626

2727
/**
28-
* @var string
29-
*/
30-
private $defaultFormat = 'html';
31-
32-
/**
33-
* @param FormatList $formatList
28+
* @param OutputHelper $outputHelper
3429
*/
3530
public function __construct(
36-
FormatList $formatList
31+
OutputHelper $outputHelper
3732
) {
38-
$this->formatList = $formatList;
33+
$this->outputHelper = $outputHelper;
3934
}
4035

4136
/**
42-
* {@inheritdoc}
37+
* @inheritdoc
4338
*/
4439
public function resolve(
4540
Field $field,
4641
$context,
4742
ResolveInfo $info,
4843
array $value = null,
4944
array $args = null
50-
) {
45+
): array {
5146
if (!isset($value['model'])) {
5247
throw new GraphQlInputException(__('"model" value should be specified'));
5348
}
5449

5550
/* @var $product Product */
5651
$product = $value['model'];
5752
$fieldName = $field->getName();
58-
$formatIdentifier = $args['format'] ?? $this->defaultFormat;
59-
$format = $this->formatList->create($formatIdentifier);
60-
$result = ['content' => $format->getContent($product, $fieldName)];
53+
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
6154

62-
return $result;
55+
return ['html' => $renderedValue];
6356
}
6457
}

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@
6464
<argument name="collectionProcessor" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor</argument>
6565
</arguments>
6666
</type>
67-
<type name="Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\FormatList">
68-
<arguments>
69-
<argument name="formats" xsi:type="array">
70-
<item name="html" xsi:type="string">Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\Html</item>
71-
</argument>
72-
</arguments>
73-
</type>
7467
<virtualType name="Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\ProductFilterProcessor" type="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
7568
<arguments>
7669
<argument name="customFilters" xsi:type="array">

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

Lines changed: 2 additions & 9 deletions
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: ComplexTextValue @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductComplexTextAttribute")
252+
short_description: ComplexTextValue @doc(description: "A short description of the product. Its use depends on the theme.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductComplexTextAttribute")
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")
@@ -556,10 +556,3 @@ type SortFields @doc(description: "SortFields contains a default value for sort
556556
default: String @doc(description: "Default value of sort fields")
557557
options: [SortField] @doc(description: "Available sort fields")
558558
}
559-
560-
type ProductTextAttribute {
561-
content (
562-
format: String @doc(description: "The format of content")
563-
): String @doc(description: "The format of content")
564-
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextAttribute")
565-
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Model\Resolver\ComplexTextValue;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
15+
/**
16+
* HTML format for complex text value.
17+
*
18+
* Initially, a value from parent resolver should be in HTML format, therefore, there is no any customization.
19+
*/
20+
class HtmlFormat implements ResolverInterface
21+
{
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function resolve(
26+
Field $field,
27+
$context,
28+
ResolveInfo $info,
29+
array $value = null,
30+
array $args = null
31+
): ?string {
32+
if (!isset($value['html'])) {
33+
throw new GraphQlInputException(__('"html" value should be specified'));
34+
}
35+
36+
return $value['html'];
37+
}
38+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ enum SortEnum @doc(description: "This enumeration indicates whether to return re
3636
ASC
3737
DESC
3838
}
39+
40+
type ComplexTextValue {
41+
html: String! @doc(description: "HTML format") @resolver(class: "\\Magento\\GraphQl\\Model\\Resolver\\ComplexTextValue\\HtmlFormat")
42+
}

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

Lines changed: 32 additions & 4 deletions
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->assertComplexTextAttributes($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]);
@@ -918,11 +923,9 @@ private function assertEavAttributes($product, $actualResponse)
918923
{
919924
$eavAttributes = [
920925
'url_key',
921-
'description',
922926
'meta_description',
923927
'meta_keyword',
924928
'meta_title',
925-
'short_description',
926929
'country_of_manufacture',
927930
'gift_message_available',
928931
'news_from_date',
@@ -944,6 +947,31 @@ private function assertEavAttributes($product, $actualResponse)
944947
$this->assertResponseFields($actualResponse, $assertionMap);
945948
}
946949

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

0 commit comments

Comments
 (0)