Skip to content

Commit 7e90ca5

Browse files
authored
Merge pull request #7970 from magento-arcticfoxes/B2B-2463
B2B-2463: Improve custom attributes metadata fetching for category models in graphql
2 parents f3e1661 + 87beb1d commit 7e90ca5

File tree

6 files changed

+141
-76
lines changed

6 files changed

+141
-76
lines changed

app/code/Magento/Catalog/Model/Category/AttributeRepository.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class AttributeRepository implements CategoryAttributeRepositoryInterface
2929
*/
3030
private $eavConfig;
3131

32+
/**
33+
* @var array
34+
*/
35+
private $metadataCache;
36+
3237
/**
3338
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
3439
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
@@ -48,7 +53,7 @@ public function __construct(
4853
}
4954

5055
/**
51-
* {@inheritdoc}
56+
* @inheritdoc
5257
*/
5358
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
5459
{
@@ -59,7 +64,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
5964
}
6065

6166
/**
62-
* {@inheritdoc}
67+
* @inheritdoc
6368
*/
6469
public function get($attributeCode)
6570
{
@@ -70,23 +75,27 @@ public function get($attributeCode)
7075
}
7176

7277
/**
73-
* {@inheritdoc}
78+
* @inheritdoc
79+
*
7480
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7581
*/
7682
public function getCustomAttributesMetadata($dataObjectClassName = null)
7783
{
78-
$defaultAttributeSetId = $this->eavConfig
79-
->getEntityType(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE)
80-
->getDefaultAttributeSetId();
81-
$searchCriteria = $this->searchCriteriaBuilder->addFilters(
82-
[
83-
$this->filterBuilder
84-
->setField('attribute_set_id')
85-
->setValue($defaultAttributeSetId)
86-
->create(),
87-
]
88-
);
89-
90-
return $this->getList($searchCriteria->create())->getItems();
84+
if (!isset($this->metadataCache[$dataObjectClassName])) {
85+
$defaultAttributeSetId = $this->eavConfig
86+
->getEntityType(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE)
87+
->getDefaultAttributeSetId();
88+
$searchCriteria = $this->searchCriteriaBuilder->addFilters(
89+
[
90+
$this->filterBuilder
91+
->setField('attribute_set_id')
92+
->setValue($defaultAttributeSetId)
93+
->create(),
94+
]
95+
);
96+
$this->metadataCache[$dataObjectClassName] = $this->getList($searchCriteria->create())
97+
->getItems();
98+
}
99+
return $this->metadataCache[$dataObjectClassName];
91100
}
92101
}

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

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
namespace Magento\CatalogGraphQl\Model\Category;
99

10-
use Magento\Catalog\Api\CategoryRepositoryInterface;
11-
use Magento\Catalog\Api\Data\CategorySearchResultsInterface;
12-
use Magento\Catalog\Api\Data\CategorySearchResultsInterfaceFactory;
1310
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1411
use Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category\CollectionProcessorInterface;
1512
use Magento\CatalogGraphQl\Model\Category\Filter\SearchCriteria;
1613
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
14+
use Magento\Framework\DB\Select;
1715
use Magento\Framework\Exception\InputException;
1816
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1917
use Magento\GraphQl\Model\Query\ContextInterface;
@@ -39,16 +37,6 @@ class CategoryFilter
3937
*/
4038
private $extensionAttributesJoinProcessor;
4139

42-
/**
43-
* @var CategorySearchResultsInterfaceFactory
44-
*/
45-
private $categorySearchResultsFactory;
46-
47-
/**
48-
* @var CategoryRepositoryInterface
49-
*/
50-
private $categoryRepository;
51-
5240
/**
5341
* @var SearchCriteria
5442
*/
@@ -58,23 +46,17 @@ class CategoryFilter
5846
* @param CollectionFactory $categoryCollectionFactory
5947
* @param CollectionProcessorInterface $collectionProcessor
6048
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
61-
* @param CategorySearchResultsInterfaceFactory $categorySearchResultsFactory
62-
* @param CategoryRepositoryInterface $categoryRepository
6349
* @param SearchCriteria $searchCriteria
6450
*/
6551
public function __construct(
6652
CollectionFactory $categoryCollectionFactory,
6753
CollectionProcessorInterface $collectionProcessor,
6854
JoinProcessorInterface $extensionAttributesJoinProcessor,
69-
CategorySearchResultsInterfaceFactory $categorySearchResultsFactory,
70-
CategoryRepositoryInterface $categoryRepository,
7155
SearchCriteria $searchCriteria
7256
) {
7357
$this->categoryCollectionFactory = $categoryCollectionFactory;
7458
$this->collectionProcessor = $collectionProcessor;
7559
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
76-
$this->categorySearchResultsFactory = $categorySearchResultsFactory;
77-
$this->categoryRepository = $categoryRepository;
7860
$this->searchCriteria = $searchCriteria;
7961
}
8062

@@ -95,22 +77,21 @@ public function getResult(array $criteria, StoreInterface $store, array $attribu
9577
$this->extensionAttributesJoinProcessor->process($collection);
9678
$this->collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context);
9779

98-
/** @var CategorySearchResultsInterface $searchResult */
99-
$categories = $this->categorySearchResultsFactory->create();
100-
$categories->setSearchCriteria($searchCriteria);
101-
$categories->setItems($collection->getItems());
102-
$categories->setTotalCount($collection->getSize());
80+
// only fetch necessary category entity id
81+
$collection
82+
->getSelect()
83+
->reset(Select::COLUMNS)
84+
->columns(
85+
'e.entity_id'
86+
);
10387

104-
$categoryIds = [];
105-
foreach ($categories->getItems() as $category) {
106-
$categoryIds[] = (int)$category->getId();
107-
}
88+
$categoryIds = $collection->load()->getLoadedIds();
10889

10990
$totalPages = 0;
110-
if ($categories->getTotalCount() > 0 && $searchCriteria->getPageSize() > 0) {
111-
$totalPages = ceil($categories->getTotalCount() / $searchCriteria->getPageSize());
91+
if ($collection->getSize() > 0 && $searchCriteria->getPageSize() > 0) {
92+
$totalPages = ceil($collection->getSize() / $searchCriteria->getPageSize());
11293
}
113-
if ($searchCriteria->getCurrentPage() > $totalPages && $categories->getTotalCount() > 0) {
94+
if ($searchCriteria->getCurrentPage() > $totalPages && $collection->getSize() > 0) {
11495
throw new GraphQlInputException(
11596
__(
11697
'currentPage value %1 specified is greater than the %2 page(s) available.',
@@ -121,7 +102,7 @@ public function getResult(array $criteria, StoreInterface $store, array $attribu
121102

122103
return [
123104
'category_ids' => $categoryIds,
124-
'total_count' => $categories->getTotalCount(),
105+
'total_count' => $collection->getSize(),
125106
'page_info' => [
126107
'total_pages' => $totalPages,
127108
'page_size' => $searchCriteria->getPageSize(),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ public function hydrateCategory(Category $category, $basicFieldsOnly = false) :
6060
if ($basicFieldsOnly) {
6161
$categoryData = $category->getData();
6262
} else {
63-
$categoryData = $this->dataObjectProcessor->buildOutputDataArray($category, CategoryInterface::class);
63+
$categoryData = $this->dataObjectProcessor->buildOutputDataArray(
64+
$category,
65+
CategoryInterface::class
66+
);
6467
}
68+
6569
$categoryData['id'] = $category->getId();
6670
$categoryData['uid'] = $this->uidEncoder->encode((string) $category->getId());
6771
$categoryData['children'] = [];

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@
191191
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
192192
<plugin name="availableProductsFilter" type="Magento\CatalogGraphQl\Plugin\AvailableProductsFilter" />
193193
</type>
194+
<type name="Magento\CatalogGraphQl\Model\Category\Hydrator">
195+
<arguments>
196+
<argument name="dataObjectProcessor" xsi:type="object">Magento\CatalogGraphQl\Category\DataObjectProcessor</argument>
197+
</arguments>
198+
</type>
194199
<virtualType name="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ChildProduct"
195200
type="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product">
196201
<arguments>
@@ -207,4 +212,16 @@
207212
</argument>
208213
</arguments>
209214
</virtualType>
215+
<virtualType
216+
name="Magento\CatalogGraphQl\Category\DataObjectProcessor"
217+
type="Magento\Framework\Reflection\DataObjectProcessor"
218+
>
219+
<arguments>
220+
<argument name="excludedMethodsClassMap" xsi:type="array">
221+
<item name="Magento\Catalog\Api\Data\CategoryInterface" xsi:type="array">
222+
<item name="getChildren" xsi:type="string">getChildren</item>
223+
</item>
224+
</argument>
225+
</arguments>
226+
</virtualType>
210227
</config>

lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,36 @@ class DataObjectProcessor
4646
*/
4747
private $processors;
4848

49+
/**
50+
* @var array[]
51+
*/
52+
private $excludedMethodsClassMap;
53+
4954
/**
5055
* @param MethodsMap $methodsMapProcessor
5156
* @param TypeCaster $typeCaster
5257
* @param FieldNamer $fieldNamer
5358
* @param CustomAttributesProcessor $customAttributesProcessor
5459
* @param ExtensionAttributesProcessor $extensionAttributesProcessor
5560
* @param array $processors
61+
* @param array $excludedMethodsClassMap
5662
*/
5763
public function __construct(
5864
MethodsMap $methodsMapProcessor,
5965
TypeCaster $typeCaster,
6066
FieldNamer $fieldNamer,
6167
CustomAttributesProcessor $customAttributesProcessor,
6268
ExtensionAttributesProcessor $extensionAttributesProcessor,
63-
array $processors = []
69+
array $processors = [],
70+
array $excludedMethodsClassMap = []
6471
) {
6572
$this->methodsMapProcessor = $methodsMapProcessor;
6673
$this->typeCaster = $typeCaster;
6774
$this->fieldNamer = $fieldNamer;
6875
$this->extensionAttributesProcessor = $extensionAttributesProcessor;
6976
$this->customAttributesProcessor = $customAttributesProcessor;
7077
$this->processors = $processors;
78+
$this->excludedMethodsClassMap = $excludedMethodsClassMap;
7179
}
7280

7381
/**
@@ -84,7 +92,13 @@ public function buildOutputDataArray($dataObject, $dataObjectType)
8492
$methods = $this->methodsMapProcessor->getMethodsMap($dataObjectType);
8593
$outputData = [];
8694

95+
$excludedMethodsForDataObjectType = $this->excludedMethodsClassMap[$dataObjectType] ?? [];
96+
8797
foreach (array_keys($methods) as $methodName) {
98+
if (in_array($methodName, $excludedMethodsForDataObjectType)) {
99+
continue;
100+
}
101+
88102
if (!$this->methodsMapProcessor->isMethodValidForDataField($dataObjectType, $methodName)) {
89103
continue;
90104
}

0 commit comments

Comments
 (0)