GraphQL subscriptions for AnyCable
A (mostly) drop-in replacement for default ActionCable subscriptions adapter shipped with graphql gem but works with AnyCable!
IMPORTANT: This gem is still in proof of concept stage. It is not yet tested in production and everything may change without any notice. You have warned.
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. No expiration or data cleanup yet.
- 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
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)
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.