Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: expose cache keys in resolvers/directives #3228

Closed
guoliu opened this issue Aug 29, 2019 · 9 comments
Closed

Feature Request: expose cache keys in resolvers/directives #3228

guoliu opened this issue Aug 29, 2019 · 9 comments
Labels
⛲️ feature New addition or enhancement to existing solutions

Comments

@guoliu
Copy link

guoliu commented Aug 29, 2019

I haven't find any current ways of doing this, but it should be extremely helpful for folks who need more control on caches like us.

We are implementing a cache invalidation mechanism, where we keep a mapping between object global id and the list of cache keys associate with it, then invalidate that list when needed.

However, there's currently no easy way of getting the cache key in resolvers or directives. Looks like the cache key can be add to request context after this line, so everything downstream can access it if needed.

Do you think that's a reasonable feature, and are there any unexpected side effects of adding keys to context?

@abernix abernix added the ⛲️ feature New addition or enhancement to existing solutions label Aug 30, 2019
@glasser
Copy link
Member

glasser commented Sep 4, 2019

That does seem like it would be a useful bit of data to expose. I haven't been personally using this feature (or working in the Apollo Server codebase at all) very heavily since my initial implementation, so definitely curious what @abernix and others who are more involved think.

@guoliu
Copy link
Author

guoliu commented Sep 6, 2019

Thank you for your response @glasser! Since we rely on this feature currently, we are using a customized plugin with cache key injected into context. I would love to submit a PR if @abernix and others think this is a useful feature to the community.

@BondDimy
Copy link

BondDimy commented Apr 1, 2020

Hello, any updates about this feature ? I've also faced with cache invalidation issue, could you help me, please ?

@guoliu
Copy link
Author

guoliu commented Apr 7, 2020

Hello, any updates about this feature ? I've also faced with cache invalidation issue, could you help me, please ?

@BondDimy I think the easiest way is to implement your own cache plugin that exposes the cache key, record the key in a directive, and invalidate them in another directive when needed. More discussions here: #2437 (comment)

@BondDimy
Copy link

BondDimy commented Apr 7, 2020

@guoliu Thanks too much for your reply, yep I thought about custom plugin, currently working on it

@nelsonpecora
Copy link

Does anyone have examples of plugins that do this?

@guoliu
Copy link
Author

guoliu commented Sep 16, 2020

Does anyone have examples of plugins that do this?

@nelsonpecora you can checkout this repo: https://github.com/thematters/apollo-response-cache

@nelsonpecora
Copy link

@guoliu ooh, this looks like exactly what we need, thanks!

@glasser
Copy link
Member

glasser commented Oct 20, 2022

I hope the other cache plugin helps!

One note is that the concept of "the cache key" for the current full response cache plugin doesn't quite make sense: when using the sessionId hook, the plugin tries two different cache keys (one with the session ID and one without it).

Since AS v3.10.0, the response cache plugin has a generateCacheKey option which takes in GraphQLRequestContext. So you could pass this option and give it the side effect of recording the generated cache key on your contextValue.

Because it seems like there are multiple ways to achieve this goal (and the goal itself isn't entirely well-defined) I'm going to close this issue. Sorry for the delay!

@glasser glasser closed this as completed Oct 20, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
⛲️ feature New addition or enhancement to existing solutions
Projects
None yet
Development

No branches or pull requests

5 participants