Skip to content

Commit b91bfc7

Browse files
committed
MC-39836: CategoryFilterInput(parent_id) has to accept parent_uid and UIDS as input
- added parent_category_uid - fixed tests
1 parent 057c268 commit b91bfc7

File tree

5 files changed

+134
-14
lines changed

5 files changed

+134
-14
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Framework\GraphQl\Query\Uid;
12+
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
13+
14+
/**
15+
* Parent Category UID processor class for category uid and category id arguments
16+
*/
17+
class ParentCategoryUidsArgsProcessor implements ArgumentsProcessorInterface
18+
{
19+
private const ID = 'parent_id';
20+
21+
private const UID = 'parent_category_uid';
22+
23+
/** @var Uid */
24+
private $uidEncoder;
25+
26+
/**
27+
* @param Uid $uidEncoder
28+
*/
29+
public function __construct(Uid $uidEncoder)
30+
{
31+
$this->uidEncoder = $uidEncoder;
32+
}
33+
34+
/**
35+
* Composite processor that loops through available processors for arguments that come from graphql input
36+
*
37+
* @param string $fieldName,
38+
* @param array $args
39+
* @return array
40+
* @throws GraphQlInputException
41+
*/
42+
public function process(
43+
string $fieldName,
44+
array $args
45+
): array {
46+
$filterKey = 'filters';
47+
$parentUidFilter = $args[$filterKey][self::UID] ?? [];
48+
$parentIdFilter = $args[$filterKey][self::ID] ?? [];
49+
if (!empty($parentIdFilter)
50+
&& !empty($parentUidFilter)
51+
&& ($fieldName === 'categories' || $fieldName === 'categoryList')) {
52+
throw new GraphQlInputException(
53+
__('`%1` and `%2` can\'t be used at the same time.', [self::ID, self::UID])
54+
);
55+
} elseif (!empty($parentUidFilter)) {
56+
if (isset($parentUidFilter['eq'])) {
57+
$args[$filterKey][self::ID]['eq'] = $this->uidEncoder->decode(
58+
$parentUidFilter['eq']
59+
);
60+
} elseif (!empty($parentUidFilter['in'])) {
61+
foreach ($parentUidFilter['in'] as $parentUids) {
62+
$args[$filterKey][self::ID]['in'][] = $this->uidEncoder->decode($parentUids);
63+
}
64+
}
65+
unset($args[$filterKey][self::UID]);
66+
}
67+
return $args;
68+
}
69+
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
1313

1414
/**
15-
* Category UID processor class for category uid and category id arguments
15+
* Parent category UID processor class for category uid and category id arguments
1616
*/
17-
class CategoryUidArgsProcessor implements ArgumentsProcessorInterface
17+
class ParentCategoryUidArgsProcessor implements ArgumentsProcessorInterface
1818
{
19-
private const ID = 'category_id';
19+
private const ID = 'parent_id';
2020

21-
private const UID = 'category_uid';
21+
private const UID = 'parent_uid';
2222

2323
/** @var Uid */
2424
private $uidEncoder;
@@ -43,19 +43,19 @@ public function process(
4343
string $fieldName,
4444
array $args
4545
): array {
46-
$idFilter = $args['filter'][self::ID] ?? [];
47-
$uidFilter = $args['filter'][self::UID] ?? [];
48-
if (!empty($idFilter)
49-
&& !empty($uidFilter)
46+
$parentUidFilter = $args['filter'][self::UID] ?? [];
47+
$parentIdFilter = $args['filter'][self::ID] ?? [];
48+
if (!empty($parentIdFilter)
49+
&& !empty($parentUidFilter)
5050
&& $fieldName === 'products') {
5151
throw new GraphQlInputException(
5252
__('`%1` and `%2` can\'t be used at the same time.', [self::ID, self::UID])
5353
);
54-
} elseif (!empty($uidFilter)) {
55-
if (isset($uidFilter['eq'])) {
56-
$args['filter'][self::ID]['eq'] = $this->uidEncoder->decode((string) $uidFilter['eq']);
57-
} elseif (!empty($uidFilter['in'])) {
58-
foreach ($uidFilter['in'] as $uid) {
54+
} elseif (!empty($parentUidFilter)) {
55+
if (isset($parentUidFilter['eq'])) {
56+
$args['filter'][self::ID]['eq'] = $this->uidEncoder->decode((string) $parentUidFilter['eq']);
57+
} elseif (!empty($parentUidFilter['in'])) {
58+
foreach ($parentUidFilter['in'] as $uid) {
5959
$args['filter'][self::ID]['in'][] = $this->uidEncoder->decode((string) $uid);
6060
}
6161
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<argument name="processors" xsi:type="array">
7373
<item name="category_uid" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\Query\CategoryUidArgsProcessor</item>
7474
<item name="category_uids" xsi:type="object">Magento\CatalogGraphQl\Model\Category\CategoryUidsArgsProcessor</item>
75+
<item name="parent_category_uids" xsi:type="object">Magento\CatalogGraphQl\Model\Category\ParentCategoryUidsArgsProcessor</item>
7576
</argument>
7677
</arguments>
7778
</type>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ input CategoryFilterInput @doc(description: "CategoryFilterInput defines the fi
332332
{
333333
ids: FilterEqualTypeInput @deprecated(reason: "Use the `category_uid` argument instead.") @doc(description: "Deprecated: use 'category_uid' to filter uniquely identifiers of categories.")
334334
category_uid: FilterEqualTypeInput @doc(description: "Filter by the unique category ID for a `CategoryInterface` object.")
335-
parent_id: FilterEqualTypeInput @doc(description: "Filter by the unique parent category ID for a `CategoryInterface` object.")
335+
parent_id: FilterEqualTypeInput @deprecated @doc(description: "Filter by the unique parent category ID for a `CategoryInterface` object.")
336+
parent_category_uid: FilterEqualTypeInput @doc(description: "Filter by the unique parent category ID for a `CategoryInterface` object.")
336337
url_key: FilterEqualTypeInput @doc(description: "Filter by the part of the URL that identifies the category.")
337338
name: FilterMatchTypeInput @doc(description: "Filter by the display name of the category.")
338339
url_path: FilterEqualTypeInput @doc(description: "Filter by the URL path for the category.")

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoriesQuery/CategoriesFilterTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,20 @@ public function filterSingleCategoryDataProvider(): array
613613
'position' => '1'
614614
]
615615
],
616+
[
617+
'parent_category_uid',
618+
'eq',
619+
'NA==',
620+
[
621+
'id' => '5',
622+
'name' => 'Category 1.1.1',
623+
'url_key' => 'category-1-1-1',
624+
'url_path' => 'category-1/category-1-1/category-1-1-1',
625+
'children_count' => '0',
626+
'path' => '1/2/3/4/5',
627+
'position' => '1'
628+
]
629+
],
616630
[
617631
'name',
618632
'match',
@@ -761,6 +775,41 @@ public function filterMultipleCategoriesDataProvider(): array
761775
]
762776
]
763777
],
778+
// Filter by multiple parent UIDs
779+
[
780+
'parent_category_uid',
781+
'in',
782+
'["Mw==", "NA=="]',
783+
[
784+
[
785+
'id' => '4',
786+
'name' => 'Category 1.1',
787+
'url_key' => 'category-1-1',
788+
'url_path' => 'category-1/category-1-1',
789+
'children_count' => '0',
790+
'path' => '1/2/3/4',
791+
'position' => '1'
792+
],
793+
[
794+
'id' => '5',
795+
'name' => 'Category 1.1.1',
796+
'url_key' => 'category-1-1-1',
797+
'url_path' => 'category-1/category-1-1/category-1-1-1',
798+
'children_count' => '0',
799+
'path' => '1/2/3/4/5',
800+
'position' => '1'
801+
],
802+
[
803+
'id' => '13',
804+
'name' => 'Category 1.2',
805+
'url_key' => 'category-1-2',
806+
'url_path' => 'category-1/category-1-2',
807+
'children_count' => '0',
808+
'path' => '1/2/3/13',
809+
'position' => '2'
810+
]
811+
]
812+
],
764813
//Filter by multiple url keys
765814
[
766815
'url_key',

0 commit comments

Comments
 (0)