PHP code example of graycore / magento2-graph-ql-cache

1. Go to this page and download the library: Download graycore/magento2-graph-ql-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

graycore / magento2-graph-ql-cache example snippets


declare(strict_types=1);

namespace Vendor\Module\Model\Resolver;

use Graycore\GraphQlCache\Api\ResolverTtlStoreInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class VendorResolver implements ResolverInterface
{

    public function __construct(
        private ResolverTtlStoreInterface $ttlStore
    ) {}

    /**
     * @inheritDoc
     */
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $this->ttl->add(300); // raw seconds
        $this->ttl->addForDate('2025-08-01T12:00:00'); // string or DateTimeImmutable
        $this->ttl->addForStartDate($date); // uses midnight of the provided date
        $this->ttl->addForEndDate($date);   // uses 23:59:59 of the provided date

        return $value;
    }
}