Skip to content

Commit cc9295c

Browse files
32460 GraphQL: add category_url_path to ProductAttributeFilterInput
1 parent d3f202a commit cc9295c

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 uid and category id arguments
17+
*/
18+
class CategoryUrlPathArgsProcessor implements ArgumentsProcessorInterface
19+
{
20+
private const ID = 'category_id';
21+
22+
private const UID = 'category_uid';
23+
24+
private const 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::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::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::PATH])
63+
);
64+
}
65+
66+
/** @var Collection $collection */
67+
$collection = $this->collectionFactory->create();
68+
$collection->addAttributeToSelect('entity_id');
69+
$collection->addAttributeToFilter('url_path', $pathFilter['eq']);
70+
71+
if ($collection->count() === 0) {
72+
throw new GraphQlInputException(
73+
__('No category with the provided %1 was found', ['category_url_path'])
74+
);
75+
}
76+
$category = $collection->getFirstItem();
77+
$args['filter'][self::ID]['eq'] = $category->getId();
78+
79+
unset($args['filter'][self::PATH]);
80+
}
81+
return $args;
82+
}
83+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<type name="Magento\Framework\GraphQl\Query\Resolver\ArgumentsCompositeProcessor">
7171
<arguments>
7272
<argument name="processors" xsi:type="array">
73+
<item name="category_url_path" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\Query\CategoryUrlPathArgsProcessor</item>
7374
<item name="category_uid" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\Query\CategoryUidArgsProcessor</item>
7475
<item name="category_uids" xsi:type="object">Magento\CatalogGraphQl\Model\Category\CategoryUidsArgsProcessor</item>
7576
<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
@@ -333,6 +333,7 @@ type CategoryProducts @doc(description: "The category products object returned i
333333
input ProductAttributeFilterInput @doc(description: "ProductAttributeFilterInput 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.") {
334334
category_id: FilterEqualTypeInput @deprecated(reason: "Use the `category_uid` argument instead.") @doc(description: "Deprecated: use `category_uid` to filter product by category id.")
335335
category_uid: FilterEqualTypeInput @doc(description: "Filter product by the unique ID for a `CategoryInterface` object.")
336+
category_url_path: FilterEqualTypeInput @doc(description: "Filter product by category URL path.")
336337
}
337338

338339
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.")

0 commit comments

Comments
 (0)