Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 2a504ce

Browse files
authored
ENGCOM-1635: GraphQL - Added sort by options to Products GraphQL type #12
2 parents aae0b0c + e1e3fc8 commit 2a504ce

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Category;
9+
10+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\Resolver\Value;
13+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
16+
/**
17+
* Retrieves the sort fields data
18+
*/
19+
class SortFields implements ResolverInterface
20+
{
21+
/**
22+
* @var ValueFactory
23+
*/
24+
private $valueFactory;
25+
26+
/**
27+
* @var \Magento\Catalog\Model\Config
28+
*/
29+
private $catalogConfig;
30+
31+
/**
32+
* @var \Magento\Store\Model\StoreManagerInterface
33+
*/
34+
private $storeManager;
35+
36+
/**
37+
* @var \Magento\Catalog\Model\Category\Attribute\Source\Sortby
38+
*/
39+
private $sortbyAttributeSource;
40+
41+
/**
42+
* @param ValueFactory $valueFactory
43+
* @param \Magento\Catalog\Model\Config $catalogConfig
44+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
45+
* @oaram \Magento\Catalog\Model\Category\Attribute\Source\Sortby $sortbyAttributeSource
46+
*/
47+
public function __construct(
48+
ValueFactory $valueFactory,
49+
\Magento\Catalog\Model\Config $catalogConfig,
50+
\Magento\Store\Model\StoreManagerInterface $storeManager,
51+
\Magento\Catalog\Model\Category\Attribute\Source\Sortby $sortbyAttributeSource
52+
) {
53+
$this->valueFactory = $valueFactory;
54+
$this->catalogConfig = $catalogConfig;
55+
$this->storeManager = $storeManager;
56+
$this->sortbyAttributeSource = $sortbyAttributeSource;
57+
}
58+
59+
/**
60+
* {@inheritDoc}
61+
*/
62+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
63+
{
64+
$sortFieldsOptions = $this->sortbyAttributeSource->getAllOptions();
65+
array_walk(
66+
$sortFieldsOptions,
67+
function (&$option) {
68+
$option['label'] = (string)$option['label'];
69+
}
70+
);
71+
$data = [
72+
'default' => $this->catalogConfig->getProductListDefaultSortBy($this->storeManager->getStore()->getId()),
73+
'options' => $sortFieldsOptions,
74+
];
75+
76+
$result = function () use ($data) {
77+
return $data;
78+
};
79+
80+
return $this->valueFactory->create($result);
81+
}
82+
}

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ type Products @doc(description: "The Products object is the top-level object ret
402402
page_info: SearchResultPageInfo @doc(description: "An object that includes the page_info and currentPage values specified in the query")
403403
total_count: Int @doc(description: "The number of products returned")
404404
filters: [LayerFilter] @doc(description: "Layered navigation filters array")
405+
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")
405406
}
406407

407408
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.") {
@@ -521,3 +522,13 @@ interface LayerFilterItemInterface @typeResolver(class: "Magento\\CatalogGraphQl
521522
type LayerFilterItem implements LayerFilterItemInterface {
522523

523524
}
525+
526+
type SortField {
527+
value: String @doc(description: "Attribute code of sort field")
528+
label: String @doc(description: "Label of sort field")
529+
}
530+
531+
type SortFields @doc(description: "SortFields contains a default value for sort fields and all available sort fields") {
532+
default: String @doc(description: "Default value of sort fields")
533+
options: [SortField] @doc(description: "Available sort fields")
534+
}

app/code/Magento/GraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ type SearchResultPageInfo @doc(description: "SearchResultPageInfo provides navig
3030
enum SortEnum @doc(description: "This enumeration indicates whether to return results in ascending or descending order") {
3131
ASC
3232
DESC
33-
}
33+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,15 @@ public function testQueryProductsInCurrentPageSortedByPriceASC()
512512
page_size
513513
current_page
514514
}
515+
sort_fields
516+
{
517+
default
518+
options
519+
{
520+
value
521+
label
522+
}
523+
}
515524
}
516525
}
517526
QUERY;
@@ -530,6 +539,13 @@ public function testQueryProductsInCurrentPageSortedByPriceASC()
530539
$this->assertProductItems($filteredChildProducts, $response);
531540
$this->assertEquals(4, $response['products']['page_info']['page_size']);
532541
$this->assertEquals(1, $response['products']['page_info']['current_page']);
542+
$this->assertArrayHasKey('sort_fields', $response['products']);
543+
$this->assertArrayHasKey('options', $response['products']['sort_fields']);
544+
$this->assertArrayHasKey('default', $response['products']['sort_fields']);
545+
$this->assertEquals('position', $response['products']['sort_fields']['default']);
546+
$this->assertArrayHasKey('value', $response['products']['sort_fields']['options'][0]);
547+
$this->assertArrayHasKey('label', $response['products']['sort_fields']['options'][0]);
548+
$this->assertEquals('position', $response['products']['sort_fields']['options'][0]['value']);
533549
}
534550

535551
/**

0 commit comments

Comments
 (0)