Partial Caching

By default Grafbase caches an entire response based on a cache key that's formed using the entire request. The experimental partial caching feature enables more granular caching - caching only some fields in the response, or even applying multiple different sets of cache configurations into a single request/response.

This can be enabled via the experimental schema configuration

export default config({ graph: g, experimental: { partialCaching: true, }, })

This should be combined with cache setting parameters, as documented in the cache config documentation. For the rest of this document we'll be using the following settings:

import { config, graph } from '@grafbase/sdk' const g = graph.Standalone() g.query('recommendations', { resolver: 'fetchRecommendations', returns: g.ref('Recommendations').list(), cache: { maxAge: 360, }, }) g.query('deals', { resolver: 'fetchRecommendations', returns: g.ref('Deal').list(), }) export default config({ graph: g, experimental: { partialCaching: true, }, })

When a query is received to this graph, the query will be split behind the scenes into multiple subqueries based on the fields that are present in the original query, and the cache settings that are applied to those fields.

For example here is a query based on the above schema, and the chunks that it will be split into:

query { recommendations { name price score } deals { name price } }

This feature is still experimental and being actively worked on, so there's some limitations and edge cases that it doesn't handle yet:

  1. Currently @defer is only supported on the edge runtime, and not on the Node.js runtime.
  2. We don't currently support cache purging for these cache entries.
  3. Stale while revalidate support is not there yet.
  4. Caching parts of items inside lists will behave badly if the order of the list in the cache and the order returned from the API does not match. For now we'd recommend caching all or none of list items.
Was this page helpful?