This repository was archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
GraphQL - Added sort by options to Products GraphQL type #12
Merged
magento-engcom-team
merged 6 commits into
magento:2.3-develop
from
zbigniewkuras:issiue-3
May 22, 2018
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd3aa38
GraphQL - Added sort by options to Products GraphQL type
zbigniewkuras 02d6a22
GraphQL - Added missing constructor doc block type for StoreManagerIn…
zbigniewkuras 9035d29
GraphQL - Created separate resolver for sort_fields
zbigniewkuras d804bab
GraphQL - Removed unnecessary empty lines.
zbigniewkuras 9962c66
GraphQL - Created one resolver for sort_fields. Added tests.
zbigniewkuras 1855062
GraphQL - moved sort_fields to Products object. Some fixes
zbigniewkuras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
app/code/Magento/CatalogGraphQl/Model/Resolver/Category/SortFieldDefault.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\CatalogGraphQl\Model\Resolver\Category; | ||
|
|
||
| use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
| use Magento\Framework\GraphQl\Config\Element\Field; | ||
| use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
| use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
| use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
|
|
||
| /** | ||
| * Retrieves the sort field default value | ||
| */ | ||
| class SortFieldDefault implements ResolverInterface | ||
| { | ||
| /** | ||
| * @var ValueFactory | ||
| */ | ||
| private $valueFactory; | ||
|
|
||
| /** | ||
| * @var \Magento\Catalog\Model\Config | ||
| */ | ||
| private $catalogConfig; | ||
|
|
||
| /** | ||
| * @var \Magento\Store\Model\StoreManagerInterface | ||
| */ | ||
| private $storeManager; | ||
|
|
||
| /** | ||
| * @param ValueFactory $valueFactory | ||
| * @param \Magento\Catalog\Model\Config $catalogConfig | ||
| * @param \Magento\Store\Model\StoreManagerInterface $storeManager | ||
| */ | ||
| public function __construct( | ||
| ValueFactory $valueFactory, | ||
| \Magento\Catalog\Model\Config $catalogConfig, | ||
| \Magento\Store\Model\StoreManagerInterface $storeManager | ||
| ) { | ||
| $this->valueFactory = $valueFactory; | ||
| $this->catalogConfig = $catalogConfig; | ||
| $this->storeManager = $storeManager; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value | ||
| { | ||
| $sortFieldDefault = $this->catalogConfig->getProductListDefaultSortBy($this->storeManager->getStore()->getId()); | ||
|
|
||
| $result = function () use ($sortFieldDefault) { | ||
| return $sortFieldDefault; | ||
| }; | ||
|
|
||
| return $this->valueFactory->create($result); | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
app/code/Magento/CatalogGraphQl/Model/Resolver/Category/SortFieldsOptions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\CatalogGraphQl\Model\Resolver\Category; | ||
|
|
||
| use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
| use Magento\Framework\GraphQl\Config\Element\Field; | ||
| use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
| use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
| use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
|
|
||
| /** | ||
| * Retrieves the sort fields options information object | ||
| */ | ||
| class SortFieldsOptions implements ResolverInterface | ||
| { | ||
| /** | ||
| * @var ValueFactory | ||
| */ | ||
| private $valueFactory; | ||
|
|
||
| /** | ||
| * @var \Magento\Catalog\Model\Config | ||
| */ | ||
| private $catalogConfig; | ||
|
|
||
| /** | ||
| * @param ValueFactory $valueFactory | ||
| * @param \Magento\Catalog\Model\Config $catalogConfig | ||
| */ | ||
| public function __construct( | ||
| ValueFactory $valueFactory, | ||
| \Magento\Catalog\Model\Config $catalogConfig | ||
| ) { | ||
| $this->valueFactory = $valueFactory; | ||
| $this->catalogConfig = $catalogConfig; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value | ||
| { | ||
| $sortFieldsOptions = [ | ||
| ['key' => 'position', 'label' => 'Position'] | ||
| ]; | ||
| foreach ($this->catalogConfig->getAttributesUsedForSortBy() as $attribute) { | ||
| $sortFieldsOptions[] = ['key' => $attribute->getAttributeCode(), 'label' => $attribute->getStoreLabel()]; | ||
| } | ||
|
|
||
| $result = function () use ($sortFieldsOptions) { | ||
| return $sortFieldsOptions; | ||
| }; | ||
|
|
||
| return $this->valueFactory->create($result); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\SortFieldDefault") | ||
| options: [SortField] @doc(description: "Available sort fields") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\SortFieldsOptions") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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']); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:keyis renamed tovalueas 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::getAllOptionscasuses 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: