GraphQL - Added sort by options to Products GraphQL type#12
GraphQL - Added sort by options to Products GraphQL type#12magento-engcom-team merged 6 commits intomagento:2.3-developfrom
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.
Please add constructor doc block type hint for StoreManagerInterface
|
Closed and Re Opened PR to trigger Travis CI builds. |
| ); | ||
| } | ||
|
|
||
| $options = $this->catalogConfig->getAttributeUsedForSortByArray(); |
There was a problem hiding this comment.
Please create a separate resolver for sort_fields. There are 2 main reasons for that:
- Performance. In case
sort_fieldsis 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.
Roger, i will fix it, thanks for your feedback
|
Hi @zbigniewkuras |
paliarush
left a comment
There was a problem hiding this comment.
@zbigniewkuras thank you for making additional changes, please see new comments. As soon as those are addressed, we can merge the PR.
| 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.
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.
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
keyis renamed tovalueas suggested in another comment)
There was a problem hiding this comment.
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.
How about this one \Magento\Catalog\Model\Config::getAttributeUsedForSortByArray?
There was a problem hiding this comment.
It's the same, this function also contains translation function for Position option
$options = ['position' => __('Position')];
There was a problem hiding this comment.
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.
Please rename key to value for consistency with naming in other places in Magento, for example \Magento\Catalog\Model\Category\Attribute\Source\Sortby::getAllOptions
| $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.
Please add assertion for options values, it is not validated at the moment.
| 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.
sort_fields should be moved to Products type, as mentioned in the issue description, because SearchResultPageInfo is meant for storing paginating information.
| 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.
Looks like an accidental change.
There was a problem hiding this comment.
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