GraphQL subscriptions for AnyCable
A (mostly) drop-in replacement for default ActionCable subscriptions adapter shipped with graphql gem but works with AnyCable!
Why?
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.
Differences
- 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)
Compatibility
- Should work with ActionCable in development
- Should work without Rails via LiteCable
Requirements
AnyCable must be configured with redis broadcast adapter (this is default).
Installation
Add this line to your application's Gemfile:
gem 'graphql-anycable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install graphql-anycable
Usage
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)
Operations
To avoid filling Redis storage with stale subscription data:
Set
subscription_expiration_seconds
setting to number of seconds (e.g.604800
for 1 week). See configuration section below for details.Execute
rake graphql:anycable:clean
once in a while to clean up stale subscription data.Heroku users should set up
use_redis_object_on_cleanup
setting tofalse
due to limitations in Heroku Redis.
Configuration
GraphQL-Anycable uses anyway_config to configure itself. There are several possibilities to configure this gem:
Environment variables:
GRAPHQL_ANYCABLE_SUBSCRIPTION_EXPIRATION_SECONDS=604800 GRAPHQL_ANYCABLE_USE_REDIS_OBJECT_ON_CLEANUP=true
YAML configuration files:
# config/graphql_anycable.yml production: subscription_expiration_seconds: 300 # 5 minutes use_redis_object_on_cleanup: false # For restricted redis installations
Configuration from your application code:
GraphQL::Anycable.configure do |config| config.subscription_expiration_seconds = 3600 # 1 hour end
And any other way provided by anyway_config. Check its documentation!
Development
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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/Envek/graphql-anycable.
License
The gem is available as open source under the terms of the MIT License.