Cached templates
Use cached templates in Optimizely Graph to reduce GraphQL query latency and improve response times.
NoteCached templates are enabled by default for queries created on or after January 1, 2025.
Cached templates reduce request latency for GraphQL queries by speeding up query execution and improving response times. Use cached templates when you want frequently used queries to run faster without reprocessing the query structure each time.
A cached template is a stored, pre-translated GraphQL query with variable placeholders that Optimizely Graph reuses instead of reprocessing the query from scratch.
Prerequisites
Before you enable cached templates, confirm you have the following:
- A working Optimizely Graph endpoint.
- A valid auth token to send requests.
How cached templates work
In traditional GraphQL processing, the system translates each query into backend service requests, which can be slow and resource-intensive. Cached templates store the translated query with variable placeholders. The system then substitutes new variable values into the cached template instead of reprocessing the query structure. This reduces processing overhead and improves response times, even when variable values change.
Comparison of cached templates and cached queries
Cached queries store the full result of a specific query and its parameters, while cached templates store only the query structure with placeholders. Both cached templates and cached queries improve performance for frequently used queries, but they work differently.
- Cached templates – Use cached templates when query parameters change frequently and you need flexibility. Cached templates store the query structure with placeholders, which lets the system substitute different variable values efficiently when query inputs change.
- Cached queries – Use cached queries when you frequently execute queries with the same data and parameters. Cached queries store the entire query, including its results, which reduces server load and improves execution speed.
Limitations
Cached templates support the following variable types:
BooleanDateDateTimeStringIntFloatLocales(variable name must belocale)[Locales](variable name must belocale)
NoteWhen the system detects a query with unsupported variable types, it automatically switches from cached templates to cached queries so the query still runs efficiently.
Enable cached templates
To enable cached templates, include the following parameters in the Optimizely Graph request:
- Add the query string parameter
stored=trueto the request URL.https://cg.optimizely.com/content/v2?auth=123456789&stored=true - Add the request header with the key
cg-stored-queryand the valuetemplate.
The following code example shows a request that uses cached templates:
-H "Content-Type: application/json" \
-H "cg-stored-query: template" \
-d '{
"query": "query GetItem($id: ID!) { item(id: $id) { name, description } }",
"variables": {
"id": "12345"
}
}'Updated 9 days ago
