This repository has been archived by the owner on May 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL - Created separate resolver for sort_fields
- Loading branch information
1 parent
02d6a22
commit 9035d29
Showing
4 changed files
with
130 additions
and
32 deletions.
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 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 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 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 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