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

Commit

Permalink
GraphQL - Created separate resolver for sort_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigniewkuras committed May 16, 2018
1 parent 02d6a22 commit 9035d29
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* Retrieves the sort field default value
*/
class SortFieldDefault implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @var \Magento\Catalog\Model\Config
*/
private $catalogConfig;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @param ValueFactory $valueFactory
* @param \Magento\Catalog\Model\Config $catalogConfig
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
ValueFactory $valueFactory,
\Magento\Catalog\Model\Config $catalogConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->valueFactory = $valueFactory;
$this->catalogConfig = $catalogConfig;
$this->storeManager = $storeManager;
}

/**
* {@inheritDoc}
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
{
$sortFieldDefault = $this->catalogConfig->getProductListDefaultSortBy($this->storeManager->getStore()->getId());

$result = function () use ($sortFieldDefault) {
return $sortFieldDefault;
};

return $this->valueFactory->create($result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* Retrieves the sort fields options information object
*/
class SortFieldsOptions implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @var \Magento\Catalog\Model\Config
*/
private $catalogConfig;

/**
* @param ValueFactory $valueFactory
* @param \Magento\Catalog\Model\Config $catalogConfig
*/
public function __construct(
ValueFactory $valueFactory,
\Magento\Catalog\Model\Config $catalogConfig
) {
$this->valueFactory = $valueFactory;
$this->catalogConfig = $catalogConfig;
}

/**
* {@inheritDoc}
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
{
$sortFieldsOptions = [
['key' => 'position', 'label' => 'Position']
];
foreach ($this->catalogConfig->getAttributesUsedForSortBy() as $attribute) {
$sortFieldsOptions[] = ['key' => $attribute->getAttributeCode(), 'label' => $attribute->getStoreLabel()];
}

$result = function () use ($sortFieldsOptions) {
return $sortFieldsOptions;
};

return $this->valueFactory->create($result);
}
}
33 changes: 3 additions & 30 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,27 @@ class Products implements ResolverInterface
* @var Layer\DataProvider\Filters
*/
private $filtersDataProvider;

/**
* @var \Magento\Catalog\Model\Config
*/
private $catalogConfig;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @param Builder $searchCriteriaBuilder
* @param Search $searchQuery
* @param Filter $filterQuery
* @param ValueFactory $valueFactory
* @param \Magento\Catalog\Model\Config $catalogConfig
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
Builder $searchCriteriaBuilder,
Search $searchQuery,
Filter $filterQuery,
SearchFilter $searchFilter,
ValueFactory $valueFactory,
\Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider,
\Magento\Catalog\Model\Config $catalogConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager
\Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->searchQuery = $searchQuery;
$this->filterQuery = $filterQuery;
$this->searchFilter = $searchFilter;
$this->valueFactory = $valueFactory;
$this->filtersDataProvider = $filtersDataProvider;
$this->catalogConfig = $catalogConfig;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -134,25 +118,14 @@ public function resolve(
);
}

$options = $this->catalogConfig->getAttributeUsedForSortByArray();

$sortFields = [
'default' => $this->catalogConfig->getProductListDefaultSortBy($this->storeManager->getStore()->getId()),
'options' => []
];

$sortFields['options'][] = ['key' => 'position', 'label' => 'Position'];
foreach ($this->catalogConfig->getAttributesUsedForSortBy() as $attribute) {
$sortFields['options'][] = ['key' => $attribute->getAttributeCode(), 'label' => $attribute->getStoreLabel()];
}


$data = [
'total_count' => $searchResult->getTotalCount(),
'items' => $searchResult->getProductsSearchResult(),
'page_info' => [
'page_size' => $searchCriteria->getPageSize(),
'current_page' => $currentPage,
'sort_fields' => $sortFields,
'sort_fields' => [],
],
'filters' => $this->filtersDataProvider->getData($layerType),

Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/GraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ type SortField {
}

type SortFields @doc(description: "SortFields contains a default value for sort fields and all available sort fields") {
default: String @doc(description: "Default value of sort fields")
options: [SortField] @doc(description: "Available sort fields")
default: String @doc(description: "Default value of sort fields") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\SortFieldDefault")
options: [SortField] @doc(description: "Available sort fields") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\SortFieldsOptions")
}

0 comments on commit 9035d29

Please sign in to comment.