Skip to content

Commit

Permalink
Merge pull request #8179 from magento-arcticfoxes/B2B-2256
Browse files Browse the repository at this point in the history
B2B-2256: "countries" and "country" GraphQl query has no cache identity
  • Loading branch information
dhaecker authored Mar 4, 2023
2 parents aab3ee6 + 2190290 commit 4a41ec4
Show file tree
Hide file tree
Showing 8 changed files with 1,511 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\Config;

use Magento\DirectoryGraphQl\Model\Resolver\Country\Identity;
use Magento\Framework\App\Config\ValueInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\Config\Cache\Tag\Strategy\TagGeneratorInterface;

/**
* Generator that generates cache tags for country configuration
*/
class CountryTagGenerator implements TagGeneratorInterface
{
/**
* @var string[]
*/
private $countryConfigPaths = [
'general/locale/code',
'general/country/allow'
];

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param StoreManagerInterface $storeManager
*/
public function __construct(
StoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
}

/**
* @inheritdoc
*/
public function generateTags(ValueInterface $config): array
{
if (in_array($config->getPath(), $this->countryConfigPaths)) {
if ($config->getScope() == ScopeInterface::SCOPE_WEBSITES) {
$website = $this->storeManager->getWebsite($config->getScopeId());
$storeIds = $website->getStoreIds();
} elseif ($config->getScope() == ScopeInterface::SCOPE_STORES) {
$storeIds = [$config->getScopeId()];
} else {
$storeIds = array_keys($this->storeManager->getStores());
}
$tags = [];
foreach ($storeIds as $storeId) {
$tags[] = sprintf('%s_%s', Identity::CACHE_TAG, $storeId);
}
return $tags;
}
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\DirectoryGraphQl\Model\Resolver\Country;

use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
use Magento\Store\Model\StoreManagerInterface;

class Identity implements IdentityInterface
{
/**
* @var string
*/
public const CACHE_TAG = 'gql_country';

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param StoreManagerInterface $storeManager
*/
public function __construct(StoreManagerInterface $storeManager)
{
$this->storeManager = $storeManager;
}

/**
* @inheritdoc
*/
public function getIdentities(array $resolvedData): array
{
if (empty($resolvedData)) {
return [];
}
$storeId = $this->storeManager->getStore()->getId();
return [self::CACHE_TAG, sprintf('%s_%s', self::CACHE_TAG, $storeId)];
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/DirectoryGraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<item name="currency_tag_generator" xsi:type="object">
Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\Config\CurrencyTagGenerator
</item>
<item name="country_tag_generator" xsi:type="object">
Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\Config\CountryTagGenerator
</item>
</argument>
</arguments>
</type>
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/DirectoryGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

type Query {
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")
countries: [Country] @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Countries") @doc(description: "The countries query provides information for all countries.") @cache(cacheable: false)
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)
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")
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")
}

type Currency {
Expand Down
Loading

0 comments on commit 4a41ec4

Please sign in to comment.