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

Commit

Permalink
GraphQL - moved sort_fields to Products object. Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigniewkuras committed May 18, 2018
1 parent 9962c66 commit 1855062
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,42 @@ class SortFields implements ResolverInterface
* @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 $ss
\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 = [
['value' => 'position', 'label' => 'Position']
];
foreach ($this->catalogConfig->getAttributesUsedForSortBy() as $attribute) {
$sortFieldsOptions[] = ['value' => $attribute->getAttributeCode(), 'label' => $attribute->getStoreLabel()];
}

$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,
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")
}
11 changes: 0 additions & 11 deletions app/code/Magento/GraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,9 @@ input FilterTypeInput @doc(description: "FilterTypeInput specifies which action
type SearchResultPageInfo @doc(description: "SearchResultPageInfo provides navigation for the query response") {
page_size: Int @doc(description: "Specifies the maximum number of items to return")
current_page: Int @doc(description: "Specifies which page of results to return")
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")
}

enum SortEnum @doc(description: "This enumeration indicates whether to return results in ascending or descending order") {
ASC
DESC
}

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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,14 @@ public function testQueryProductsInCurrentPageSortedByPriceASC()
{
page_size
current_page
sort_fields
}
sort_fields
{
default
options
{
default
options
{
value
label
}
value
label
}
}
}
Expand All @@ -539,13 +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']['page_info']);
$this->assertArrayHasKey('options', $response['products']['page_info']['sort_fields']);
$this->assertArrayHasKey('default', $response['products']['page_info']['sort_fields']);
$this->assertEquals('position', $response['products']['page_info']['sort_fields']['default']);
$this->assertArrayHasKey('value', $response['products']['page_info']['sort_fields']['options'][0]);
$this->assertArrayHasKey('label', $response['products']['page_info']['sort_fields']['options'][0]);
$this->assertEquals(['value'=>'position', 'label' => 'Position'], $response['products']['page_info']['sort_fields']['options'][0]);
$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 1855062

Please sign in to comment.