Skip to content

Commit 8aedb95

Browse files
author
vitaliyboyko
committed
GraphQl-44: prototype of format strategy
Signed-off-by: vitaliyboyko <v.boyko@atwix.com>
1 parent 506c148 commit 8aedb95

File tree

5 files changed

+152
-27
lines changed

5 files changed

+152
-27
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductHtmlAttribute.php renamed to app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductTextareaAttribute.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,40 @@
88
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
99

1010
use Magento\Catalog\Model\Product;
11+
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\FormatFactory;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
1213
use Magento\Framework\GraphQl\Query\Resolver\Value;
1314
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
1516
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16-
use Magento\Catalog\Helper\Output as OutputHelper;
1717

1818
/**
1919
* Resolve rendered content for attributes where HTML content is allowed
2020
*/
21-
class ProductHtmlAttribute implements ResolverInterface
21+
class ProductTextareaAttribute implements ResolverInterface
2222
{
23+
const DEFAULT_CONTENT_FORMAT_IDENTIFIER = 'html';
24+
2325
/**
2426
* @var ValueFactory
2527
*/
2628
private $valueFactory;
2729

2830
/**
29-
* @var OutputHelper
31+
* @var FormatFactory
3032
*/
31-
private $outputHelper;
33+
private $formatFactory;
3234

3335
/**
3436
* @param ValueFactory $valueFactory
35-
* @param OutputHelper $outputHelper
37+
* @param FormatFactory $formatFactory
3638
*/
3739
public function __construct(
3840
ValueFactory $valueFactory,
39-
OutputHelper $outputHelper
41+
FormatFactory $formatFactory
4042
) {
4143
$this->valueFactory = $valueFactory;
42-
$this->outputHelper = $outputHelper;
44+
$this->formatFactory = $formatFactory;
4345
}
4446

4547
/**
@@ -62,9 +64,12 @@ public function resolve(
6264
/* @var $product Product */
6365
$product = $value['model'];
6466
$fieldName = $field->getName();
65-
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
66-
$result = function () use ($renderedValue) {
67-
return $renderedValue;
67+
$formatIdentifier = $args['format'] ?? self::DEFAULT_CONTENT_FORMAT_IDENTIFIER;
68+
$format = $this->formatFactory->create($formatIdentifier);
69+
$attribute = ['content' => $format->getContent($product, $fieldName)];
70+
71+
$result = function () use ($attribute) {
72+
return $attribute;
6873
};
6974

7075
return $this->valueFactory->create($result);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\ProductTextareaAttribute;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
12+
class FormatFactory
13+
{
14+
/**
15+
* @var ObjectManagerInterface
16+
*/
17+
private $objectManager;
18+
19+
/**
20+
* @param ObjectManagerInterface $objectManager
21+
*/
22+
public function __construct(ObjectManagerInterface $objectManager)
23+
{
24+
$this->objectManager = $objectManager;
25+
}
26+
27+
/**
28+
* @param string $formatIdentifier
29+
* @param array $data
30+
* @return FormatInterface
31+
*/
32+
public function create(string $formatIdentifier, $data = []) : FormatInterface
33+
{
34+
$formatClassName = 'Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\\' . ucfirst($formatIdentifier);
35+
$formatInstance = $this->objectManager->create($formatClassName, $data);
36+
if (false == $formatInstance instanceof FormatInterface) {
37+
throw new \InvalidArgumentException(
38+
$formatInstance . ' is not instance of \Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\FormatInterface'
39+
);
40+
}
41+
return $formatInstance;
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\ProductTextareaAttribute;
9+
10+
use Magento\Catalog\Model\Product as ModelProduct;
11+
12+
interface FormatInterface
13+
{
14+
/**
15+
* @param ModelProduct $product
16+
* @param string $fieldName
17+
* @return string
18+
*/
19+
public function getContent(
20+
ModelProduct $product,
21+
string $fieldName
22+
): string;
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\ProductTextareaAttribute;
9+
10+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
11+
use Magento\Catalog\Helper\Output as OutputHelper;
12+
use Magento\Catalog\Model\Product as ModelProduct;
13+
14+
class Html implements FormatInterface
15+
{
16+
/**
17+
* @var ValueFactory
18+
*/
19+
private $valueFactory;
20+
21+
/**
22+
* @var OutputHelper
23+
*/
24+
private $outputHelper;
25+
26+
/**
27+
* @param ValueFactory $valueFactory
28+
* @param OutputHelper $outputHelper
29+
*/
30+
public function __construct(
31+
ValueFactory $valueFactory,
32+
OutputHelper $outputHelper
33+
) {
34+
$this->valueFactory = $valueFactory;
35+
$this->outputHelper = $outputHelper;
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getContent(
42+
ModelProduct $product,
43+
string $fieldName
44+
): string {
45+
return $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
46+
}
47+
}

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
type Query {
55
products (
6-
search: String @doc(description: "Performs a full-text search using the specified key words."),
7-
filter: ProductFilterInput @doc(description: "Identifies which product attributes to search for and return."),
8-
pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
9-
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
10-
sort: ProductSortInput @doc(description: "Specifies which attribute to sort on, and whether to return the results in ascending or descending order.")
11-
): Products
12-
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Products") @doc(description: "The products query searches for products that match the criteria specified in the search and filter attributes")
6+
search: String @doc(description: "Performs a full-text search using the specified key words."),
7+
filter: ProductFilterInput @doc(description: "Identifies which product attributes to search for and return."),
8+
pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
9+
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
10+
sort: ProductSortInput @doc(description: "Specifies which attribute to sort on, and whether to return the results in ascending or descending order.")
11+
): Products
12+
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Products") @doc(description: "The products query searches for products that match the criteria specified in the search and filter attributes")
1313
category (
14-
id: Int @doc(description: "Id of the category")
15-
): CategoryTree
14+
id: Int @doc(description: "Id of the category")
15+
): CategoryTree
1616
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree")
1717
}
1818

@@ -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: String @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductHtmlAttribute")
252-
short_description: String @doc(description: "A short description of the product. Its use depends on the theme.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductHtmlAttribute")
251+
description: ProductTextareaAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.")
252+
short_description: ProductTextareaAttribute @doc(description: "A short description of the product. Its use depends on the theme.")
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")
@@ -377,10 +377,10 @@ interface CategoryInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model
377377
product_count: Int @doc(description: "The number of products in the category")
378378
default_sort_by: String @doc(description: "The attribute to use for sorting")
379379
products(
380-
pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
381-
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
382-
sort: ProductSortInput @doc(description: "Specifies which attribute to sort on, and whether to return the results in ascending or descending order.")
383-
): CategoryProducts @doc(description: "The list of products assigned to the category") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\Products")
380+
pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
381+
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
382+
sort: ProductSortInput @doc(description: "Specifies which attribute to sort on, and whether to return the results in ascending or descending order.")
383+
): CategoryProducts @doc(description: "The list of products assigned to the category") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\Products")
384384
breadcrumbs: [Breadcrumb] @doc(description: "Breadcrumbs, parent categories info") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\Breadcrumbs")
385385
}
386386

@@ -408,8 +408,8 @@ type VirtualProduct implements ProductInterface, CustomizableProductInterface @d
408408
}
409409

410410
type SimpleProduct implements ProductInterface, PhysicalProductInterface, CustomizableProductInterface @doc(description: "A simple product is tangible and are usually sold as single units or in fixed quantities")
411-
{
412-
}
411+
{
412+
}
413413

414414
type Products @doc(description: "The Products object is the top-level object returned in a product search") {
415415
items: [ProductInterface] @doc(description: "An array of products that match the specified search criteria")
@@ -551,3 +551,10 @@ type SortFields @doc(description: "SortFields contains a default value for sort
551551
default: String @doc(description: "Default value of sort fields")
552552
options: [SortField] @doc(description: "Available sort fields")
553553
}
554+
555+
type ProductTextareaAttribute {
556+
content (
557+
format: String! @doc(description: "The format of content")
558+
): String
559+
@resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextareaAttribute")
560+
}

0 commit comments

Comments
 (0)