Skip to content

Commit

Permalink
Merge branch 'graphQl-44-product-textarea-field-format' of github.com…
Browse files Browse the repository at this point in the history
…:magento/graphql-ce into graphQl-44-product-textarea-field-format
  • Loading branch information
Valeriy Nayda committed Nov 6, 2018
2 parents cac1016 + f82912d commit 43804b9
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,51 @@

namespace Magento\CatalogGraphQl\Model\Resolver\Product;

use Magento\Catalog\Model\Product;
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\FormatList;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Model\Product;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Catalog\Helper\Output as OutputHelper;

/**
* Resolve rendered content for attributes where HTML content is allowed
*/
class ProductTextAttribute implements ResolverInterface
class ProductComplexTextAttribute implements ResolverInterface
{
/**
* @var FormatList
* @var OutputHelper
*/
private $formatList;
private $outputHelper;

/**
* @var string
*/
private $defaultFormat = 'html';

/**
* @param FormatList $formatList
* @param OutputHelper $outputHelper
*/
public function __construct(
FormatList $formatList
OutputHelper $outputHelper
) {
$this->formatList = $formatList;
$this->outputHelper = $outputHelper;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
): array {
if (!isset($value['model'])) {
throw new GraphQlInputException(__('"model" value should be specified'));
}

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

return $result;
return ['html' => $renderedValue];
}
}

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions app/code/Magento/CatalogGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@
<argument name="collectionProcessor" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor</argument>
</arguments>
</type>
<type name="Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\FormatList">
<arguments>
<argument name="formats" xsi:type="array">
<item name="html" xsi:type="string">Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\Html</item>
</argument>
</arguments>
</type>
<virtualType name="Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\ProductFilterProcessor" type="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
<arguments>
<argument name="customFilters" xsi:type="array">
Expand Down
11 changes: 2 additions & 9 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
id: Int @doc(description: "The ID number assigned to the product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\EntityIdToId")
name: String @doc(description: "The product name. Customers use this name to identify the product.")
sku: String @doc(description: "A number or code assigned to a product to identify the product, options, price, and manufacturer")
description: ProductTextAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.")
short_description: ProductTextAttribute @doc(description: "A short description of the product. Its use depends on the theme.")
description: ComplexTextValue @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductComplexTextAttribute")
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")
special_price: Float @doc(description: "The discounted price of the product")
special_from_date: String @doc(description: "The beginning date that a product has a special price")
special_to_date: String @doc(description: "The end date that a product has a special price")
Expand Down Expand Up @@ -556,10 +556,3 @@ type SortFields @doc(description: "SortFields contains a default value for sort
default: String @doc(description: "Default value of sort fields")
options: [SortField] @doc(description: "Available sort fields")
}

type ProductTextAttribute {
content (
format: String @doc(description: "The format of content")
): String @doc(description: "The format of content")
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextAttribute")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Model\Resolver\ComplexTextValue;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* HTML format for complex text value.
*
* Initially, a value from parent resolver should be in HTML format, therefore, there is no any customization.
*/
class HtmlFormat implements ResolverInterface
{
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): ?string {
if (!isset($value['html'])) {
throw new GraphQlInputException(__('"html" value should be specified'));
}

return $value['html'];
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/GraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ enum SortEnum @doc(description: "This enumeration indicates whether to return re
ASC
DESC
}

type ComplexTextValue {
html: String! @doc(description: "HTML format") @resolver(class: "\\Magento\\GraphQl\\Model\\Resolver\\ComplexTextValue\\HtmlFormat")
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function testQueryAllFieldsSimpleProduct()
attribute_set_id
country_of_manufacture
created_at
description
description {
html
}
gift_message_available
id
categories {
Expand Down Expand Up @@ -203,7 +205,9 @@ public function testQueryAllFieldsSimpleProduct()
position
sku
}
short_description
short_description {
html
}
sku
small_image {
path
Expand Down Expand Up @@ -262,6 +266,7 @@ public function testQueryAllFieldsSimpleProduct()
$this->assertArrayHasKey(0, $response['products']['items']);
$this->assertBaseFields($product, $response['products']['items'][0]);
$this->assertEavAttributes($product, $response['products']['items'][0]);
$this->assertComplexTextAttributes($product, $response['products']['items'][0]);
$this->assertOptions($product, $response['products']['items'][0]);
$this->assertTierPrices($product, $response['products']['items'][0]);
$this->assertArrayHasKey('websites', $response['products']['items'][0]);
Expand Down Expand Up @@ -918,11 +923,9 @@ private function assertEavAttributes($product, $actualResponse)
{
$eavAttributes = [
'url_key',
'description',
'meta_description',
'meta_keyword',
'meta_title',
'short_description',
'country_of_manufacture',
'gift_message_available',
'news_from_date',
Expand All @@ -944,6 +947,31 @@ private function assertEavAttributes($product, $actualResponse)
$this->assertResponseFields($actualResponse, $assertionMap);
}

/**
* @param ProductInterface $product
* @param array $actualResponse
*/
private function assertComplexTextAttributes($product, $actualResponse)
{
$eavAttributes = [
'description',
'short_description',
];
$assertionMap = [];
foreach ($eavAttributes as $attributeCode) {
$expectedAttribute = $product->getCustomAttribute($attributeCode);

$assertionMap[] = [
'response_field' => $this->eavAttributesToGraphQlSchemaFieldTranslator($attributeCode),
'expected_value' => $expectedAttribute ? [
'html' => $expectedAttribute->getValue()
] : null
];
}

$this->assertResponseFields($actualResponse, $assertionMap);
}

/**
* @param string $eavAttributeCode
* @return string
Expand Down

0 comments on commit 43804b9

Please sign in to comment.