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

Commit

Permalink
ENGCOM-1635: GraphQL - Added sort by options to Products GraphQL type #…
Browse files Browse the repository at this point in the history
  • Loading branch information
paliarush authored May 22, 2018
2 parents aae0b0c + e1e3fc8 commit 2a504ce
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
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);
}
}
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

0 comments on commit 2a504ce

Please sign in to comment.