-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
graphql-ce-120: added store configs schema and resolver
- Loading branch information
vitaliyboyko
committed
Jul 18, 2018
1 parent
1d469f8
commit e1f53d4
Showing
3 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigsDataProvider.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,77 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\StoreGraphQl\Model\Resolver\Store; | ||
|
||
use Magento\Store\Api\Data\StoreConfigInterface; | ||
use Magento\Store\Api\StoreConfigManagerInterface; | ||
|
||
/** | ||
* StoreConfig field data provider, used for GraphQL request processing. | ||
*/ | ||
class StoreConfigsDataProvider | ||
{ | ||
/** | ||
* @var StoreConfigManagerInterface | ||
*/ | ||
private $storeConfigManager; | ||
|
||
/** | ||
* @param StoreConfigManagerInterface $storeConfigManager | ||
*/ | ||
public function __construct( | ||
StoreConfigManagerInterface $storeConfigManager | ||
) { | ||
$this->storeConfigManager = $storeConfigManager; | ||
} | ||
|
||
/** | ||
* Get store configs by store codes | ||
* | ||
* @param array $storeCodes | ||
* @return array | ||
*/ | ||
public function getStoreConfigsByStoreCodes(array $storeCodes = null) : array | ||
{ | ||
$storeConfigs = $this->storeConfigManager->getStoreConfigs($storeCodes); | ||
|
||
return ['items' => $this->hidrateStoreConfigs($storeConfigs)]; | ||
} | ||
|
||
/** | ||
* Transform StoreConfig objects to in array format | ||
* | ||
* @param StoreConfigInterface[] $storeConfigs | ||
* @return array | ||
*/ | ||
private function hidrateStoreConfigs(array $storeConfigs) : array | ||
{ | ||
$storeConfigsData = []; | ||
/** @var StoreConfigInterface $storeConfig */ | ||
foreach ($storeConfigs as $storeConfig) { | ||
$storeConfigsData[] = [ | ||
'id' => $storeConfig->getId(), | ||
'code' => $storeConfig->getCode(), | ||
'website_id' => $storeConfig->getWebsiteId(), | ||
'locale' => $storeConfig->getLocale(), | ||
'base_currency_code' => $storeConfig->getBaseCurrencyCode(), | ||
'default_display_currency_code' => $storeConfig->getDefaultDisplayCurrencyCode(), | ||
'timezone' => $storeConfig->getTimezone(), | ||
'weight_unit' => $storeConfig->getWeightUnit(), | ||
'base_url' => $storeConfig->getBaseUrl(), | ||
'base_link_url' => $storeConfig->getBaseLinkUrl(), | ||
'base_static_url' => $storeConfig->getSecureBaseStaticUrl(), | ||
'base_media_url' => $storeConfig->getBaseMediaUrl(), | ||
'secure_base_link_url' => $storeConfig->getSecureBaseLinkUrl(), | ||
'secure_base_static_url' => $storeConfig->getSecureBaseStaticUrl(), | ||
'secure_base_media_url' => $storeConfig->getSecureBaseMediaUrl() | ||
]; | ||
} | ||
|
||
return $storeConfigsData; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigs.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,84 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\StoreGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigsDataProvider; | ||
|
||
/** | ||
* StoreConfig page field resolver, used for GraphQL request processing. | ||
*/ | ||
class StoreConfigs implements ResolverInterface | ||
{ | ||
/** | ||
* @var StoreConfigsDataProvider | ||
*/ | ||
private $storeConfigsDataProvider; | ||
|
||
/** | ||
* @var ValueFactory | ||
*/ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param StoreConfigsDataProvider $storeConfigsDataProvider | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
StoreConfigsDataProvider $storeConfigsDataProvider, | ||
ValueFactory $valueFactory | ||
) { | ||
$this->valueFactory = $valueFactory; | ||
$this->storeConfigsDataProvider = $storeConfigsDataProvider; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) : Value { | ||
|
||
$storeCodes = $this->getStoreCodes($args); | ||
$storeConfigsData = $this->storeConfigsDataProvider->getStoreConfigsByStoreCodes($storeCodes); | ||
|
||
$result = function () use ($storeConfigsData) { | ||
return !empty($storeConfigsData) ? $storeConfigsData : []; | ||
}; | ||
|
||
return $this->valueFactory->create($result); | ||
} | ||
|
||
/** | ||
* Retrieve store codes | ||
* | ||
* @param array $args | ||
* @return array | ||
* @throws GraphQlInputException | ||
*/ | ||
private function getStoreCodes($args) : array | ||
{ | ||
if (isset($args['store_codes'])) { | ||
if (is_array($args['store_codes'])) { | ||
return $args['store_codes']; | ||
} | ||
throw new GraphQlInputException(__('"store codes should contain a valid array')); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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