Skip to content

Commit 436d8a6

Browse files
committed
MAGETWO-95215: [GraphQL] Performance issue in filters realization
- Optimized category data loading for products query
1 parent 165dccf commit 436d8a6

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

app/code/Magento/CatalogGraphQl/Model/Category/Hydrator.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\CatalogGraphQl\Model\Category;
99

1010
use Magento\Catalog\Api\Data\CategoryInterface;
11+
use Magento\Catalog\Model\Category;
1112
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CustomAttributesFlattener;
1213
use Magento\Framework\Reflection\DataObjectProcessor;
1314

@@ -41,14 +42,19 @@ public function __construct(
4142
/**
4243
* Hydrate and flatten category object to flat array
4344
*
44-
* @param CategoryInterface $category
45+
* @param Category $category
46+
* @param bool $basicFieldsOnly Set to false to avoid expensive hydration, used for performance optimization
4547
* @return array
4648
*/
47-
public function hydrateCategory(CategoryInterface $category) : array
49+
public function hydrateCategory(Category $category, $basicFieldsOnly = false) : array
4850
{
49-
$categoryData = $this->dataObjectProcessor->buildOutputDataArray($category, CategoryInterface::class);
51+
if ($basicFieldsOnly) {
52+
$categoryData = $category->getData();
53+
} else {
54+
$categoryData = $this->dataObjectProcessor->buildOutputDataArray($category, CategoryInterface::class);
55+
$categoryData['product_count'] = $category->getProductCount();
56+
}
5057
$categoryData['id'] = $category->getId();
51-
$categoryData['product_count'] = $category->getProductCount();
5258
$categoryData['children'] = [];
5359
$categoryData['available_sort_by'] = $category->getAvailableSortBy();
5460
return $this->flattener->flatten($categoryData);

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Magento\Framework\GraphQl\Config\Element\Field;
1818
use Magento\Framework\GraphQl\Query\ResolverInterface;
1919
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
20-
use Magento\Framework\Reflection\DataObjectProcessor;
20+
use Magento\CatalogGraphQl\Model\Category\Hydrator as CategoryHydrator;
2121

2222
/**
2323
* Resolver for category objects the product is assigned to.
@@ -36,11 +36,6 @@ class Categories implements ResolverInterface
3636
*/
3737
private $categoryIds = [];
3838

39-
/**
40-
* @var DataObjectProcessor
41-
*/
42-
private $dataObjectProcessor;
43-
4439
/**
4540
* @var AttributesJoiner
4641
*/
@@ -57,25 +52,29 @@ class Categories implements ResolverInterface
5752
private $valueFactory;
5853

5954
/**
60-
* Category constructor.
55+
* @var CategoryHydrator
56+
*/
57+
private $categoryHydrator;
58+
59+
/**
6160
* @param CollectionFactory $collectionFactory
62-
* @param DataObjectProcessor $dataObjectProcessor
6361
* @param AttributesJoiner $attributesJoiner
6462
* @param CustomAttributesFlattener $customAttributesFlattener
6563
* @param ValueFactory $valueFactory
64+
* @param CategoryHydrator $categoryHydrator
6665
*/
6766
public function __construct(
6867
CollectionFactory $collectionFactory,
69-
DataObjectProcessor $dataObjectProcessor,
7068
AttributesJoiner $attributesJoiner,
7169
CustomAttributesFlattener $customAttributesFlattener,
72-
ValueFactory $valueFactory
70+
ValueFactory $valueFactory,
71+
CategoryHydrator $categoryHydrator
7372
) {
7473
$this->collection = $collectionFactory->create();
75-
$this->dataObjectProcessor = $dataObjectProcessor;
7674
$this->attributesJoiner = $attributesJoiner;
7775
$this->customAttributesFlattener = $customAttributesFlattener;
7876
$this->valueFactory = $valueFactory;
77+
$this->categoryHydrator = $categoryHydrator;
7978
}
8079

8180
/**
@@ -109,8 +108,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
109108
if (in_array($item->getId(), $categoryIds)) {
110109

111110
// Try to extract all requested fields from the loaded collection data
112-
$categories[$item->getId()] = $item->getData();
113-
$categories[$item->getId()]['id'] = $item->getId();
111+
$categories[$item->getId()] = $this->categoryHydrator->hydrateCategory($item, true);
114112
$requestedFields = $that->attributesJoiner->getQueryFields($info->fieldNodes[0]);
115113
$extractedFields = array_keys($categories[$item->getId()]);
116114
$foundFields = array_intersect($requestedFields, $extractedFields);
@@ -119,13 +117,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
119117
}
120118

121119
// If not all requested fields were extracted from the collection, start more complex extraction
122-
$categories[$item->getId()] = $this->dataObjectProcessor->buildOutputDataArray(
123-
$item,
124-
CategoryInterface::class
125-
);
126-
$categories[$item->getId()] = $this->customAttributesFlattener
127-
->flatten($categories[$item->getId()]);
128-
$categories[$item->getId()]['product_count'] = $item->getProductCount();
120+
$categories[$item->getId()] = $this->categoryHydrator->hydrateCategory($item);
121+
129122
}
130123
}
131124

0 commit comments

Comments
 (0)