Skip to content

Commit

Permalink
ENGCOM-2421: Added breadcrumbs support #112
Browse files Browse the repository at this point in the history
 - Merge Pull Request magento/graphql-ce#112 from magento/graphql-ce:19-add-breadcrumbs-support
 - Merged commits:
   1. 34ed6d1
   2. 78a67c3
   3. 3d973af
   4. cfd7502
  • Loading branch information
magento-engcom-team committed Jul 19, 2018
2 parents 6200e46 + cfd7502 commit 606fb83
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Category;

use \Magento\CatalogGraphQl\Model\Resolver\Category\DataProvider\Breadcrumbs as BreadcrumbsDataProvider;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;

/**
* Retrieves breadcrumbs
*/
class Breadcrumbs implements ResolverInterface
{
/**
* @var BreadcrumbsDataProvider
*/
private $breadcrumbsDataProvider;

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param BreadcrumbsDataProvider $breadcrumbsDataProvider
* @param ValueFactory $valueFactory
*/
public function __construct(
BreadcrumbsDataProvider $breadcrumbsDataProvider,
ValueFactory $valueFactory
) {
$this->breadcrumbsDataProvider = $breadcrumbsDataProvider;
$this->valueFactory = $valueFactory;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): Value
{
if (!isset($value['path'])) {
$result = function () {
return null;
};
return $this->valueFactory->create($result);
}

$result = function () use ($value) {
$breadcrumbsData = $this->breadcrumbsDataProvider->getData($value['path']);
return count($breadcrumbsData) ? $breadcrumbsData : null;
};
return $this->valueFactory->create($result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Category\DataProvider;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

/**
* Breadcrumbs data provider
*/
class Breadcrumbs
{
/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* @param CollectionFactory $collectionFactory
*/
public function __construct(
CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}

/**
* @param string $categoryPath
* @return array
*/
public function getData(string $categoryPath): array
{
$breadcrumbsData = [];

$pathCategoryIds = explode('/', $categoryPath);
$parentCategoryIds = array_slice($pathCategoryIds, 2, -1);

if (count($parentCategoryIds)) {
$collection = $this->collectionFactory->create();
$collection->addAttributeToSelect(['name', 'url_key']);
$collection->addAttributeToFilter('entity_id', $parentCategoryIds);

foreach ($collection as $category) {
$breadcrumbsData[] = [
'category_id' => $category->getId(),
'category_name' => $category->getName(),
'category_level' => $category->getLevel(),
'category_url_key' => $category->getUrlKey(),
];
}
}
return $breadcrumbsData;
}
}
8 changes: 8 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ interface CategoryInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
sort: ProductSortInput @doc(description: "Specifies which attribute to sort on, and whether to return the results in ascending or descending order.")
): CategoryProducts @doc(description: "The list of products assigned to the category") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\Products")
breadcrumbs: [Breadcrumb] @doc(description: "Breadcrumbs, parent categories info") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\Breadcrumbs")
}

type Breadcrumb @doc(description: "Breadcrumb item"){
category_id: Int @doc(description: "Category ID")
category_name: String @doc(description: "Category name")
category_level: Int @doc(description: "Category level")
category_url_key: String @doc(description: "Category URL key")
}

type CustomizableRadioOption implements CustomizableOptionInterface @doc(description: "CustomizableRadioOption contains information about a set of radio buttons that are defined as part of a customizable option") {
Expand Down

0 comments on commit 606fb83

Please sign in to comment.