Skip to content
This repository has been archived by the owner on May 20, 2019. It is now read-only.

Commit

Permalink
GraphQL - Created one resolver for sort_fields. Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigniewkuras committed May 17, 2018
1 parent d804bab commit 9962c66
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* Retrieves the sort field default value
* Retrieves the sort fields data
*/
class SortFieldDefault implements ResolverInterface
class SortFields implements ResolverInterface
{
/**
* @var ValueFactory
Expand All @@ -41,7 +41,8 @@ class SortFieldDefault implements ResolverInterface
public function __construct(
ValueFactory $valueFactory,
\Magento\Catalog\Model\Config $catalogConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\Category\Attribute\Source\Sortby $ss
) {
$this->valueFactory = $valueFactory;
$this->catalogConfig = $catalogConfig;
Expand All @@ -53,10 +54,20 @@ public function __construct(
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
{
$sortFieldDefault = $this->catalogConfig->getProductListDefaultSortBy($this->storeManager->getStore()->getId());
$sortFieldsOptions = [
['value' => 'position', 'label' => 'Position']
];
foreach ($this->catalogConfig->getAttributesUsedForSortBy() as $attribute) {
$sortFieldsOptions[] = ['value' => $attribute->getAttributeCode(), 'label' => $attribute->getStoreLabel()];
}

$result = function () use ($sortFieldDefault) {
return $sortFieldDefault;
$data = [
'default' => $this->catalogConfig->getProductListDefaultSortBy($this->storeManager->getStore()->getId()),
'options' => $sortFieldsOptions,
];

$result = function () use ($data) {
return $data;
};

return $this->valueFactory->create($result);
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions app/code/Magento/GraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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")
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")
}

enum SortEnum @doc(description: "This enumeration indicates whether to return results in ascending or descending order") {
Expand All @@ -34,11 +34,11 @@ enum SortEnum @doc(description: "This enumeration indicates whether to return re
}

type SortField {
key: String @doc(description: "Attribute code of sort field")
value: 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")
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
Expand Up @@ -516,7 +516,7 @@ public function testQueryProductsInCurrentPageSortedByPriceASC()
default
options
{
key
value
label
}
}
Expand All @@ -543,6 +543,9 @@ public function testQueryProductsInCurrentPageSortedByPriceASC()
$this->assertArrayHasKey('options', $response['products']['page_info']['sort_fields']);
$this->assertArrayHasKey('default', $response['products']['page_info']['sort_fields']);
$this->assertEquals('position', $response['products']['page_info']['sort_fields']['default']);
$this->assertArrayHasKey('value', $response['products']['page_info']['sort_fields']['options'][0]);
$this->assertArrayHasKey('label', $response['products']['page_info']['sort_fields']['options'][0]);
$this->assertEquals(['value'=>'position', 'label' => 'Position'], $response['products']['page_info']['sort_fields']['options'][0]);
}

/**
Expand Down

0 comments on commit 9962c66

Please sign in to comment.