Skip to content

Commit 761c80e

Browse files
committed
docs: add README
1 parent 60d4fdd commit 761c80e

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# graycore/magento2-graph-ql-cache
2+
3+
The Graycore GraphQL Cache module provides a structured mechanism for managing and applying cache lifetimes (TTLs) for Magento 2 GraphQL queries. It allows resolvers to contribute dynamic TTL values based on business logic, such as future dates, promotions, or entity validity. These TTLs are then automatically applied to the response as cache-control headers, enabling full-page caching and CDN integration for GraphQL endpoints.
4+
5+
<div align="center">
6+
7+
[![Packagist Downloads](https://img.shields.io/packagist/dm/graycore/magento2-graph-ql-cache?color=blue)](https://packagist.org/packages/graycore/magento2-graph-ql-cache/stats)
8+
[![Packagist Version](https://img.shields.io/packagist/v/graycore/magento2-graph-ql-cache?color=blue)](https://packagist.org/packages/graycore/magento2-graph-ql-cache)
9+
[![Packagist License](https://img.shields.io/packagist/l/graycore/magento2-graph-ql-cache)](https://github.com/graycoreio/magento2-graph-ql-cache/blob/main/LICENSE)
10+
11+
</div>
12+
13+
## Use Case
14+
15+
This module is ideal for Magento stores that want to:
16+
17+
- Dynamically cache GraphQL queries based on data freshness
18+
- Improve GraphQL performance with edge caching/CDNs
19+
- Respect TTLs for time-sensitive data (e.g. flash sales, scheduled promotions, expiring inventory)
20+
21+
## Key Features
22+
23+
- Resolver-Driven TTL Aggregation
24+
Resolver code can dynamically register TTLs using ResolverTtlStoreInterface, either with explicit seconds or based on future dates (with support for start-of-day and end-of-day cutoffs).
25+
26+
- Automatic Cache Header Population
27+
This plugin will intercept GraphQL response rendering and applies the minimum TTL collected during the query lifecycle, enabling fine-grained cache-control behavior for GraphQL results.
28+
29+
- Supports Flexible Input
30+
TTL input can be an integer, a DateTimeImmutable object, or a string (auto-parsed using the current store’s timezone).
31+
32+
- Integration with Magento’s GraphQlCache system
33+
Honors and respects Magento's existing CacheableQuery mechanisms, enhancing them with automatic TTL computation.
34+
35+
## Install
36+
37+
```bash
38+
composer require graycore/magento2-graph-ql-cache
39+
bin/magento module:enable Graycore_GraphQlCache
40+
bin/magento setup:upgrade
41+
```
42+
43+
## Usage
44+
45+
The primary mechanism that you interact with after installation is the `Graycore\GraphQlCache\Api\ResolverTtlStoreInterface`.
46+
47+
```php
48+
declare(strict_types=1);
49+
50+
namespace Vendor\Module\Model\Resolver;
51+
52+
use Graycore\GraphQlCache\Api\ResolverTtlStoreInterface;
53+
use Magento\Framework\GraphQl\Config\Element\Field;
54+
use Magento\Framework\GraphQl\Query\ResolverInterface;
55+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
56+
57+
class VendorResolver implements ResolverInterface
58+
{
59+
60+
public function __construct(
61+
private ResolverTtlStoreInterface $ttlStore
62+
) {}
63+
64+
/**
65+
* @inheritDoc
66+
*/
67+
public function resolve(
68+
Field $field,
69+
$context,
70+
ResolveInfo $info,
71+
array $value = null,
72+
array $args = null
73+
) {
74+
$this->ttl->add(300); // raw seconds
75+
$this->ttl->addForDate('2025-08-01T12:00:00'); // string or DateTimeImmutable
76+
$this->ttl->addForStartDate($date); // uses midnight of the provided date
77+
$this->ttl->addForEndDate($date); // uses 23:59:59 of the provided date
78+
79+
return $value;
80+
}
81+
}
82+
```
83+
84+
85+
## Upgrading
86+
87+
* [Semver Policy](https://semver.org/)

0 commit comments

Comments
 (0)