graphql-kotlin
graphql-kotlin copied to clipboard
Add support for @cacheControl static hints
Problem Description
When using graphql-kotlin in a federated-graph scenario with server-side caching via Apollo, the Apollo federation layer makes its caching decisions based on the cache-control
HTTP header sent with the subgraph's responses participating in a query. This is described in detail in the Apollo Federation docs, details on the expected http header can be found in the Apollo Server docs.
Currently, @cacheControl
annotations aren't supported with this version of graphql-kotlin. Also, even when adding these as custom annotations, the required response headers with the correctly calculated max age aren't calculated/added to the response. Thus, using this library for subgraphs participating in a federated graph scenario with caching functionality isn't supported.
Additional Context
While support for calculating the caching TTL was added in federation-jvm with PR 133, it is stated that setting the cache-control
header in the response is the subgraph service owner's responsibility:
Apollo Gateway looks for a cache-control policy in a subgraph's HTTP headers, so it's up to the subgraph service owner to pluck the value off the extensions and add it as a response header.
Workaround A current workaround for this issue is to:
- Implement
@cacheControl
annotations as custom directives. - Implement a Spring WebFilter that intercepts the server's response and sets the
cache-control
header (as described in the graphql-kotlin docs).
This would allow the federation layer to perform caching based on the subgraph's response header, however this requires lots of manual code and adds a time penalty to each graphQL call as every response needs to be intercepted and the cache TTL calculated manually.
Proposed Solution
It would be lovely if native support for static @cacheControl hints would be added that makes use of the caching TTL calculation provided by https://github.com/apollographql/federation-jvm and added the cache-control
header to the server's response, including its calculated TTL as well as the scope (private|public).
Additional Information
Netflix's DGS framework is about to add support for this behaviour with an upcoming release.