Skip to content

fix(urlrewrite,graphql): routes now handles disabled entities #36968

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ public function getData(
int $storeId = null
): array {
$categoryId = (int)$id;
$categoriesTree = $this->categoryTree->getTreeCollection($info, $categoryId, $storeId);
if ($categoriesTree->count() == 0) {
$categoriesTree = $this->categoryTree->getTree($info, $categoryId, $storeId);
if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
}
$result = current($this->extractDataFromCategoryTree->buildTree($categoriesTree, [$categoryId]));
$result = current($this->extractDataFromCategoryTree->execute($categoriesTree));

if ($result['model']->getIsActive() != '1') {
throw new GraphQlNoSuchEntityException(__('Category is disabled.'));
}

$result['type_id'] = $entity_type;
return $result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\CatalogUrlRewriteGraphQl\Model\DataProvider\UrlRewrite;
Expand All @@ -11,6 +13,8 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\UrlRewriteGraphQl\Model\DataProvider\EntityDataProviderInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;

class ProductDataProvider implements EntityDataProviderInterface
{
Expand Down Expand Up @@ -47,7 +51,18 @@ public function getData(
): array {
$product = $this->productRepository->getById($id, false, $storeId);
$result = $product->getData();

if (
$product->getStatus() == Status::STATUS_DISABLED
|| (
$product->getVisibility() != Visibility::VISIBILITY_IN_CATALOG &&
$product->getVisibility() != Visibility::VISIBILITY_BOTH
)
) {
throw new NoSuchEntityException(__("Routed Product is disabled."));
}

$result['model'] = $product;
return $result;
}
}
}
14 changes: 12 additions & 2 deletions app/code/Magento/UrlRewriteGraphQl/Model/Resolver/Route.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\UrlRewriteGraphQl\Model\Resolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Query\Uid;
use Magento\UrlRewrite\Model\UrlFinderInterface;
Expand Down Expand Up @@ -58,15 +62,21 @@ public function resolve(
);
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
if ($resultArray) {
$result = [];
if (isset($resultArray['type'])) {
try {
$result = $this->entityDataProviderComposite->getData(
$resultArray['type'],
(int)$resultArray['id'],
$info,
$storeId
);
} catch (NoSuchEntityException $th) {
// Disabled Entities are expected to throw a NoSuchEntityException.
return null;
} catch (GraphQlNoSuchEntityException $th) {
// Disabled Entities are expected to throw a NoSuchEntityException.
return null;
}

$result['redirect_code'] = $resultArray['redirect_code'];
$result['relative_url'] = $resultArray['relative_url'];
$result['type'] = $resultArray['type'];
Expand Down