anycable / graphql-anycable

A drop-in replacement for GraphQL ActionCable subscriptions. Works with AnyCable.
MIT License
112 stars 18 forks source link

Make Redis keys prefix configurable #35

Closed palkan closed 1 year ago

palkan commented 1 year ago

Currently, we have key prefixes hard-coded: https://github.com/anycable/graphql-anycable/blob/2f3217ec64bf4b67d89d5f2d1a46048ba6beb9b4/lib/graphql/subscriptions/anycable_subscriptions.rb#L59-L62

It's fine for in most cases, but there are situations when we want a bit of flexibility. For example, when we use the same Redis with different app instances (e.g., staging or preview apps) or when we want to safely cleanup the old state (e.g., due to improper TTL settings).

Let's make the prefix (graphql-) configurable (we can make it a part of the Config).

Also, for the migration scenario, it might be useful to support two namespaces at the same time. We don't need to implement it in the library, but we can make it easier for the end user to do it themselves. Let's make sure that safely overriding the Config field in-place is taken into account internally:

GraphQL::AnyCable::Config.prepend(Module.new do
  def redis_prefix
    Thread.current[:graphql_anycable_redis_prefix] || super
  end
end)

Then, in the application code, if I want to switch the namespace, I just provide the thread var (and reset it after I'm done); that's the responsibility of the application developer; the code snippet above is just an example.