Skip to content

Commit

Permalink
graphQl-44: added ProductTextAttribute and fixed api functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliyboyko committed Oct 24, 2018
1 parent a18a087 commit 0a4dc35
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product;

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

/**
* Resolve rendered content for text attributes
*/
class ProductTextAttribute implements ResolverInterface
{
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): array {
$value['field'] = $field->getName();

return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Model\Product;
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\Helper\Output as OutputHelper;
Expand Down Expand Up @@ -41,14 +42,17 @@ public function resolve(
ResolveInfo $info,
array $value = null,
array $args = null
): array {
): ?string {
if (!isset($value['model'])) {
return [];
throw new GraphQlInputException(__('"model" value should be specified'));
}
if (!isset($value['field'])) {
throw new GraphQlInputException(__('"field" value should be specified'));
}

/* @var $product Product */
$product = $value['model'];
$fieldName = $field->getName();
$fieldName = $value['field'];
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);

return $renderedValue;
Expand Down
4 changes: 2 additions & 2 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: ProductTextAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextAttribute")
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")
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
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->assertTextEavAttributes($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 @@ -917,11 +922,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 @@ -943,6 +946,31 @@ private function assertEavAttributes($product, $actualResponse)
$this->assertResponseFields($actualResponse, $assertionMap);
}

/**
* @param ProductInterface $product
* @param array $actualResponse
*/
private function assertTextEavAttributes($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 0a4dc35

Please sign in to comment.