How to use CacheControl in DgsDataFetchingEnvironment #821
-
EDIT: My understanding was wrong, and many of my statements are wrong. Please see marked answer for useful information. I'm trying to implement caching in a federated GraphQL API. I'm using Apollo Gateway for the federation server, and DGS for the subgraph APIs. Apollo's documentation shows a @CacheControl directive that is supposed to add caching information to the extensions field in the GraphQL response. There is a Java GraphQL class graphql.cachecontrol.CacheControl which handles the logic of adding the extension, and I see that DgsDataFetchingEnvironment has an instance of the CacheControl in it via getCacheControl(). However, I do not think the caching hints I add to CacheControl object are not passed to the response by default. I believe my options are to extend/modify the DGSRestController or the DefaultDgsQueryExecutor so that I can call the CacheControl method addTo(ExecutionResult result) on the instance of the ExecutionResult before it's returned. I feel like DefaultDgsQueryExecutor is the more direct approach, but it's not "open" so cannot be extended. I attempting to work around this by implementing my own DgsQueryExecutor that has an instance of DefaultDgsQueryExecutor that it uses, but it seems like a lot of work to add to the ExecutionResult. I am nearly certain I'm missing an easier way to add the cacheControl extension. Does anyone have any ideas? If there is not an easier way, does it make sense to rquest a new feature that DefaultDgsQueryExecutor is open? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
@jwalton922 - I've recently spent time diving into this rabbit hole. The current Now for some good news, there is a open PR for Within the PR comments you'll see @berngp has been thinking about mechanisms which DGS could leverage this |
Beta Was this translation helpful? Give feedback.
-
Thank you for the super fast response! I had several flaws in my understanding that those links helped clear up, and I will eagerly await for that PR to be merged and released. |
Beta Was this translation helpful? Give feedback.
-
@jwalton922 did you soled this issue? i mean how did you implemented @CacheControl in the dgs project which gives cache-control in the response headers? |
Beta Was this translation helpful? Give feedback.
-
I don't know if it can help, but to forward the @CacheControl directive that are indicated in our schema in the servlet http response headers we had to instrument our graphql queries with some Spring beans:
It feels like a hack waiting for something being standardised but it is "doing the job" on our side. |
Beta Was this translation helpful? Give feedback.
@jwalton922 - I've recently spent time diving into this rabbit hole.
The current
graphql-java
class graphql.cachecontrol.CacheControl was added many years ago and was based on the Apollo Cache Control Specification which has since been deprecated. My guess is that the aforementioned class ingraphql-java
should also be deprecated/removed. Note: this is different from the up-to-date Apollo server-side caching documentation. Some more details can be found hereNow for some good news, there is a open PR for
federation-jvm
which is very close to being merged and released, which adds in the samecacheControl
support that apollo-server (graphql-js) uses. 🎉Within the PR comments you'll see @berngp