A (mostly) drop-in replacement for default ActionCable subscriptions adapter shipped with graphql gem but works with AnyCable!
AnyCable is fast because it does not execute any Ruby code. But default subscription implementation shipped with graphql gem requires to do exactly that: re-evaluate GraphQL queries in ActionCable process. AnyCable doesn't support this (it's possible but hard to implement).
See https://github.com/anycable/anycable-rails/issues/40 for more details and discussion.
- Subscription information is stored in Redis database configured to be used by AnyCable. Expiration or data cleanup should be configured separately (see below).
- GraphQL queries for all subscriptions are re-executed in the process that triggers event (it may be web server, async jobs, rake tasks or whatever)
- Should work with ActionCable in development
- Should work without Rails via LiteCable
AnyCable must be configured with redis broadcast adapter (this is default).
Add this line to your application's Gemfile:
gem 'graphql-anycable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install graphql-anycable
-
Plug it into the schema (replace from ActionCable adapter if you have one):
class MySchema < GraphQL::Schema use GraphQL::Subscriptions::AnyCableSubscriptions subscription SubscriptionType end
-
Execute query in ActionCable/LiteCable channel.
class GraphqlChannel < ApplicationCable::Channel def execute(data) result = MySchema.execute( query: data["query"], context: context, variables: Hash(data["variables"]), operation_name: data["operationName"], ) transmit( result: result.subscription? ? { data: nil } : result.to_h, more: result.subscription?, ) end def unsubscribed channel_id = params.fetch("channelId") MySchema.subscriptions.delete_channel_subscriptions(channel_id) end private def context { account_id: account&.id, channel: self, } end end
Make sure that you're passing channel instance as
channel
key to the context. -
Trigger events as usual:
MySchema.subscriptions.trigger(:product_updated, {}, Product.first!, scope: account.id)
To avoid filling Redis storage with stale subscription data:
- Set
GRAPHQL_ANYCABLE_SUBSCRIPTION_EXPIRATION_SECONDS
environment variable to number of seconds (e.g.604800
for 1 week). See anyway_config documentation to other ways of configuring this gem. - Execute
rake graphql:anycable:clean_expired_subscriptions
once in a while to clean up stale subscription data
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/Envek/graphql-anycable.
The gem is available as open source under the terms of the MIT License.