Skip to content

Commit b98e623

Browse files
committed
B2B-2256: "countries" and "country" GraphQl query has no cache identity
1 parent 0727043 commit b98e623

File tree

6 files changed

+177
-2
lines changed

6 files changed

+177
-2
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy;
9+
10+
use Magento\DirectoryGraphQl\Model\Resolver\Country\Identity;
11+
use Magento\Framework\App\Cache\Tag\StrategyInterface;
12+
use Magento\Framework\App\Config\ValueInterface;
13+
use Magento\Store\Model\ScopeInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
16+
/**
17+
* Produce cache tags for country config.
18+
*/
19+
class CountryConfig implements StrategyInterface
20+
{
21+
/**
22+
* @var string[]
23+
*/
24+
private $countryConfigPaths = [
25+
'general/locale/code',
26+
'general/country/allow',
27+
'general/region/display_all',
28+
'general/region/state_required'
29+
];
30+
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private $storeManager;
35+
36+
/**
37+
* @param StoreManagerInterface $storeManager
38+
*/
39+
public function __construct(
40+
StoreManagerInterface $storeManager
41+
) {
42+
$this->storeManager = $storeManager;
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function getTags($object): array
49+
{
50+
if (!is_object($object)) {
51+
throw new \InvalidArgumentException('Provided argument is not an object');
52+
}
53+
54+
if ($object instanceof ValueInterface
55+
&& in_array($object->getPath(), $this->countryConfigPaths)
56+
&& $object->isValueChanged()
57+
) {
58+
if ($object->getScope() == ScopeInterface::SCOPE_WEBSITES) {
59+
$website = $this->storeManager->getWebsite($object->getScopeId());
60+
$storeIds = $website->getStoreIds();
61+
} elseif ($object->getScope() == ScopeInterface::SCOPE_STORES) {
62+
$storeIds = [$object->getScopeId()];
63+
} else {
64+
$storeIds = array_keys($this->storeManager->getStores());
65+
}
66+
$ids = [];
67+
foreach ($storeIds as $storeId) {
68+
$ids[] = sprintf('%s_%s', Identity::CACHE_TAG, $storeId);
69+
}
70+
return $ids;
71+
}
72+
73+
return [];
74+
}
75+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DirectoryGraphQl\Model\Resolver\Country;
9+
10+
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
class Identity implements IdentityInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
public const CACHE_TAG = 'gql_country';
19+
20+
/**
21+
* @var StoreManagerInterface
22+
*/
23+
private $storeManager;
24+
25+
/**
26+
* @param StoreManagerInterface $storeManager
27+
*/
28+
public function __construct(StoreManagerInterface $storeManager)
29+
{
30+
$this->storeManager = $storeManager;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function getIdentities(array $resolvedData): array
37+
{
38+
if (empty($resolvedData)) {
39+
return [];
40+
}
41+
$storeId = $this->storeManager->getStore()->getId();
42+
return [self::CACHE_TAG, sprintf('%s_%s', self::CACHE_TAG, $storeId)];
43+
}
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Framework\App\Cache\Tag\Strategy\Factory">
10+
<arguments>
11+
<argument name="customStrategies" xsi:type="array">
12+
<item name="Magento\Framework\App\Config\ValueInterface" xsi:type="object">
13+
Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\CountryConfig
14+
</item>
15+
</argument>
16+
</arguments>
17+
</type>
18+
</config>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
type Query {
55
currency: Currency @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Currency") @doc(description: "Return information about the store's currency.") @cache(cacheable: false)
6-
countries: [Country] @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Countries") @doc(description: "The countries query provides information for all countries.") @cache(cacheable: false)
7-
country (id: String): Country @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Country") @doc(description: "The countries query provides information for a single country.") @cache(cacheable: false)
6+
countries: [Country] @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Countries") @doc(description: "The countries query provides information for all countries.") @cache(cacheIdentity: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Country\\Identity")
7+
country (id: String): Country @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Country") @doc(description: "The countries query provides information for a single country.") @cache(cacheIdentity: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Country\\Identity")
88
}
99

1010
type Currency {

dev/tests/integration/framework/Magento/TestFramework/Config/Model/ConfigStorage.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,22 @@ private function normalizeScope(string $scope): string
120120

121121
return $scope;
122122
}
123+
124+
/**
125+
* Delete configuration from db
126+
*
127+
* @param string $path
128+
* @param string $scope
129+
* @param string|null $scopeCode
130+
* @return void
131+
*/
132+
public function deleteConfigFromDb(
133+
string $path,
134+
string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
135+
?string $scopeCode = null
136+
) {
137+
$scope = $this->normalizeScope($scope);
138+
$scopeId = $this->getIdByScope($scope, $scopeCode);
139+
$this->configResource->deleteConfig($path, $scope, $scopeId);
140+
}
123141
}

dev/tests/integration/testsuite/Magento/Store/_files/multiple_websites_with_store_groups_stores_rollback.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,38 @@
1616
if ($websiteId) {
1717
$website->delete();
1818
}
19+
20+
/** Delete the third website **/
1921
$website2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Website::class);
2022
/** @var $website \Magento\Store\Model\Website */
2123
$websiteId2 = $website2->load('third', 'code')->getId();
2224
if ($websiteId2) {
2325
$website2->delete();
2426
}
2527

28+
/** Delete the second store groups **/
29+
$group = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Group::class);
30+
/** @var $group \Magento\Store\Model\Group */
31+
$groupId = $group->load('second_store', 'code')->getId();
32+
if ($groupId) {
33+
$group->delete();
34+
}
35+
36+
/** Delete the third store groups **/
37+
$group2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Group::class);
38+
/** @var $group2 \Magento\Store\Model\Group */
39+
$groupId2 = $group2->load('third_store', 'code')->getId();
40+
if ($groupId2) {
41+
$group2->delete();
42+
}
43+
44+
/** Delete the second store **/
2645
$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
2746
if ($store->load('second_store_view', 'code')->getId()) {
2847
$store->delete();
2948
}
3049

50+
/** Delete the third store **/
3151
$store2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
3252
if ($store2->load('third_store_view', 'code')->getId()) {
3353
$store2->delete();

0 commit comments

Comments
 (0)