Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

[BugFix] Graph Ql category tree structure is lost (children all grouped with last item) #246

Closed
raivisdejus opened this issue Nov 12, 2018 · 5 comments
Assignees
Labels
bug Something isn't working Component: CatalogGraphQl

Comments

@raivisdejus
Copy link

Preconditions

Magento 2.3-develop

Steps to reproduce

Run the following Graph Ql query

{
    category(id: 2) {
        children {
            name
            children {
                name
                children {
                    name
                    children {
                        name
                    }
                }
            }
        }
    }
}

Expected result

Category tree with categories and their children. Tree matches site category structure

Actual result

Categories are sorted according to their levels where last items get all the children.

@raivisdejus
Copy link
Author

Fix with tests for this issue has been prepared and will be uploaded for code review and pull request soon.

@naydav naydav changed the title Graph Ql category tree structure is lost (children all grouped with last item) [BugFix] Graph Ql category tree structure is lost (children all grouped with last item) Dec 12, 2018
@naydav naydav added this to the Release: 2.3.1 milestone Dec 12, 2018
@naydav naydav assigned naydav and raivisdejus and unassigned naydav Dec 12, 2018
@misha-kotov misha-kotov added bug Something isn't working in-progress labels Dec 14, 2018
@danslo
Copy link
Contributor

danslo commented Jan 15, 2019

Ran into this as well. We're currently using the following diff to work around it for now:

diff --git a/Model/Resolver/Products/DataProvider/CategoryTree.php b/Model/Resolver/Products/DataProvider/CategoryTree.php
index bae9def4ee6..8201c6d7819 100644
--- a/Model/Resolver/Products/DataProvider/CategoryTree.php
+++ b/Model/Resolver/Products/DataProvider/CategoryTree.php
@@ -98,29 +98,33 @@ class CategoryTree
         $collection->addPathFilter(sprintf('.*/%s/[/0-9]*$', $rootCategoryId));
         $collection->addFieldToFilter('level', ['gt' => $level]);
         $collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
-        $collection->setOrder('level');
+        $collection->setOrder('path');
         $collection->getSelect()->orWhere(
             $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField() . ' = ?',
             $rootCategoryId
         );
-        return $this->processTree($collection->getIterator());
+        return $this->processTree($collection->getIterator(), $level);
     }
 
     /**
      * @param \Iterator $iterator
      * @return array
      */
-    private function processTree(\Iterator $iterator) : array
+    private function processTree(\Iterator $iterator, $currentLevel) : array
     {
         $tree = [];
         while ($iterator->valid()) {
             /** @var CategoryInterface $category */
             $category = $iterator->current();
+            if ($currentLevel > $category->getLevel()) {
+                break;
+            }
             $iterator->next();
             $nextCategory = $iterator->current();
             $tree[$category->getId()] = $this->hydrator->hydrateCategory($category);
-            if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
-                $tree[$category->getId()]['children'] = $this->processTree($iterator);
+            if ($nextCategory && $nextCategory->getLevel() > $category->getLevel()) {
+                $tree[$nextCategory->getParentId()]['children'] =
+                    $this->processTree($iterator, $nextCategory->getLevel());
             }
         }

Probably not the most elegant way, and also 2.3-develop has refactored many parts and this diff applies on 2.3.0 only.

Looking forward to a "proper" patch :)

@nuzil
Copy link
Contributor

nuzil commented Jan 24, 2019

It was fixed within this PR and merged
#164

@raivisdejus
Copy link
Author

Glad to hear this was fixed

@naydav
Copy link
Contributor

naydav commented Feb 11, 2019

#343

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working Component: CatalogGraphQl
Projects
None yet
Development

No branches or pull requests

6 participants