-
Notifications
You must be signed in to change notification settings - Fork 154
GraphQL - Added sort by options to Products GraphQL type #12
GraphQL - Added sort by options to Products GraphQL type #12
Conversation
\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 |
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 StoreManagerInterface
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.
Added!
Closed and Re Opened PR to trigger Travis CI builds. |
@@ -118,14 +134,28 @@ public function resolve( | |||
); | |||
} | |||
|
|||
$options = $this->catalogConfig->getAttributeUsedForSortByArray(); |
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 create a separate resolver for sort_fields
. There are 2 main reasons for that:
- Performance. In case
sort_fields
is not requested, all this logic will not be executed in case of separate resolver - Following single responsibility and object decomposition simplifies maintenance and understanding
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.
Roger, i will fix it, thanks for your feedback
Hi @zbigniewkuras |
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.
@zbigniewkuras thank you for making additional changes, please see new comments. As soon as those are addressed, we can merge the PR.
@@ -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") |
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.
There should be a single new resolver, just for sort_fields
.
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value | ||
{ | ||
$sortFieldsOptions = [ |
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 use \Magento\Catalog\Model\Category\Attribute\Source\Sortby::getAllOptions
. It has two benefits:
- includes position, so no need to hardcode it here again
- result is already properly formatted, no need to do foreach (assuming that
key
is renamed tovalue
as suggested in another comment)
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.
The reason i didn't use this function was the translation function called for labels in \Magento\Catalog\Model\Category\Attribute\Source\Sortby::getAllOptions
casuses Internal server error and every value for label is equal null. I'm not sure exactly, but it looks like a bug.
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.
How about this one \Magento\Catalog\Model\Config::getAttributeUsedForSortByArray
?
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.
It's the same, this function also contains translation function for Position option
$options = ['position' => __('Position')];
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.
The problem is that underlying GraphQL library (webonyx) expects exact match between declared type (string) and the actual value type (Magento\Framework\Phrase), and it does not try to perform type casting.
I was able to make it work by manually casting Phrase to string before returning the result:
$sortFieldsOptions = $this->sortBy->getAllOptions();
array_walk(
$sortFieldsOptions,
function (&$option) {
$option['label'] = (string)$option['label'];
}
);
default | ||
options | ||
{ | ||
key |
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 rename key
to value
for consistency with naming in other places in Magento, for example \Magento\Catalog\Model\Category\Attribute\Source\Sortby::getAllOptions
@@ -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']); |
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 assertion for options
values, it is not validated at the moment.
@@ -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") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\SortFields") |
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.
sort_fields
should be moved to Products
type, as mentioned in the issue description, because SearchResultPageInfo
is meant for storing paginating information.
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.
Okey
ValueFactory $valueFactory, | ||
\Magento\Catalog\Model\Config $catalogConfig, | ||
\Magento\Store\Model\StoreManagerInterface $storeManager, | ||
\Magento\Catalog\Model\Category\Attribute\Source\Sortby $ss |
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.
Looks like an accidental change.
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.
Yes, you are right. I was testing function \Magento\Catalog\Model\Category\Attribute\Source\Sortby::getAllOptions()
. I will fix it
Approved ✅ |
@zbigniewkuras, the PR has been merged to |
Added SortFields information in the Products endpoint documentation. |
[Forwardport] Corrected function comment
2.3 develop update
Description
Added available sort options and default sort option for product listing to page_info object of Products GraphQL object.
Fixed Issues (if relevant)
Manual testing scenarios
sort_fields { default options { key label } }
Contribution checklist