16
16
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
17
17
use Magento \CatalogGraphQl \Model \Resolver \Products \DataProvider \CategoryTree ;
18
18
use Magento \CatalogGraphQl \Model \Category \CategoryFilter ;
19
+ use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory ;
19
20
20
21
/**
21
22
* Category List resolver, used for GraphQL category data request processing.
@@ -27,6 +28,11 @@ class CategoryList implements ResolverInterface
27
28
*/
28
29
private $ categoryTree ;
29
30
31
+ /**
32
+ * @var CollectionFactory
33
+ */
34
+ private $ collectionFactory ;
35
+
30
36
/**
31
37
* @var CategoryFilter
32
38
*/
@@ -47,17 +53,20 @@ class CategoryList implements ResolverInterface
47
53
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
48
54
* @param CheckCategoryIsActive $checkCategoryIsActive
49
55
* @param CategoryFilter $categoryFilter
56
+ * @param CollectionFactory $collectionFactory
50
57
*/
51
58
public function __construct (
52
59
CategoryTree $ categoryTree ,
53
60
ExtractDataFromCategoryTree $ extractDataFromCategoryTree ,
54
61
CheckCategoryIsActive $ checkCategoryIsActive ,
55
- CategoryFilter $ categoryFilter
62
+ CategoryFilter $ categoryFilter ,
63
+ CollectionFactory $ collectionFactory
56
64
) {
57
65
$ this ->categoryTree = $ categoryTree ;
58
66
$ this ->extractDataFromCategoryTree = $ extractDataFromCategoryTree ;
59
67
$ this ->checkCategoryIsActive = $ checkCategoryIsActive ;
60
68
$ this ->categoryFilter = $ categoryFilter ;
69
+ $ this ->collectionFactory = $ collectionFactory ;
61
70
}
62
71
63
72
/**
@@ -69,10 +78,18 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
69
78
return $ value [$ field ->getName ()];
70
79
}
71
80
81
+ $ categoryCollection = $ this ->collectionFactory ->create ();
82
+ $ categoryCollection ->addAttributeToFilter ('is_active ' , 1 );
83
+ $ categoryCollection ->addAttributeToSelect (['name ' ,'url_key ' , 'ids ' ]);
84
+
72
85
if (!isset ($ args ['filters ' ])) {
73
86
$ rootCategoryIds = [(int )$ context ->getExtensionAttributes ()->getStore ()->getRootCategoryId ()];
74
87
} else {
75
- $ rootCategoryIds = $ this ->categoryFilter ->applyFilters ($ args );
88
+ $ this ->categoryFilter ->applyFilters ($ args , $ categoryCollection );
89
+ $ rootCategoryIds = [];
90
+ foreach ($ categoryCollection as $ category ) {
91
+ $ rootCategoryIds [] = (int )$ category ->getId ();
92
+ }
76
93
}
77
94
78
95
$ result = [];
@@ -81,7 +98,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
81
98
$ this ->checkCategoryIsActive ->execute ($ rootCategoryId );
82
99
}
83
100
$ categoryTree = $ this ->categoryTree ->getTree ($ info , $ rootCategoryId );
84
- if (empty ($ categoryTree )) {
101
+ if (empty ($ categoryTree ) || ( $ categoryTree -> count () == 0 ) ) {
85
102
throw new GraphQlNoSuchEntityException (__ ('Category doesn \'t exist ' ));
86
103
}
87
104
$ result [] = current ($ this ->extractDataFromCategoryTree ->execute ($ categoryTree ));
0 commit comments