Skip to content

Commit 983dbb7

Browse files
committed
B2B-2258: Add caching capability to the storeConfig GraphQl query
1 parent 73a8ee9 commit 983dbb7

File tree

4 files changed

+155
-150
lines changed

4 files changed

+155
-150
lines changed

app/code/Magento/StoreGraphQl/Model/Resolver/Store/ConfigIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ConfigIdentity implements IdentityInterface
1414
/**
1515
* @var string
1616
*/
17-
const CACHE_TAG = 'gql_store_config';
17+
public const CACHE_TAG = 'gql_store_config';
1818

1919
/**
2020
* @inheritDoc

app/code/Magento/StoreGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright © Magento, Inc. All rights reserved.
22
# See COPYING.txt for license details.
33
type Query {
4-
storeConfig : StoreConfig @resolver(class: "Magento\\StoreGraphQl\\Model\\Resolver\\StoreConfigResolver") @doc(description: "Return details about the store's configuration.") @cache(cacheIdentity: "Magento\\StoreGraphQl\\Model\\Resolver\\Store\\Identity")
4+
storeConfig : StoreConfig @resolver(class: "Magento\\StoreGraphQl\\Model\\Resolver\\StoreConfigResolver") @doc(description: "Return details about the store's configuration.") @cache(cacheIdentity: "Magento\\StoreGraphQl\\Model\\Resolver\\Store\\ConfigIdentity")
55
availableStores(
66
useCurrentGroup: Boolean @doc(description: "Filter store views by the current store group.")
77
): [StoreConfig] @resolver(class: "Magento\\StoreGraphQl\\Model\\Resolver\\AvailableStoresResolver") @doc(description: "Get a list of available store views and their config information.")

dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiConfigFixture.php

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
namespace Magento\TestFramework\Annotation;
99

10-
use Magento\Config\Model\ResourceModel\Config as ConfigResource;
10+
use Magento\Config\Model\Config\Factory as ConfigFactory;
1111
use Magento\Framework\App\Config\MutableScopeConfigInterface;
1212
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Store\Api\StoreRepositoryInterface;
14+
use Magento\Store\Api\WebsiteRepositoryInterface;
1315
use Magento\Store\Model\ScopeInterface;
14-
use Magento\Store\Model\StoreManagerInterface;
1516
use Magento\TestFramework\App\ApiMutableScopeConfig;
1617
use Magento\TestFramework\Config\Model\ConfigStorage;
1718
use Magento\TestFramework\Helper\Bootstrap;
@@ -22,11 +23,11 @@
2223
class ApiConfigFixture extends ConfigFixture
2324
{
2425
/**
25-
* Values need to be deleted form the database
26+
* Values are inherited
2627
*
2728
* @var array
2829
*/
29-
private $valuesToDeleteFromDatabase = [];
30+
private $valuesNotFromDatabase = [];
3031

3132
/**
3233
* @inheritdoc
@@ -40,7 +41,7 @@ protected function setStoreConfigValue(array $matches, $configPathAndValue): voi
4041
/** @var ConfigStorage $configStorage */
4142
$configStorage = Bootstrap::getObjectManager()->get(ConfigStorage::class);
4243
if (!$configStorage->checkIsRecordExist($configPath, ScopeInterface::SCOPE_STORES, $storeCode)) {
43-
$this->valuesToDeleteFromDatabase[$storeCode][$configPath ?? ''] = $requiredValue ?? '';
44+
$this->valuesNotFromDatabase[$storeCode][$configPath ?? ''] = $requiredValue ?? '';
4445
}
4546

4647
parent::setStoreConfigValue($matches, $configPathAndValue);
@@ -56,7 +57,7 @@ protected function setGlobalConfigValue($configPathAndValue): void
5657
/** @var ConfigStorage $configStorage */
5758
$configStorage = Bootstrap::getObjectManager()->get(ConfigStorage::class);
5859
if (!$configStorage->checkIsRecordExist($configPath)) {
59-
$this->valuesToDeleteFromDatabase['global'][$configPath] = $requiredValue;
60+
$this->valuesNotFromDatabase['global'][$configPath] = $requiredValue;
6061
}
6162

6263
$originalValue = $this->getScopeConfigValue($configPath, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
@@ -76,7 +77,7 @@ protected function setWebsiteConfigValue(array $matches, $configPathAndValue): v
7677
/** @var ConfigStorage $configStorage */
7778
$configStorage = Bootstrap::getObjectManager()->get(ConfigStorage::class);
7879
if (!$configStorage->checkIsRecordExist($configPath, ScopeInterface::SCOPE_WEBSITES, $websiteCode)) {
79-
$this->valuesToDeleteFromDatabase[$websiteCode][$configPath ?? ''] = $requiredValue ?? '';
80+
$this->valuesNotFromDatabase[$websiteCode][$configPath ?? ''] = $requiredValue ?? '';
8081
}
8182

8283
parent::setWebsiteConfigValue($matches, $configPathAndValue);
@@ -88,12 +89,15 @@ protected function setWebsiteConfigValue(array $matches, $configPathAndValue): v
8889
*/
8990
protected function _restoreConfigData()
9091
{
91-
/** @var ConfigResource $configResource */
92-
$configResource = Bootstrap::getObjectManager()->get(ConfigResource::class);
9392
/* Restore global values */
9493
foreach ($this->globalConfigValues as $configPath => $originalValue) {
95-
if (isset($this->valuesToDeleteFromDatabase['global'][$configPath])) {
96-
$configResource->deleteConfig($configPath);
94+
if (isset($this->valuesNotFromDatabase['global'][$configPath])) {
95+
$this->inheritConfig(
96+
$configPath,
97+
$originalValue,
98+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
99+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
100+
);
97101
} else {
98102
$this->_setConfigValue($configPath, $originalValue);
99103
}
@@ -103,9 +107,13 @@ protected function _restoreConfigData()
103107
foreach ($this->storeConfigValues as $storeCode => $originalData) {
104108
foreach ($originalData as $configPath => $originalValue) {
105109
$storeCode = $storeCode ?: null;
106-
if (isset($this->valuesToDeleteFromDatabase[$storeCode][$configPath])) {
107-
$scopeId = $this->getIdByScopeType(ScopeInterface::SCOPE_STORES, $storeCode);
108-
$configResource->deleteConfig($configPath, ScopeInterface::SCOPE_STORES, $scopeId);
110+
if (isset($this->valuesNotFromDatabase[$storeCode][$configPath])) {
111+
$this->inheritConfig(
112+
$configPath,
113+
(string)$originalValue,
114+
ScopeInterface::SCOPE_STORES,
115+
$storeCode
116+
);
109117
} else {
110118
$this->setScopeConfigValue(
111119
$configPath,
@@ -121,9 +129,13 @@ protected function _restoreConfigData()
121129
foreach ($this->websiteConfigValues as $websiteCode => $originalData) {
122130
foreach ($originalData as $configPath => $originalValue) {
123131
$websiteCode = $websiteCode ?: null;
124-
if (isset($this->valuesToDeleteFromDatabase[$websiteCode][$configPath])) {
125-
$scopeId = $this->getIdByScopeType(ScopeInterface::SCOPE_WEBSITES, $websiteCode);
126-
$configResource->deleteConfig($configPath, ScopeInterface::SCOPE_WEBSITES, $scopeId);
132+
if (isset($this->valuesNotFromDatabase[$websiteCode][$configPath])) {
133+
$this->inheritConfig(
134+
$configPath,
135+
$originalValue,
136+
ScopeInterface::SCOPE_WEBSITES,
137+
$websiteCode
138+
);
127139
} else {
128140
$this->setScopeConfigValue(
129141
$configPath,
@@ -159,28 +171,47 @@ protected function getScopeConfigValue(string $configPath, string $scopeType, st
159171
}
160172

161173
/**
162-
* Get id by code
174+
* Inherit the config and remove the config from database
163175
*
176+
* @param string $path
177+
* @param string $value
164178
* @param string $scopeType
165-
* @param string|null $scopeId
166-
* @return int
179+
* @param string|null $scopeCode
180+
* @return void
167181
*/
168-
private function getIdByScopeType(string $scopeType, ?string $scopeId): int
169-
{
170-
$id = 0;
171-
/** @var StoreManagerInterface $storeManager */
172-
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
173-
switch ($scopeType) {
174-
case ScopeInterface::SCOPE_WEBSITES:
175-
$id = (int)$storeManager->getWebsite($scopeId)->getId();
176-
break;
177-
case ScopeInterface::SCOPE_STORES:
178-
$id = (int)$storeManager->getStore($scopeId)->getId();
179-
break;
180-
default:
181-
break;
182+
private function inheritConfig(
183+
string $path,
184+
?string $value,
185+
string $scopeType,
186+
?string $scopeCode
187+
) {
188+
$pathParts = explode('/', $path);
189+
$store = 0;
190+
$configData = [
191+
'section' => $pathParts[0],
192+
'website' => '',
193+
'store' => $store,
194+
'groups' => [
195+
$pathParts[1] => [
196+
'fields' => [
197+
$pathParts[2] => [
198+
'value' => $value,
199+
'inherit' => 1
200+
]
201+
]
202+
]
203+
]
204+
];
205+
$objectManager = Bootstrap::getObjectManager();
206+
if ($scopeType === ScopeInterface::SCOPE_STORE && $scopeCode !== null) {
207+
$store = $objectManager->get(StoreRepositoryInterface::class)->get($scopeCode)->getId();
208+
$configData['store'] = $store;
209+
} elseif ($scopeType === ScopeInterface::SCOPE_WEBSITES && $scopeCode !== null) {
210+
$website = $objectManager->get(WebsiteRepositoryInterface::class)->get($scopeCode)->getId();
211+
$configData['store'] = '';
212+
$configData['website'] = $website;
182213
}
183214

184-
return $id;
215+
$objectManager->get(ConfigFactory::class)->create(['data' => $configData])->save();
185216
}
186217
}

0 commit comments

Comments
 (0)