Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit b7a04f0

Browse files
authored
Merge pull request #13 from zbigniewkuras/issue-9
GraphQL - Fix urlResolver query to support relative path
2 parents 9506e9b + e39d356 commit b7a04f0

File tree

3 files changed

+93
-29
lines changed

3 files changed

+93
-29
lines changed

app/code/Magento/UrlRewriteGraphQl/Model/Resolver/UrlRewrite.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Magento\Framework\GraphQl\Query\ResolverInterface;
1515
use Magento\Store\Model\StoreManagerInterface;
1616
use Magento\UrlRewrite\Model\UrlFinderInterface;
17+
use Magento\Store\Model\ScopeInterface;
18+
use Magento\Cms\Helper\Page;
1719

1820
/**
1921
* UrlRewrite field resolver, used for GraphQL request processing.
@@ -34,20 +36,28 @@ class UrlRewrite implements ResolverInterface
3436
* @var ValueFactory
3537
*/
3638
private $valueFactory;
37-
39+
40+
/**
41+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
42+
*/
43+
private $scopeConfig;
44+
3845
/**
3946
* @param UrlFinderInterface $urlFinder
4047
* @param StoreManagerInterface $storeManager
4148
* @param ValueFactory $valueFactory
49+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
4250
*/
4351
public function __construct(
4452
UrlFinderInterface $urlFinder,
4553
StoreManagerInterface $storeManager,
46-
ValueFactory $valueFactory
54+
ValueFactory $valueFactory,
55+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
4756
) {
4857
$this->urlFinder = $urlFinder;
4958
$this->storeManager = $storeManager;
5059
$this->valueFactory = $valueFactory;
60+
$this->scopeConfig = $scopeConfig;
5161
}
5262

5363
/**
@@ -63,8 +73,19 @@ public function resolve(
6373
$result = function () {
6474
return null;
6575
};
76+
6677
if (isset($args['url'])) {
67-
$urlRewrite = $this->findCanonicalUrl($args['url']);
78+
$url = $args['url'];
79+
if (substr($url, 0, 1) === '/' && $url !== '/') {
80+
$url = ltrim($url, '/');
81+
} else if ($url === '/') {
82+
$homePageIdentifier = $this->scopeConfig->getValue(
83+
Page::XML_PATH_HOME_PAGE,
84+
ScopeInterface::SCOPE_STORE
85+
);
86+
$url = $homePageIdentifier;
87+
}
88+
$urlRewrite = $this->findCanonicalUrl($url);
6889
if ($urlRewrite) {
6990
$urlRewriteReturnArray = [
7091
'id' => $urlRewrite->getEntityId(),
@@ -96,6 +117,7 @@ private function findCanonicalUrl(string $requestPath) : ?\Magento\UrlRewrite\Se
96117
if (!$urlRewrite) {
97118
$urlRewrite = $this->findUrlFromTargetPath($requestPath);
98119
}
120+
99121
return $urlRewrite;
100122
}
101123

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
{
2-
"name": "magento/module-url-rewrite-graph-ql",
3-
"description": "N/A",
4-
"type": "magento2-module",
5-
"require": {
6-
"php": "~7.1.3||~7.2.0",
7-
"magento/framework": "*",
8-
"magento/module-url-rewrite": "*",
9-
"magento/module-store": "*"
10-
11-
},
12-
"suggest": {
13-
"magento/module-graph-ql": "*"
14-
},
15-
"license": [
16-
"OSL-3.0",
17-
"AFL-3.0"
18-
],
19-
"autoload": {
20-
"files": [
21-
"registration.php"
22-
],
23-
"psr-4": {
24-
"Magento\\UrlRewriteGraphQl\\": ""
25-
}
26-
}
27-
}
2+
"name" : "magento/module-url-rewrite-graph-ql",
3+
"description" : "N/A",
4+
"type" : "magento2-module",
5+
"require" : {
6+
"php" : "~7.1.3||~7.2.0",
7+
"magento/framework" : "*",
8+
"magento/module-url-rewrite" : "*",
9+
"magento/module-store" : "*",
10+
"magento/cms" : "*"
11+
},
12+
"suggest" : {
13+
"magento/module-graph-ql" : "*"
14+
},
15+
"license" : [
16+
"OSL-3.0",
17+
"AFL-3.0"
18+
],
19+
"autoload" : {
20+
"files" : [
21+
"registration.php"
22+
],
23+
"psr-4" : {
24+
"Magento\\UrlRewriteGraphQl\\" : ""
25+
}
26+
}
27+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/UrlRewrite/UrlResolverTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,46 @@ public function testInvalidUrlResolverInput()
272272
$this->assertArrayHasKey('urlResolver', $response);
273273
$this->assertNull($response['urlResolver']);
274274
}
275+
276+
/**
277+
* Test for category entity with leading slash
278+
*/
279+
public function testCategoryUrlWithLeadingSlash()
280+
{
281+
$productSku = 'p002';
282+
$urlPath2 = 'cat-1.html';
283+
/** @var ProductRepositoryInterface $productRepository */
284+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
285+
$product = $productRepository->get($productSku, false, null, true);
286+
$storeId = $product->getStoreId();
287+
288+
/** @var UrlFinderInterface $urlFinder */
289+
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
290+
$actualUrls = $urlFinder->findOneByData(
291+
[
292+
'request_path' => $urlPath2,
293+
'store_id' => $storeId
294+
]
295+
);
296+
$categoryId = $actualUrls->getEntityId();
297+
$targetPath = $actualUrls->getTargetPath();
298+
$expectedType = $actualUrls->getEntityType();
299+
300+
$query
301+
= <<<QUERY
302+
{
303+
urlResolver(url:"/{$urlPath2}")
304+
{
305+
id
306+
canonical_url
307+
type
308+
}
309+
}
310+
QUERY;
311+
$response = $this->graphQlQuery($query);
312+
$this->assertArrayHasKey('urlResolver', $response);
313+
$this->assertEquals($categoryId, $response['urlResolver']['id']);
314+
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
315+
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
316+
}
275317
}

0 commit comments

Comments
 (0)