strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Investigate caching options and Apollo Cache Control

Open patrick91 opened this issue 5 years ago • 6 comments

https://github.com/apollographql/apollo-cache-control [deprecated] https://www.apollographql.com/docs/apollo-server/performance/caching/

Caching in GraphQL is not super easy, we might add some utilities for it :)

patrick91 avatar Apr 07 '19 22:04 patrick91

Hi @patrick91 , are there any plans to implement this? If there are no plans for implementation, do you know of any strategies for this? I imagine we have to set the cache-control headers before sending response back. Apollo-server does this by reading the @cacheControl directive. Is there a way for us to do the same using strawberry plugins? Maybe on the on_request_end hook? Can we read directives from this hook in execution_context? I might not be able to get a clean way of doing this, but if I can get it done even in a rough way I'll be happy to share it :)

@vamshiaruru-virgodesigns adding cache control directives is easy, doing the caching in an extension is more difficult at the moment.

We do have an example of validation cache here: https://strawberry.rocks/docs/extensions/validation-cache#validationcache this is not what you want, but it could be a start for an implementation :)

patrick91 avatar Aug 03 '22 11:08 patrick91

Hey I started digging into this... As a first step maybe we can provide the CacheControl directive?

I was thinking about an apollo namespace... but maybe there are better places :) https://github.com/strawberry-graphql/strawberry/compare/add-cache-control-directive?expand=1

estyxx avatar Aug 03 '22 16:08 estyxx

I think apollo as a namespace might be fine, but I'm not sure if adding the directive is enough, as it can only be used when using federation (or any gateway that knows about this directive)

FYI I'm doing some changes related to schema directives in #2047 (I think the only change that I've not pushed out is https://github.com/strawberry-graphql/strawberry/pull/2047/commits/b961b3dcb3849b7e0da1e16d89327ca4383b4db3) so your change should be ok

patrick91 avatar Aug 03 '22 16:08 patrick91

Just adding a directive isn't enough I think. I think we have to add proper cache control headers in the request. I might be wrong, but apollo server reads these directives and sets appropriate headers based on the directives, and federation reads values from the header instead of the directive.

Just adding a directive isn't enough I think. I think we have to add proper cache control headers in the request. I might be wrong, but apollo server reads these directives and sets appropriate headers based on the directives, and federation reads values from the header instead of the directive.

you're right, I didn't read the whole docs:

When using Apollo Federation, the @cacheControl directive and CacheControlScope enum may be defined in a subgraph's schema. An Apollo Server-based subgraph will calculate and set the cache hint for the response that it sends to the gateway as it would for a non-federated Apollo Server sending a response to a client. The gateway will then calculate the cache hint for the overall response based on the most restrictive settings among all of the responses received from the subgraphs involved in query plan execution.

https://www.apollographql.com/docs/apollo-server/performance/caching/#using-with-federation

patrick91 avatar Aug 03 '22 17:08 patrick91

Just going through old issues, I'll close this as not planned, we can discuss caching in another issue if we still want/need it 😊

patrick91 avatar Apr 06 '24 07:04 patrick91