Skip to content

Commit c090d38

Browse files
committed
MC-18996: GraphQl Url Resolver doesn't return any results if the url_key doesn't have the extension
- add url prefix to schema
1 parent 3fe8a17 commit c090d38

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\CatalogUrlRewriteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
17+
/**
18+
* Returns the url suffix for catalog
19+
*/
20+
class CategoryUrlSuffix implements ResolverInterface
21+
{
22+
const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/category_url_suffix';
23+
24+
/**
25+
* Cache for product rewrite suffix
26+
*
27+
* @var array
28+
*/
29+
private $productUrlSuffix = [];
30+
31+
/**
32+
* @var ScopeConfigInterface
33+
*/
34+
private $scopeConfig;
35+
36+
/**
37+
* @param ScopeConfigInterface $scopeConfig
38+
* @param array $productUrlSuffix
39+
*/
40+
public function __construct(ScopeConfigInterface $scopeConfig, array $productUrlSuffix = [])
41+
{
42+
$this->productUrlSuffix = $productUrlSuffix;
43+
$this->scopeConfig = $scopeConfig;
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function resolve(
50+
Field $field,
51+
$context,
52+
ResolveInfo $info,
53+
array $value = null,
54+
array $args = null
55+
): string {
56+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
57+
return $this->getProductUrlSuffix($storeId);
58+
}
59+
60+
/**
61+
* Retrieve product url suffix by store
62+
*
63+
* @param int $storeId
64+
* @return string
65+
*/
66+
private function getProductUrlSuffix(int $storeId): string
67+
{
68+
if (!isset($this->productUrlSuffix[$storeId])) {
69+
$this->productUrlSuffix[$storeId] = $this->scopeConfig->getValue(
70+
self::XML_PATH_PRODUCT_URL_SUFFIX,
71+
ScopeInterface::SCOPE_STORE,
72+
$storeId
73+
);
74+
}
75+
return $this->productUrlSuffix[$storeId];
76+
}
77+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\CatalogUrlRewriteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
17+
/**
18+
* Returns the url suffix for product
19+
*/
20+
class ProductUrlSuffix implements ResolverInterface
21+
{
22+
const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix';
23+
24+
/**
25+
* Cache for product rewrite suffix
26+
*
27+
* @var array
28+
*/
29+
private $productUrlSuffix = [];
30+
31+
/**
32+
* @var ScopeConfigInterface
33+
*/
34+
private $scopeConfig;
35+
36+
/**
37+
* @param ScopeConfigInterface $scopeConfig
38+
* @param array $productUrlSuffix
39+
*/
40+
public function __construct(ScopeConfigInterface $scopeConfig, array $productUrlSuffix = [])
41+
{
42+
$this->productUrlSuffix = $productUrlSuffix;
43+
$this->scopeConfig = $scopeConfig;
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function resolve(
50+
Field $field,
51+
$context,
52+
ResolveInfo $info,
53+
array $value = null,
54+
array $args = null
55+
): string {
56+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
57+
return $this->getProductUrlSuffix($storeId);
58+
}
59+
60+
/**
61+
* Retrieve product url suffix by store
62+
*
63+
* @param int $storeId
64+
* @return string
65+
*/
66+
private function getProductUrlSuffix(int $storeId): string
67+
{
68+
if (!isset($this->productUrlSuffix[$storeId])) {
69+
$this->productUrlSuffix[$storeId] = $this->scopeConfig->getValue(
70+
self::XML_PATH_PRODUCT_URL_SUFFIX,
71+
ScopeInterface::SCOPE_STORE,
72+
$storeId
73+
);
74+
}
75+
return $this->productUrlSuffix[$storeId];
76+
}
77+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33

44
interface ProductInterface {
55
url_key: String @doc(description: "The part of the URL that identifies the product")
6+
url_suffix: String @doc(description: "The part of the URL that is appended after the url key") @resolver(class: "Magento\\CatalogUrlRewriteGraphQl\\Model\\Resolver\\ProductUrlSuffix")
67
url_path: String @deprecated(reason: "Use product's `canonical_url` or url rewrites instead")
78
url_rewrites: [UrlRewrite] @doc(description: "URL rewrites list") @resolver(class: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\UrlRewrite")
89
}
910

11+
interface CategoryInterface {
12+
url_suffix: String @doc(description: "The part of the URL that is appended after the url key") @resolver(class: "Magento\\CatalogUrlRewriteGraphQl\\Model\\Resolver\\CategoryUrlSuffix")
13+
}
14+
1015
input ProductFilterInput {
1116
url_key: FilterTypeInput @doc(description: "The part of the URL that identifies the product")
1217
url_path: FilterTypeInput @deprecated(reason: "Use product's `canonical_url` or url rewrites instead")

0 commit comments

Comments
 (0)