Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Update identity-class.md #5567

Merged
merged 3 commits into from
Oct 14, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions guides/v2.3/graphql/develop/identity-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ If you create a cacheable query (similar to those for product, category, and CMS

An Identity class implements `Magento\Framework\GraphQl\Query\Resolver\IdentityInterface`. Your Identity class must contain the following elements:

* A private string that specifies the path to the class that defines the module’s cache tag.
* Choose a cache tag prefix for the entity.

* Your implementation of the `getIdentities(array $resolvedData)` method. Generally, this method takes an array of query results and creates a new cache tag for each entity based on the original string and the unique identifier for each item to be cached. For example, the getIdentities method for the `CatalogGraphQl` component appends the product ID to the `cat_p` cache tag, such as `cat_p_1`, `cat_p_2`, and so on.
* Your implementation of the `getIdentities(array $resolvedData)` method. The method maps the array of enties data to an array of cache tags, one for each entity.
Generally, this method takes an array of query results and creates a cache tag for each entity based on the original string and the unique identifier for each item to be cached. For example, the getIdentities method for the `CatalogGraphQl` component appends the product ID to the `cat_p` cache tag prefix, such as `cat_p_1`, `cat_p_2`, and so on.
Usually the method also adds the cache tag without an appended ID to the result array, so all cache records can be removed at once, and not only cache records for specific entities.

Use following example as the basis for your custom `Identity` class:

Expand All @@ -19,20 +21,20 @@ declare(strict_types=1);

namespace PathToModule\Model\Resolver\MyModule;

use Magento\Framework\GraphQl\Query\IdentityResolverInterface;
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;

/**
* Get identities from resolved data
*/
class MyIdentity implements IdentityInterface
{
private $cacheTag = \Path\To\Model\Class::CACHE_TAG;
private $cacheTag = \PathToModule\Model\MyEntity::CACHE_TAG;

/**
* Get identity tags from resolved data
*
* @param array $resolvedData
* @return int[]|string[]
* @return string[]
*/
public function getIdentities(array $resolvedData): array
{
Expand All @@ -42,7 +44,7 @@ class MyIdentity implements IdentityInterface
$ids[] = sprintf('%s_%s', $this->cacheTag, $item['entity_id']);
}
if (!empty($ids)) {
array_unshift($ids, $this->cacheTag);
$ids[] = $this->cacheTag;
}
return $ids;
}
Expand Down