Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit da5a9ed

Browse files
author
Valeriy Naida
authored
Merge pull request #3239 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents d2adcdd + 8a037d8 commit da5a9ed

30 files changed

+1289
-129
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Product\Option;
9+
10+
use Magento\Catalog\Model\Product\Option\Type\Date as ProductDateOptionType;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Stdlib\DateTime;
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
class DateType extends ProductDateOptionType
18+
{
19+
/**
20+
* Make valid string as a value of date option type for GraphQl queries
21+
*
22+
* @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...)
23+
* @return ProductDateOptionType
24+
*/
25+
public function validateUserValue($values)
26+
{
27+
if ($this->_dateExists() || $this->_timeExists()) {
28+
return parent::validateUserValue($this->formatValues($values));
29+
}
30+
31+
return $this;
32+
}
33+
34+
/**
35+
* Format date value from string to date array
36+
*
37+
* @param [] $values
38+
* @return []
39+
* @throws LocalizedException
40+
*/
41+
private function formatValues($values)
42+
{
43+
if (isset($values[$this->getOption()->getId()])) {
44+
$value = $values[$this->getOption()->getId()];
45+
$dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value);
46+
$values[$this->getOption()->getId()] = [
47+
'date' => $value,
48+
'year' => $dateTime->format('Y'),
49+
'month' => $dateTime->format('m'),
50+
'day' => $dateTime->format('d'),
51+
'hour' => $dateTime->format('H'),
52+
'minute' => $dateTime->format('i'),
53+
'day_part' => $dateTime->format('a'),
54+
];
55+
}
56+
57+
return $values;
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
public function useCalendar()
64+
{
65+
return false;
66+
}
67+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Categories.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct(
7979
}
8080

8181
/**
82-
* {@inheritdoc}
82+
* @inheritdoc
8383
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8484
*/
8585
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
@@ -114,6 +114,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
114114
$categories[$item->getId()] = $this->customAttributesFlattener
115115
->flatten($categories[$item->getId()]);
116116
$categories[$item->getId()]['product_count'] = $item->getProductCount();
117+
$categories[$item->getId()]['model'] = $item;
117118
}
118119
}
119120

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Category;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Catalog\Helper\Output as OutputHelper;
16+
17+
/**
18+
* Resolve rendered content for category attributes where HTML content is allowed
19+
*/
20+
class CategoryHtmlAttribute implements ResolverInterface
21+
{
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+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function resolve(
40+
Field $field,
41+
$context,
42+
ResolveInfo $info,
43+
array $value = null,
44+
array $args = null
45+
) {
46+
if (!isset($value['model'])) {
47+
throw new LocalizedException(__('"model" value should be specified'));
48+
}
49+
50+
/* @var $category Category */
51+
$category = $value['model'];
52+
$fieldName = $field->getName();
53+
$renderedValue = $this->outputHelper->categoryAttribute($category, $category->getData($fieldName), $fieldName);
54+
55+
return $renderedValue;
56+
}
57+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\Product;
1111
use Magento\Catalog\Model\Product\ImageFactory;
12+
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\GraphQl\Config\Element\Field;
1314
use Magento\Framework\GraphQl\Query\ResolverInterface;
1415
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
@@ -45,7 +46,7 @@ public function resolve(
4546
array $args = null
4647
): array {
4748
if (!isset($value['model'])) {
48-
throw new \LogicException(__('"model" value should be specified'));
49+
throw new LocalizedException(__('"model" value should be specified'));
4950
}
5051
/** @var Product $product */
5152
$product = $value['model'];

app/code/Magento/CatalogGraphQl/Model/Resolver/Products.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10+
use Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters;
1011
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1112
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Filter;
1213
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search;
@@ -51,13 +52,15 @@ class Products implements ResolverInterface
5152
* @param Builder $searchCriteriaBuilder
5253
* @param Search $searchQuery
5354
* @param Filter $filterQuery
55+
* @param SearchFilter $searchFilter
56+
* @param Filters $filtersDataProvider
5457
*/
5558
public function __construct(
5659
Builder $searchCriteriaBuilder,
5760
Search $searchQuery,
5861
Filter $filterQuery,
5962
SearchFilter $searchFilter,
60-
\Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider
63+
Filters $filtersDataProvider
6164
) {
6265
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
6366
$this->searchQuery = $searchQuery;
@@ -100,10 +103,10 @@ public function resolve(
100103

101104
$currentPage = $searchCriteria->getCurrentPage();
102105
if ($searchCriteria->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) {
103-
$currentPage = new GraphQlInputException(
106+
throw new GraphQlInputException(
104107
__(
105-
'currentPage value %1 specified is greater than the number of pages available.',
106-
[$maxPages]
108+
'currentPage value %1 specified is greater than the %2 page(s) available.',
109+
[$currentPage, $maxPages]
107110
)
108111
);
109112
}
@@ -113,7 +116,8 @@ public function resolve(
113116
'items' => $searchResult->getProductsSearchResult(),
114117
'page_info' => [
115118
'page_size' => $searchCriteria->getPageSize(),
116-
'current_page' => $currentPage
119+
'current_page' => $currentPage,
120+
'total_pages' => $maxPages
117121
],
118122
'filters' => $this->filtersDataProvider->getData($layerType)
119123
];

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function __construct(
8383
}
8484

8585
/**
86+
* Returns categories tree starting from parent $rootCategoryId
87+
*
8688
* @param ResolveInfo $resolveInfo
8789
* @param int $rootCategoryId
8890
* @return array
@@ -107,6 +109,8 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId) : array
107109
}
108110

109111
/**
112+
* Iterates through category tree
113+
*
110114
* @param \Iterator $iterator
111115
* @return array
112116
*/
@@ -119,6 +123,7 @@ private function processTree(\Iterator $iterator) : array
119123
$iterator->next();
120124
$nextCategory = $iterator->current();
121125
$tree[$category->getId()] = $this->hydrator->hydrateCategory($category);
126+
$tree[$category->getId()]['model'] = $category;
122127
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
123128
$tree[$category->getId()]['children'] = $this->processTree($iterator);
124129
}
@@ -128,6 +133,8 @@ private function processTree(\Iterator $iterator) : array
128133
}
129134

130135
/**
136+
* Joins EAV attributes recursively
137+
*
131138
* @param Collection $collection
132139
* @param FieldNode $fieldNode
133140
* @return void

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\Catalog\Model\Product\Option\Type\Date" type="Magento\CatalogGraphQl\Model\Product\Option\DateType" />
910
<type name="Magento\CatalogGraphQl\Model\ProductInterfaceTypeResolverComposite">
1011
<arguments>
1112
<argument name="productTypeNameResolvers" xsi:type="array">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ interface CustomizableProductInterface @typeResolver(class: "Magento\\CatalogGra
369369

370370
interface CategoryInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\CategoryInterfaceTypeResolver") @doc(description: "CategoryInterface contains the full set of attributes that can be returned in a category search") {
371371
id: Int @doc(description: "An ID that uniquely identifies the category")
372-
description: String @doc(description: "An optional description of the category")
372+
description: String @doc(description: "An optional description of the category") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoryHtmlAttribute")
373373
name: String @doc(description: "The display name of the category")
374374
path: String @doc(description: "Category Path")
375375
path_in_store: String @doc(description: "Category path in store")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ input FilterTypeInput @doc(description: "FilterTypeInput specifies which action
2929
type SearchResultPageInfo @doc(description: "SearchResultPageInfo provides navigation for the query response") {
3030
page_size: Int @doc(description: "Specifies the maximum number of items to return")
3131
current_page: Int @doc(description: "Specifies which page of results to return")
32+
total_pages: Int @doc(description: "Total pages")
3233
}
3334

3435
enum SortEnum @doc(description: "This enumeration indicates whether to return results in ascending or descending order") {

0 commit comments

Comments
 (0)