Passing GQL query context into cache key generation
natebeck opened this issue · 4 comments
Hey all!
Super awesome gem, digging into it a bit, I don't readily see any way to access the GQL query context when generating a cache key or resolver.
field :stats, SegmentStatsType, "Stats for this segment", null: false,
cache: { key: :guid, expiry: 6.hours.to_i }
I have one segment which is user specific that I'd like to use the current user's id as part of the cache key, and it's not readily apparent how to achieve this.
field :stats, SegmentStatsType, "Stats for this segment", null: false,
cache: { key: -> (obj) { obj.what_is_my_cache_key(context[:current_user] }, expiry: 6.hours.to_i }
Digging into the source code, I don't think this is a supported use case yet unless I'm just missing it. I'm happy to help implement something to support this use case but would love some insight on to how you would imagine this working.
Thanks again for all the work you've put into this thus far, and for open sourcing it.
Hey @natebeck thanks for reporting this! It probably would be a good idea to make the gql context available in the context of the cache key proc.
I can't get to adding support for this right away, @natebeck so if you're interested in opening a PR, you can take a look at https://github.com/stackshareio/graphql-cache/blob/master/lib/graphql/cache/fetcher.rb#L33
The simplest solution would be to add a parameter to GraphQL::Cache::Key#initialize
for the query context, and pass it in when creating the key in GraphQL::Cache::Fetcher#cached_resolve
.
Actually, nvm @natebeck I went ahead and pushed a PR with the code changes to pass the context into a key proc....however it's a breaking change for the previous API (existing procs will fail if they don't accept the second parameter). I'll circle back to try and find a way to do it without that limitation, but feel free to suggest approaches for that and/or commits here: #50
Thanks, I'll pull down the branch and take a look.