Skip to content

Commit 4a41ec4

Browse files
authored
Merge pull request #8179 from magento-arcticfoxes/B2B-2256
B2B-2256: "countries" and "country" GraphQl query has no cache identity
2 parents aab3ee6 + 2190290 commit 4a41ec4

File tree

8 files changed

+1511
-73
lines changed

8 files changed

+1511
-73
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Config;
9+
10+
use Magento\DirectoryGraphQl\Model\Resolver\Country\Identity;
11+
use Magento\Framework\App\Config\ValueInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Store\Model\Config\Cache\Tag\Strategy\TagGeneratorInterface;
15+
16+
/**
17+
* Generator that generates cache tags for country configuration
18+
*/
19+
class CountryTagGenerator implements TagGeneratorInterface
20+
{
21+
/**
22+
* @var string[]
23+
*/
24+
private $countryConfigPaths = [
25+
'general/locale/code',
26+
'general/country/allow'
27+
];
28+
29+
/**
30+
* @var StoreManagerInterface
31+
*/
32+
private $storeManager;
33+
34+
/**
35+
* @param StoreManagerInterface $storeManager
36+
*/
37+
public function __construct(
38+
StoreManagerInterface $storeManager
39+
) {
40+
$this->storeManager = $storeManager;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function generateTags(ValueInterface $config): array
47+
{
48+
if (in_array($config->getPath(), $this->countryConfigPaths)) {
49+
if ($config->getScope() == ScopeInterface::SCOPE_WEBSITES) {
50+
$website = $this->storeManager->getWebsite($config->getScopeId());
51+
$storeIds = $website->getStoreIds();
52+
} elseif ($config->getScope() == ScopeInterface::SCOPE_STORES) {
53+
$storeIds = [$config->getScopeId()];
54+
} else {
55+
$storeIds = array_keys($this->storeManager->getStores());
56+
}
57+
$tags = [];
58+
foreach ($storeIds as $storeId) {
59+
$tags[] = sprintf('%s_%s', Identity::CACHE_TAG, $storeId);
60+
}
61+
return $tags;
62+
}
63+
return [];
64+
}
65+
}
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+
}

app/code/Magento/DirectoryGraphQl/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<item name="currency_tag_generator" xsi:type="object">
1313
Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\Config\CurrencyTagGenerator
1414
</item>
15+
<item name="country_tag_generator" xsi:type="object">
16+
Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\Config\CountryTagGenerator
17+
</item>
1518
</argument>
1619
</arguments>
1720
</type>

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(cacheIdentity: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Currency\\Identity")
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 {

0 commit comments

Comments
 (0)