Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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 data
*/
class SortFields implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;

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

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

/**
* @var \Magento\Catalog\Model\Category\Attribute\Source\Sortby
*/
private $sortbyAttributeSource;

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

/**
* {@inheritDoc}
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
{
$sortFieldsOptions = $this->sortbyAttributeSource->getAllOptions();
array_walk(
$sortFieldsOptions,
function (&$option) {
$option['label'] = (string)$option['label'];
}
);
$data = [
'default' => $this->catalogConfig->getProductListDefaultSortBy($this->storeManager->getStore()->getId()),
'options' => $sortFieldsOptions,
];

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

return $this->valueFactory->create($result);
}
}
5 changes: 3 additions & 2 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ public function resolve(
'items' => $searchResult->getProductsSearchResult(),
'page_info' => [
'page_size' => $searchCriteria->getPageSize(),
'current_page' => $currentPage
'current_page' => $currentPage,
'sort_fields' => [],
],
'filters' => $this->filtersDataProvider->getData($layerType)
'filters' => $this->filtersDataProvider->getData($layerType),
];

$result = function () use ($data) {
Expand Down
11 changes: 11 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ type Products @doc(description: "The Products object is the top-level object ret
page_info: SearchResultPageInfo @doc(description: "An object that includes the page_info and currentPage values specified in the query")
total_count: Int @doc(description: "The number of products returned")
filters: [LayerFilter] @doc(description: "Layered navigation filters array")
sort_fields: SortFields @doc(description: "An object that includes the default sort field and all available sort fields") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\SortFields")
}

input ProductFilterInput @doc(description: "ProductFilterInput defines the filters to be used in the search. A filter contains at least one attribute, a comparison operator, and the value that is being searched for.") {
Expand Down Expand Up @@ -521,3 +522,13 @@ interface LayerFilterItemInterface @typeResolver(class: "Magento\\CatalogGraphQl
type LayerFilterItem implements LayerFilterItemInterface {

}

type SortField {
value: String @doc(description: "Attribute code of sort field")
label: String @doc(description: "Label of sort field")
}

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")
}
2 changes: 1 addition & 1 deletion app/code/Magento/GraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ type SearchResultPageInfo @doc(description: "SearchResultPageInfo provides navig
enum SortEnum @doc(description: "This enumeration indicates whether to return results in ascending or descending order") {
ASC
DESC
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,15 @@ public function testQueryProductsInCurrentPageSortedByPriceASC()
page_size
current_page
}
sort_fields
{
default
options
{
value
label
}
}
}
}
QUERY;
Expand All @@ -530,6 +539,13 @@ public function testQueryProductsInCurrentPageSortedByPriceASC()
$this->assertProductItems($filteredChildProducts, $response);
$this->assertEquals(4, $response['products']['page_info']['page_size']);
$this->assertEquals(1, $response['products']['page_info']['current_page']);
$this->assertArrayHasKey('sort_fields', $response['products']);
$this->assertArrayHasKey('options', $response['products']['sort_fields']);
$this->assertArrayHasKey('default', $response['products']['sort_fields']);
$this->assertEquals('position', $response['products']['sort_fields']['default']);
$this->assertArrayHasKey('value', $response['products']['sort_fields']['options'][0]);
$this->assertArrayHasKey('label', $response['products']['sort_fields']['options'][0]);
$this->assertEquals('position', $response['products']['sort_fields']['options'][0]['value']);
}

/**
Expand Down