Skip to content

Commit 35a480e

Browse files
Merge remote-tracking branch '33090/32460_GraphQL_catalog_url_path' into comm_voted_v1
2 parents 9ffec9b + 675294a commit 35a480e

File tree

4 files changed

+269
-38
lines changed

4 files changed

+269
-38
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Products\Query;
9+
10+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
11+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
14+
15+
/**
16+
* Category Path processor class for category url path argument
17+
*/
18+
class CategoryUrlPathArgsProcessor implements ArgumentsProcessorInterface
19+
{
20+
private const ID = 'category_id';
21+
22+
private const UID = 'category_uid';
23+
24+
private const URL_PATH = 'category_url_path';
25+
26+
/**
27+
* @var CollectionFactory
28+
*/
29+
private $collectionFactory;
30+
31+
/**
32+
* @param CollectionFactory $collectionFactory
33+
*/
34+
public function __construct(CollectionFactory $collectionFactory)
35+
{
36+
$this->collectionFactory = $collectionFactory;
37+
}
38+
39+
/**
40+
* Composite processor that loops through available processors for arguments that come from graphql input
41+
*
42+
* @param string $fieldName
43+
* @param array $args
44+
* @return array
45+
* @throws GraphQlInputException
46+
*/
47+
public function process(
48+
string $fieldName,
49+
array $args
50+
): array {
51+
$idFilter = $args['filter'][self::ID] ?? [];
52+
$uidFilter = $args['filter'][self::UID] ?? [];
53+
$pathFilter = $args['filter'][self::URL_PATH] ?? [];
54+
55+
if (!empty($pathFilter) && $fieldName === 'products') {
56+
if (!empty($idFilter)) {
57+
throw new GraphQlInputException(
58+
__('`%1` and `%2` can\'t be used at the same time.', [self::ID, self::URL_PATH])
59+
);
60+
} elseif (!empty($uidFilter)) {
61+
throw new GraphQlInputException(
62+
__('`%1` and `%2` can\'t be used at the same time.', [self::UID, self::URL_PATH])
63+
);
64+
}
65+
66+
/** @var Collection $collection */
67+
$collection = $this->collectionFactory->create();
68+
$collection->addAttributeToSelect('entity_id');
69+
$collection->addAttributeToFilter('url_path', $pathFilter);
70+
71+
if ($collection->count() === 0) {
72+
throw new GraphQlInputException(
73+
__('No category with the provided `%1` was found', [self::URL_PATH])
74+
);
75+
} elseif ($collection->count() === 1) {
76+
$category = $collection->getFirstItem();
77+
$args['filter'][self::ID]['eq'] = $category->getId();
78+
} else {
79+
$categoryIds = [];
80+
foreach ($collection as $category) {
81+
$categoryIds[] = $category->getId();
82+
}
83+
$args['filter'][self::ID]['in'] = $categoryIds;
84+
}
85+
86+
unset($args['filter'][self::URL_PATH]);
87+
}
88+
return $args;
89+
}
90+
}

app/code/Magento/CatalogGraphQl/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<type name="Magento\Framework\GraphQl\Query\Resolver\ArgumentsCompositeProcessor">
7979
<arguments>
8080
<argument name="processors" xsi:type="array">
81+
<item name="category_url_path" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\Query\CategoryUrlPathArgsProcessor</item>
8182
<item name="category_uid" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\Query\CategoryUidArgsProcessor</item>
8283
<item name="category_uids" xsi:type="object">Magento\CatalogGraphQl\Model\Category\CategoryUidsArgsProcessor</item>
8384
<item name="parent_category_uids" xsi:type="object">Magento\CatalogGraphQl\Model\Category\ParentCategoryUidsArgsProcessor</item>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ type CategoryProducts @doc(description: "Contains details about the products ass
344344
input ProductAttributeFilterInput @doc(description: "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.") {
345345
category_id: FilterEqualTypeInput @deprecated(reason: "Use `category_uid` instead.") @doc(description: "Deprecated: use `category_uid` to filter product by category ID.")
346346
category_uid: FilterEqualTypeInput @doc(description: "Filter product by the unique ID for a `CategoryInterface` object.")
347+
category_url_path: FilterEqualTypeInput @doc(description: "Filter product by category URL path.")
347348
}
348349

349350
input CategoryFilterInput @doc(description: "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.")

0 commit comments

Comments
 (0)