Identity class

If you create a cacheable query (similar to those for product, category, and CMS data), then you must create an Identity class for the module. The class must return unique identifiers for cache tags that can be invalidated when an entity changes. Place this class in your module’s Model/Resolver directory.

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.

  • 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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
declare(strict_types=1);

namespace PathToModule\Model\Resolver\MyModule;

use Magento\Framework\GraphQl\Query\IdentityResolverInterface;

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

    /**
     * Get identity tags from resolved data
     *
     * @param array $resolvedData
     * @return int[]|string[]
     */
    public function getIdentities(array $resolvedData): array
    {
        $ids = [];
        $items = $resolvedData['items'] ?? [];
        foreach ($items as $item) {
            $ids[] = sprintf('%s_%s', $this->cacheTag, $item['entity_id']);
        }
        if (!empty($ids)) {
            array_unshift($ids, $this->cacheTag);
        }
        return $ids;
    }
}

Use the @cache directive in your module’s graphqls file to specify the location to your Identity class. Your module’s graphqls file must point to your Identity class, as shown below:

1
2
3
4
5
    category (
        id: Int @doc(description: "Id of the category")
    ): CategoryTree
    @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree") @doc(description: "The category query searches for categories that match the criteria specified in the search and filter attributes") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoryTreeIdentity")
}