A simple plugin for Hasura DDN server-side request caching.
The plugin can be run for local development using docker
:
$ docker build -t caching-plugin .
$ docker run -p 8787:8787 caching-plugin
Add the following configuration to one of your Hasura DDN subgraphs. The first block describes the "pre-parse" stage, which will return the cached response if it exists and the given query is marked for caching.
kind: LifecyclePluginHook
version: v1
definition:
pre: parse
name: cache_get_test
url:
value: http://localhost:8787
config:
request:
headers:
additional:
hasura-m-auth:
value: my-secret-header
hasura-plugin-phase:
value: pre-parse
rawRequest:
query: {}
variables: {}
The second block describes the "pre-response" stage, which will cache the response if it hasn't already been cached and if the given query is marked for caching.
kind: LifecyclePluginHook
version: v1
definition:
pre: response
name: cache_set_test
url:
value: http://localhost:8787
config:
request:
headers:
additional:
hasura-m-auth:
value: my-secret-header
hasura-plugin-phase:
value: pre-response
rawRequest:
query: {}
variables: {}
Configuration is found in src/config.js
. Currently, there are three available
keys:
headers: { "hasura-m-auth": "my-secret-header" }
Headers expected from the engine
when a request to the plugin is made.
Currently, only one header is expected: hasura-m-auth
is a user-specified
secret that the engine should send over in its requests.
queries_to_cache: [
"query { Artist { Name } }",
"query { Album { Artist { Name } } }",
];
When a user makes a request to the server, the caching plugin will parse the request into an AST, and then check to see whether the AST matches the AST of any of these queries. If it does, the query will be cached. This means that whitespace is unimportant in these queries, and you can write them over multiple lines.
A result will be cached for each set of session and query variables that use this same query template.
time_to_live: 600;
The length of time after which a given cache entry should be invalidated. This is measured in seconds.
redis_url: "redis://redis:6379";
otel_endpoint: "http://jaeger:4318/v1/traces";
otel_headers: {
Authorization: "pat <my-personal-access-token>";
}