-
Notifications
You must be signed in to change notification settings - Fork 147
GraphQL - Added sort by options to Products GraphQL type #12
Changes from 1 commit
dd3aa38
02d6a22
9035d29
d804bab
9962c66
1855062
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,27 +53,42 @@ 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 | ||
| */ | ||
| public function __construct( | ||
| Builder $searchCriteriaBuilder, | ||
| Search $searchQuery, | ||
| Filter $filterQuery, | ||
| SearchFilter $searchFilter, | ||
| ValueFactory $valueFactory, | ||
| \Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider | ||
| \Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider, | ||
| \Magento\Catalog\Model\Config $catalogConfig, | ||
| \Magento\Store\Model\StoreManagerInterface $storeManager | ||
| ) { | ||
| $this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
| $this->searchQuery = $searchQuery; | ||
| $this->filterQuery = $filterQuery; | ||
| $this->searchFilter = $searchFilter; | ||
| $this->valueFactory = $valueFactory; | ||
| $this->filtersDataProvider = $filtersDataProvider; | ||
| $this->catalogConfig = $catalogConfig; | ||
| $this->storeManager = $storeManager; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -118,14 +133,28 @@ public function resolve( | |
| ); | ||
| } | ||
|
|
||
| $options = $this->catalogConfig->getAttributeUsedForSortByArray(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a separate resolver for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roger, i will fix it, thanks for your feedback |
||
|
|
||
| $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 | ||
| 'current_page' => $currentPage, | ||
| 'sort_fields' => $sortFields, | ||
| ], | ||
| 'filters' => $this->filtersDataProvider->getData($layerType) | ||
| 'filters' => $this->filtersDataProvider->getData($layerType), | ||
|
|
||
| ]; | ||
|
|
||
| $result = function () use ($data) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,9 +25,20 @@ 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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a single new resolver, just for |
||
| } | ||
|
|
||
| enum SortEnum @doc(description: "This enumeration indicates whether to return results in ascending or descending order") { | ||
| ASC | ||
| DESC | ||
| } | ||
|
|
||
| type SortField { | ||
| key: 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 |
|---|---|---|
|
|
@@ -511,6 +511,15 @@ public function testQueryProductsInCurrentPageSortedByPriceASC() | |
| { | ||
| page_size | ||
| current_page | ||
| sort_fields | ||
| { | ||
| default | ||
| options | ||
| { | ||
| key | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename |
||
| label | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -530,6 +539,10 @@ 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']); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add assertion for |
||
| $this->assertArrayHasKey('default', $response['products']['page_info']['sort_fields']); | ||
| $this->assertEquals('position', $response['products']['page_info']['sort_fields']['default']); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add constructor doc block type hint for
StoreManagerInterfaceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added!