Skip to content

Commit

Permalink
GraphQl-44: prototype of format strategy
Browse files Browse the repository at this point in the history
Signed-off-by: vitaliyboyko <v.boyko@atwix.com>
  • Loading branch information
vitaliyboyko committed Sep 16, 2018
1 parent 506c148 commit 8aedb95
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,40 @@
namespace Magento\CatalogGraphQl\Model\Resolver\Product;

use Magento\Catalog\Model\Product;
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\FormatFactory;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Helper\Output as OutputHelper;

/**
* Resolve rendered content for attributes where HTML content is allowed
*/
class ProductHtmlAttribute implements ResolverInterface
class ProductTextareaAttribute implements ResolverInterface
{
const DEFAULT_CONTENT_FORMAT_IDENTIFIER = 'html';

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @var OutputHelper
* @var FormatFactory
*/
private $outputHelper;
private $formatFactory;

/**
* @param ValueFactory $valueFactory
* @param OutputHelper $outputHelper
* @param FormatFactory $formatFactory
*/
public function __construct(
ValueFactory $valueFactory,
OutputHelper $outputHelper
FormatFactory $formatFactory
) {
$this->valueFactory = $valueFactory;
$this->outputHelper = $outputHelper;
$this->formatFactory = $formatFactory;
}

/**
Expand All @@ -62,9 +64,12 @@ public function resolve(
/* @var $product Product */
$product = $value['model'];
$fieldName = $field->getName();
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
$result = function () use ($renderedValue) {
return $renderedValue;
$formatIdentifier = $args['format'] ?? self::DEFAULT_CONTENT_FORMAT_IDENTIFIER;
$format = $this->formatFactory->create($formatIdentifier);
$attribute = ['content' => $format->getContent($product, $fieldName)];

$result = function () use ($attribute) {
return $attribute;
};

return $this->valueFactory->create($result);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute;

use Magento\Framework\ObjectManagerInterface;

class FormatFactory
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* @param string $formatIdentifier
* @param array $data
* @return FormatInterface
*/
public function create(string $formatIdentifier, $data = []) : FormatInterface
{
$formatClassName = 'Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\\' . ucfirst($formatIdentifier);
$formatInstance = $this->objectManager->create($formatClassName, $data);
if (false == $formatInstance instanceof FormatInterface) {
throw new \InvalidArgumentException(
$formatInstance . ' is not instance of \Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\FormatInterface'
);
}
return $formatInstance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute;

use Magento\Catalog\Model\Product as ModelProduct;

interface FormatInterface
{
/**
* @param ModelProduct $product
* @param string $fieldName
* @return string
*/
public function getContent(
ModelProduct $product,
string $fieldName
): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute;

use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Catalog\Helper\Output as OutputHelper;
use Magento\Catalog\Model\Product as ModelProduct;

class Html implements FormatInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @var OutputHelper
*/
private $outputHelper;

/**
* @param ValueFactory $valueFactory
* @param OutputHelper $outputHelper
*/
public function __construct(
ValueFactory $valueFactory,
OutputHelper $outputHelper
) {
$this->valueFactory = $valueFactory;
$this->outputHelper = $outputHelper;
}

/**
* @inheritdoc
*/
public function getContent(
ModelProduct $product,
string $fieldName
): string {
return $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
}
}
41 changes: 24 additions & 17 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

type Query {
products (
search: String @doc(description: "Performs a full-text search using the specified key words."),
filter: ProductFilterInput @doc(description: "Identifies which product attributes to search for and return."),
pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
sort: ProductSortInput @doc(description: "Specifies which attribute to sort on, and whether to return the results in ascending or descending order.")
): Products
@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")
search: String @doc(description: "Performs a full-text search using the specified key words."),
filter: ProductFilterInput @doc(description: "Identifies which product attributes to search for and return."),
pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
sort: ProductSortInput @doc(description: "Specifies which attribute to sort on, and whether to return the results in ascending or descending order.")
): Products
@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")
category (
id: Int @doc(description: "Id of the category")
): CategoryTree
id: Int @doc(description: "Id of the category")
): CategoryTree
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree")
}

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

Expand Down Expand Up @@ -408,8 +408,8 @@ type VirtualProduct implements ProductInterface, CustomizableProductInterface @d
}

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

type Products @doc(description: "The Products object is the top-level object returned in a product search") {
items: [ProductInterface] @doc(description: "An array of products that match the specified search criteria")
Expand Down Expand Up @@ -551,3 +551,10 @@ 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 ProductTextareaAttribute {
content (
format: String! @doc(description: "The format of content")
): String
@resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextareaAttribute")
}

0 comments on commit 8aedb95

Please sign in to comment.