Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMM-18 improve graphql performance #358

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add missing $escaper and $viewModels declarations ([#350](https://github.com/SnowdogApps/magento2-menu/pull/350))
- Import categories by store view ([#352](https://github.com/SnowdogApps/magento2-menu/pull/352))
- Add external vue providers config option
- Improve graphql performance

## [2.27.2] - 2024-11-08
### Fixed
Expand Down
19 changes: 13 additions & 6 deletions Model/GraphQl/Resolver/DataProvider/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Snowdog\Menu\Model\GraphQl\Resolver\DataProvider;

use Exception;
use Magento\Framework\Exception\LocalizedException;
adamwaclawczyk marked this conversation as resolved.
Show resolved Hide resolved
use Magento\Framework\Exception\NoSuchEntityException;
use Snowdog\Menu\Api\Data\NodeInterface;
Expand Down Expand Up @@ -124,6 +125,7 @@ private function convertData(NodeInterface $node): array
*/
private function loadModels($nodes, $storeId): void
{
$modelsToLoadByType = [];
/** @var NodeInterface $node */
foreach ($nodes as $node) {
$type = $node->getType();
Expand All @@ -133,12 +135,18 @@ private function loadModels($nodes, $storeId): void
if (!in_array($type, TypeModel::TYPES)) {
continue;
}
$modelsToLoadByType[$type][] = $node->getContent();
}

foreach ($modelsToLoadByType as $type => $ids) {
if (!is_array($ids)) {
continue;
}
try {
$model = $this->typeModel->getModel($type, $node->getContent(), $storeId);
} catch (NoSuchEntityException|LocalizedException $e) {
$model = null;
$this->loadedModels[$type] = $this->typeModel->getModels($type, $ids ?? [], $storeId);
} catch (Exception $e) {
continue;
}
$this->loadedModels[$type][$node->getContent()] = $model;
}
}

Expand All @@ -152,8 +160,7 @@ private function getUrlKey(NodeInterface $node): ?string
if (!isset($this->loadedModels[$node->getType()][$node->getContent()])) {
return null;
}
$currentModel = $this->loadedModels[$node->getType()][$node->getContent()];
return $this->typeModel->getModelUrlKey($node->getType(), $currentModel);
return $this->loadedModels[$node->getType()][$node->getContent()];
} elseif ($node->getType() == self::NODE_TYPE_CUSTOM_URL) {
return $node->getContent();
} else {
Expand Down
56 changes: 11 additions & 45 deletions Model/GraphQl/Resolver/DataProvider/Node/TypeModel.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,34 @@
<?php
declare(strict_types=1);

namespace Snowdog\Menu\Model\GraphQl\Resolver\DataProvider\Node;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

class TypeModel
{
const TYPES = ["category", "product", "cms_page"];

/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var CategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var PageRepositoryInterface
* @var \Snowdog\Menu\Model\ResourceModel\NodeType\AbstractNode[]
*/
private $pageRepository;
private $typeModels = [];

public function __construct(
ProductRepositoryInterface $productRepository,
CategoryRepositoryInterface $categoryRepository,
PageRepositoryInterface $pageRepository
array $typeModels = []
) {
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->pageRepository = $pageRepository;
$this->typeModels = $typeModels;
}

/**
* @param $type
* @param $modelId
* @param $storeId
* @return ProductInterface|CategoryInterface|PageInterface|null
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getModel($type, $modelId, $storeId)
public function getModels($type, $modelIds, $storeId)
{
$model = null;
switch ($type) {
case "product":
$model = $this->productRepository->getById($modelId, false, $storeId);
break;
case "category":
$model = $this->categoryRepository->get($modelId, $storeId);
break;
case "cms_page":
$model = $this->pageRepository->getById($modelId);
break;
default:
break;
if (isset($this->typeModels[$type])) {
return $this->typeModels[$type]->fetchData($storeId, $modelIds);
}
return $model;

return [];
}

public function getModelUrlKey($type, $model): ?string
Expand Down
10 changes: 10 additions & 0 deletions etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@
</argument>
</arguments>
</type>

<type name="Snowdog\Menu\Model\GraphQl\Resolver\DataProvider\Node\TypeModel">
<arguments>
<argument name="typeModels" xsi:type="array">
<item name="product" xsi:type="object">Snowdog\Menu\Model\ResourceModel\NodeType\Product</item>
<item name="category" xsi:type="object">Snowdog\Menu\Model\ResourceModel\NodeType\Category</item>
<item name="cms_page" xsi:type="object">Snowdog\Menu\Model\ResourceModel\NodeType\CmsPage</item>
</argument>
</arguments>
</type>
</config>
Loading