router
router copied to clipboard
Caching hook with Rhai or Coprocessor
Is your feature request related to a problem? Please describe.
I have some operations that hit the Router that for one reason or another I never wanted cached. This includes all Router caches like query planning, APQ, and entity cache as well as the in-memory and Redis instances.
Today I do not have a way to tell the Router to NOT cache something before it does so I can influence what is stored.
Describe the solution you'd like
There are a couple ways we could solve this, but for the custom logic I would want to have the following input regardless:
- The full text of the operation body
- The cache key that Router will use
- The type of cache key that is being calculated
- (Optional) The cache location to be used [Redis, InMemory] if it's not hard to implement could be helpful
Calculate with existing hooks
We could add more data to be sent and returned from the existing hooks like SupergraphRequest, we would need to share the cache info required and then monitor for maybe some Context value that says DO NOT CACHE somehow.
Calculate with new hooks
We could add a new hook CacheRequest that gets called for all cache instances to help deterimine if the operation should be cached and it expects a specific response to indicate the should cache value, or maybe even redefine the cache key
YAML Config
Doing the should cache check could fallback to a few specific scenarios and so if we could open up YAML config to check for certain things that would be much faster than a coprocessor. Things like
- Cost above threshold
- Not in PQ list
- Hard coded variables
cache:
disabled_for:
no_pqs: true
cost_greater_than: 50
hard_coded_args: true
number_of_aliases: 4
Describe alternatives you've considered
We could use Persisted Queries and Safelisting to block the operations that I don't want, but that hard blocks the operations, when maybe I still want to allow them through or I cannot use PQs for my clients.
Additional context
This maybe come up if someone is looking to block clients from using hard coded arguments and variables. Rather than just blocking the operation we could not cache if for some reason you sent a query that hard coded the product ID in the query body.