Skip to main content

graphql-proxy-cache

Caches GraphQL responses.

Category TrafficAdd it to Route, Service, Consumer, Consumer group, Global rule, Plugin configEngine plugin graphql-proxy-cachePriority 1009

What it does​

graphql-proxy-cache keeps a copy of the answers a route's GraphQL backend gives. The next identical query is served from that copy, without asking the backend. Cache strategy chooses where the copies live, on disk or in memory, and Cache zone names the zone that holds them.

The first POST of a query reaches the backend, and its answer carries the cache status MISS. The same query again is answered from the cache with HIT, for as long as the copy lives.

When to use it​

  • A GraphQL orders API whose catalogue queries every client sends and that changes rarely, served from memory for a minute.
  • A burst of identical queries, for example after a mobile app release, that the backend should not answer one by one.
  • A slow query whose answer may be a few minutes old, computed once and then served from the cache.
  • Answers that differ by caller, kept apart per consumer with Consumer isolation.

Not for caching REST answers by path and query string — see proxy-cache (Caches upstream responses on disk or in memory).

Set it up​

Keep GraphQL responses in memory for one minute​

Set Cache strategy to memory, set Cache TTL to 60 seconds and set Cache zone to memory_cache.

graphql-proxy-cache · as the console saves it
{
"cache_strategy": "memory",
"cache_zone": "memory_cache",
"cache_ttl": 60
}

Things to know​

  • Cache strategy — With memory, the zone memory_cache and a lifetime of 60 seconds, a second POST of { orders { id } } is answered from memory. The first answer carries MISS, the second HIT.
  • Cache strategy — Only a GET or a POST carrying a GraphQL query is cached, and only a 200 answer. A mutation reaches the backend every time, and other methods are refused with 405.
  • Cache strategy — Under Disk the lifetime of a stored response is set by the gateway's own cache configuration and not by this object, which is why the lifetime field is drawn under Memory alone.
  • Cache zone — Memory over a disk zone never caches, and Disk over a memory zone answers 500, so pick the zone that matches the strategy.
  • Cache zone — A zone the gateway configuration does not declare is refused when you save.
  • Cache zone — An empty zone name is refused when you save.
  • Cache TTL — A lifetime of 0 is refused when you save.
  • Consumer isolation — Off, the answer cached for the first caller is served to every other caller.
  • AdviceCache only answers that every caller of the route may see. For an answer that depends on who asks, keep Consumer isolation on.

Settings​

The fields as the console shows them, section by section. A field under “When … is …” is there only while that value is set.

Cache strategycache_strategystringdefault disk

Where answers are kept: on disk, or in memory with a lifetime of your own. Memory also needs the memory_cache zone below. Only a GET or a POST carrying a GraphQL query is cached, and only a 200 answer. A mutation is fetched again every time, and other methods are refused with 405.

one of disk, memory

When Cache strategy is memory​

Cache TTLcache_ttlintegerdefault 300

How long a stored answer is served before the upstream is asked for it again.

at least 1 · in seconds
Cache set cookiecache_set_cookiebooleandefault false

Off, an answer that carries a Set-Cookie header is never stored, wherever that header came from.

Cache zonecache_zonestringdefault disk_cache_one

The cache zone: disk_cache_one for Disk, memory_cache for Memory. A name the gateway configuration does not declare is refused.

1 to 100 characters
Consumer isolationconsumer_isolationbooleandefault true

On, each signed-in consumer gets only answers cached for them. Anonymous callers all share one entry.

Every plugin also has Enabled, Priority, Filter, Error response and Pre-function: see Plugin execution settings.

G8 dev · Apache APISIX® 3.19.0
Apache, Apache APISIX and APISIX are trademarks of The Apache Software Foundation.
Facts
Category
Traffic
Add it to
Route, Service, Consumer, Consumer group, Global rule, Plugin config
Engine plugin
graphql-proxy-cache
Priority
1009