Skip to content

Commit 2fcc183

Browse files
author
vitaliyboyko
committed
graphQl-44: added ComplexTextValue to graphQl module
1 parent 0a4dc35 commit 2fcc183

File tree

6 files changed

+71
-76
lines changed

6 files changed

+71
-76
lines changed

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,29 @@
1010
use Magento\Framework\GraphQl\Config\Element\Field;
1111
use Magento\Framework\GraphQl\Query\ResolverInterface;
1212
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;
1316

1417
/**
15-
* Resolve rendered content for text attributes
18+
* Resolve rendered content for attributes where HTML content is allowed
1619
*/
1720
class ProductTextAttribute implements ResolverInterface
1821
{
22+
/**
23+
* @var OutputHelper
24+
*/
25+
private $outputHelper;
26+
27+
/**
28+
* @param OutputHelper $outputHelper
29+
*/
30+
public function __construct(
31+
OutputHelper $outputHelper
32+
) {
33+
$this->outputHelper = $outputHelper;
34+
}
35+
1936
/**
2037
* @inheritdoc
2138
*/
@@ -26,8 +43,15 @@ public function resolve(
2643
array $value = null,
2744
array $args = null
2845
): array {
29-
$value['field'] = $field->getName();
46+
if (!isset($value['model'])) {
47+
throw new GraphQlInputException(__('"model" value should be specified'));
48+
}
49+
50+
/* @var $product Product */
51+
$product = $value['model'];
52+
$fieldName = $field->getName();
53+
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
3054

31-
return $value;
55+
return ['html' => $renderedValue];
3256
}
3357
}

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

-60
This file was deleted.

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

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

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

+2-6
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.") @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")
251+
description: ComplexTextValue @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: ComplexTextValue @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")
@@ -556,7 +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 @doc(description: "Product text attribute.") {
561-
html: String @doc(description: "Attribute HTML content") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextAttribute\\HtmlContent")
562-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* @author Atwix Team
4+
* @copyright Copyright (c) 2018 Atwix (https://www.atwix.com/)
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

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

0 commit comments

Comments
 (0)