Skip to content

Commit 39d2d18

Browse files
committed
Merge remote-tracking branch 'origin/2.3-develop' into MC-18995
Conflicts: setup/performance-toolkit/benchmark.jmx
2 parents 55e08f5 + fc387f4 commit 39d2d18

File tree

18 files changed

+1989
-211
lines changed

18 files changed

+1989
-211
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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\CatalogGraphQl\Model\Category;
9+
10+
use Magento\Catalog\Api\Data\CategoryInterface;
11+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\Exception\InputException;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\ScopeInterface;
16+
use Magento\Search\Model\Query;
17+
18+
/**
19+
* Category filter allows to filter collection using 'id, url_key, name' from search criteria.
20+
*/
21+
class CategoryFilter
22+
{
23+
/**
24+
* @var ScopeConfigInterface
25+
*/
26+
private $scopeConfig;
27+
28+
/**
29+
* @param ScopeConfigInterface $scopeConfig
30+
*/
31+
public function __construct(
32+
ScopeConfigInterface $scopeConfig
33+
) {
34+
$this->scopeConfig = $scopeConfig;
35+
}
36+
37+
/**
38+
* Filter for filtering the requested categories id's based on url_key, ids, name in the result.
39+
*
40+
* @param array $args
41+
* @param Collection $categoryCollection
42+
* @param StoreInterface $store
43+
* @throws InputException
44+
*/
45+
public function applyFilters(array $args, Collection $categoryCollection, StoreInterface $store)
46+
{
47+
$categoryCollection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1]);
48+
foreach ($args['filters'] as $field => $cond) {
49+
foreach ($cond as $condType => $value) {
50+
if ($field === 'ids') {
51+
$categoryCollection->addIdFilter($value);
52+
} else {
53+
$this->addAttributeFilter($categoryCollection, $field, $condType, $value, $store);
54+
}
55+
}
56+
}
57+
}
58+
59+
/**
60+
* Add filter to category collection
61+
*
62+
* @param Collection $categoryCollection
63+
* @param string $field
64+
* @param string $condType
65+
* @param string|array $value
66+
* @param StoreInterface $store
67+
* @throws InputException
68+
*/
69+
private function addAttributeFilter($categoryCollection, $field, $condType, $value, $store)
70+
{
71+
if ($condType === 'match') {
72+
$this->addMatchFilter($categoryCollection, $field, $value, $store);
73+
return;
74+
}
75+
$categoryCollection->addAttributeToFilter($field, [$condType => $value]);
76+
}
77+
78+
/**
79+
* Add match filter to collection
80+
*
81+
* @param Collection $categoryCollection
82+
* @param string $field
83+
* @param string $value
84+
* @param StoreInterface $store
85+
* @throws InputException
86+
*/
87+
private function addMatchFilter($categoryCollection, $field, $value, $store)
88+
{
89+
$minQueryLength = $this->scopeConfig->getValue(
90+
Query::XML_PATH_MIN_QUERY_LENGTH,
91+
ScopeInterface::SCOPE_STORE,
92+
$store
93+
);
94+
$searchValue = str_replace('%', '', $value);
95+
$matchLength = strlen($searchValue);
96+
if ($matchLength < $minQueryLength) {
97+
throw new InputException(__('Invalid match filter'));
98+
}
99+
100+
$categoryCollection->addAttributeToFilter($field, ['like' => "%{$searchValue}%"]);
101+
}
102+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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\CatalogGraphQl\Model\Resolver;
9+
10+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
11+
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree;
17+
use Magento\CatalogGraphQl\Model\Category\CategoryFilter;
18+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
19+
20+
/**
21+
* Category List resolver, used for GraphQL category data request processing.
22+
*/
23+
class CategoryList implements ResolverInterface
24+
{
25+
/**
26+
* @var CategoryTree
27+
*/
28+
private $categoryTree;
29+
30+
/**
31+
* @var CollectionFactory
32+
*/
33+
private $collectionFactory;
34+
35+
/**
36+
* @var CategoryFilter
37+
*/
38+
private $categoryFilter;
39+
40+
/**
41+
* @var ExtractDataFromCategoryTree
42+
*/
43+
private $extractDataFromCategoryTree;
44+
45+
/**
46+
* @param CategoryTree $categoryTree
47+
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
48+
* @param CategoryFilter $categoryFilter
49+
* @param CollectionFactory $collectionFactory
50+
*/
51+
public function __construct(
52+
CategoryTree $categoryTree,
53+
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
54+
CategoryFilter $categoryFilter,
55+
CollectionFactory $collectionFactory
56+
) {
57+
$this->categoryTree = $categoryTree;
58+
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
59+
$this->categoryFilter = $categoryFilter;
60+
$this->collectionFactory = $collectionFactory;
61+
}
62+
63+
/**
64+
* @inheritdoc
65+
*/
66+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
67+
{
68+
if (isset($value[$field->getName()])) {
69+
return $value[$field->getName()];
70+
}
71+
$store = $context->getExtensionAttributes()->getStore();
72+
73+
$rootCategoryIds = [];
74+
if (!isset($args['filters'])) {
75+
$rootCategoryIds[] = (int)$store->getRootCategoryId();
76+
} else {
77+
$categoryCollection = $this->collectionFactory->create();
78+
try {
79+
$this->categoryFilter->applyFilters($args, $categoryCollection, $store);
80+
} catch (InputException $e) {
81+
return [];
82+
}
83+
84+
foreach ($categoryCollection as $category) {
85+
$rootCategoryIds[] = (int)$category->getId();
86+
}
87+
}
88+
89+
$result = $this->fetchCategories($rootCategoryIds, $info);
90+
return $result;
91+
}
92+
93+
/**
94+
* Fetch category tree data
95+
*
96+
* @param array $categoryIds
97+
* @param ResolveInfo $info
98+
* @return array
99+
* @throws GraphQlNoSuchEntityException
100+
*/
101+
private function fetchCategories(array $categoryIds, ResolveInfo $info)
102+
{
103+
$fetchedCategories = [];
104+
foreach ($categoryIds as $categoryId) {
105+
$categoryTree = $this->categoryTree->getTree($info, $categoryId);
106+
if (empty($categoryTree)) {
107+
continue;
108+
}
109+
$fetchedCategories[] = current($this->extractDataFromCategoryTree->execute($categoryTree));
110+
}
111+
112+
return $fetchedCategories;
113+
}
114+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/Query/FieldSelection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ private function getProductFields(ResolveInfo $info): array
6060
$fieldNames[] = $this->collectProductFieldNames($selection, $fieldNames);
6161
}
6262
}
63-
64-
$fieldNames = array_merge(...$fieldNames);
65-
63+
if (!empty($fieldNames)) {
64+
$fieldNames = array_merge(...$fieldNames);
65+
}
6666
return $fieldNames;
6767
}
6868

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ type Query {
1111
): Products
1212
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Products") @doc(description: "The products query searches for products that match the criteria specified in the search and filter attributes.") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\Identity")
1313
category (
14-
id: Int @doc(description: "Id of the category.")
14+
id: Int @doc(description: "Id of the category.")
1515
): CategoryTree
16-
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree") @doc(description: "The category query searches for categories that match the criteria specified in the search and filter attributes.") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoryTreeIdentity")
16+
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree") @doc(description: "The category query searches for categories that match the criteria specified in the search and filter attributes.") @deprecated(reason: "Use 'categoryList' query instead of 'category' query") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoryTreeIdentity")
17+
categoryList(
18+
filters: CategoryFilterInput @doc(description: "Identifies which Category filter inputs to search for and return.")
19+
): [CategoryTree] @doc(description: "Returns an array of categories based on the specified filters.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryList") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoriesIdentity")
1720
}
1821

1922
type Price @doc(description: "Price is deprecated, replaced by ProductPrice. The Price object defines the price of a product as well as any tax-related adjustments.") {
@@ -294,6 +297,13 @@ input ProductAttributeFilterInput @doc(description: "ProductAttributeFilterInput
294297
category_id: FilterEqualTypeInput @doc(description: "Filter product by category id")
295298
}
296299

300+
input CategoryFilterInput @doc(description: "CategoryFilterInput defines the filters to be used in the search. A filter contains at least one attribute, a comparison operator, and the value that is being searched for.")
301+
{
302+
ids: FilterEqualTypeInput @doc(description: "Filter by category ID that uniquely identifies the category.")
303+
url_key: FilterEqualTypeInput @doc(description: "Filter by the part of the URL that identifies the category")
304+
name: FilterMatchTypeInput @doc(description: "Filter by the display name of the category.")
305+
}
306+
297307
input ProductFilterInput @doc(description: "ProductFilterInput is deprecated, use @ProductAttributeFilterInput instead. ProductFilterInput defines the filters to be used in the search. A filter contains at least one attribute, a comparison operator, and the value that is being searched for.") {
298308
name: FilterTypeInput @doc(description: "The product name. Customers use this name to identify the product.")
299309
sku: FilterTypeInput @doc(description: "A number or code assigned to a product to identify the product, options, price, and manufacturer.")

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

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class EntityUrl implements ResolverInterface
3131
*/
3232
private $customUrlLocator;
3333

34+
/**
35+
* @var int
36+
*/
37+
private $redirectType;
38+
3439
/**
3540
* @param UrlFinderInterface $urlFinder
3641
* @param CustomUrlLocatorInterface $customUrlLocator
@@ -57,49 +62,83 @@ public function resolve(
5762
throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
5863
}
5964

65+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
6066
$result = null;
6167
$url = $args['url'];
6268
if (substr($url, 0, 1) === '/' && $url !== '/') {
6369
$url = ltrim($url, '/');
6470
}
71+
$this->redirectType = 0;
6572
$customUrl = $this->customUrlLocator->locateUrl($url);
6673
$url = $customUrl ?: $url;
67-
$urlRewrite = $this->findCanonicalUrl($url, (int)$context->getExtensionAttributes()->getStore()->getId());
68-
if ($urlRewrite) {
69-
if (!$urlRewrite->getEntityId()) {
74+
$finalUrlRewrite = $this->findFinalUrl($url, $storeId);
75+
if ($finalUrlRewrite) {
76+
$relativeUrl = $finalUrlRewrite->getRequestPath();
77+
$resultArray = $this->rewriteCustomUrls($finalUrlRewrite, $storeId) ?? [
78+
'id' => $finalUrlRewrite->getEntityId(),
79+
'canonical_url' => $relativeUrl,
80+
'relative_url' => $relativeUrl,
81+
'redirectCode' => $this->redirectType,
82+
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
83+
];
84+
85+
if (empty($resultArray['id'])) {
7086
throw new GraphQlNoSuchEntityException(
7187
__('No such entity found with matching URL key: %url', ['url' => $url])
7288
);
7389
}
74-
$result = [
75-
'id' => $urlRewrite->getEntityId(),
76-
'canonical_url' => $urlRewrite->getTargetPath(),
77-
'relative_url' => $urlRewrite->getTargetPath(),
78-
'type' => $this->sanitizeType($urlRewrite->getEntityType())
79-
];
90+
91+
$result = $resultArray;
8092
}
8193
return $result;
8294
}
8395

8496
/**
85-
* Find the canonical url passing through all redirects if any
97+
* Handle custom urls with and without redirects
98+
*
99+
* @param UrlRewrite $finalUrlRewrite
100+
* @param int $storeId
101+
* @return array|null
102+
*/
103+
private function rewriteCustomUrls(UrlRewrite $finalUrlRewrite, int $storeId): ?array
104+
{
105+
if ($finalUrlRewrite->getEntityType() === 'custom' || !($finalUrlRewrite->getEntityId() > 0)) {
106+
$finalCustomUrlRewrite = clone $finalUrlRewrite;
107+
$finalUrlRewrite = $this->findFinalUrl($finalCustomUrlRewrite->getTargetPath(), $storeId, true);
108+
$relativeUrl =
109+
$finalCustomUrlRewrite->getRedirectType() == 0
110+
? $finalCustomUrlRewrite->getRequestPath() : $finalUrlRewrite->getRequestPath();
111+
return [
112+
'id' => $finalUrlRewrite->getEntityId(),
113+
'canonical_url' => $relativeUrl,
114+
'relative_url' => $relativeUrl,
115+
'redirectCode' => $finalCustomUrlRewrite->getRedirectType(),
116+
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
117+
];
118+
}
119+
return null;
120+
}
121+
122+
/**
123+
* Find the final url passing through all redirects if any
86124
*
87125
* @param string $requestPath
88126
* @param int $storeId
127+
* @param bool $findCustom
89128
* @return UrlRewrite|null
90129
*/
91-
private function findCanonicalUrl(string $requestPath, int $storeId) : ?UrlRewrite
130+
private function findFinalUrl(string $requestPath, int $storeId, bool $findCustom = false): ?UrlRewrite
92131
{
93132
$urlRewrite = $this->findUrlFromRequestPath($requestPath, $storeId);
94-
if ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
133+
if ($urlRewrite) {
134+
$this->redirectType = $urlRewrite->getRedirectType();
95135
while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
96136
$urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath(), $storeId);
97137
}
98-
}
99-
if (!$urlRewrite) {
138+
} else {
100139
$urlRewrite = $this->findUrlFromTargetPath($requestPath, $storeId);
101140
}
102-
if ($urlRewrite && !$urlRewrite->getEntityId() && !$urlRewrite->getIsAutogenerated()) {
141+
if ($urlRewrite && ($findCustom && !$urlRewrite->getEntityId() && !$urlRewrite->getIsAutogenerated())) {
103142
$urlRewrite = $this->findUrlFromTargetPath($urlRewrite->getTargetPath(), $storeId);
104143
}
105144

@@ -113,7 +152,7 @@ private function findCanonicalUrl(string $requestPath, int $storeId) : ?UrlRewri
113152
* @param int $storeId
114153
* @return UrlRewrite|null
115154
*/
116-
private function findUrlFromRequestPath(string $requestPath, int $storeId) : ?UrlRewrite
155+
private function findUrlFromRequestPath(string $requestPath, int $storeId): ?UrlRewrite
117156
{
118157
return $this->urlFinder->findOneByData(
119158
[
@@ -130,7 +169,7 @@ private function findUrlFromRequestPath(string $requestPath, int $storeId) : ?Ur
130169
* @param int $storeId
131170
* @return UrlRewrite|null
132171
*/
133-
private function findUrlFromTargetPath(string $targetPath, int $storeId) : ?UrlRewrite
172+
private function findUrlFromTargetPath(string $targetPath, int $storeId): ?UrlRewrite
134173
{
135174
return $this->urlFinder->findOneByData(
136175
[

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type EntityUrl @doc(description: "EntityUrl is an output object containing the `
99
id: Int @doc(description: "The ID assigned to the object associated with the specified url. This could be a product ID, category ID, or page ID.")
1010
canonical_url: String @deprecated(reason: "The canonical_url field is deprecated, use relative_url instead.")
1111
relative_url: String @doc(description: "The internal relative URL. If the specified url is a redirect, the query returns the redirected URL, not the original.")
12+
redirectCode: Int @doc(description: "301 or 302 HTTP code for url permanent or temporary redirect or 0 for the 200 no redirect")
1213
type: UrlRewriteEntityTypeEnum @doc(description: "One of PRODUCT, CATEGORY, or CMS_PAGE.")
1314
}
1415

0 commit comments

Comments
 (0)