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).
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)
By default, graphql-anycable evaluates queries and transmits results for every subscription client individually. Of course, it is a waste of resources if you have hundreds or thousands clients subscribed to the same data (and has huge negative impact on performance).
Thankfully, GraphQL-Ruby has added Subscriptions Broadcast feature that allows to group exact same subscriptions, execute them and transmit results only once.
To enable this feature, turn on Interpreter and pass broadcast option set to true to graphql-anycable.
By default all fields are marked as not safe for broadcasting. If a subscription has at least one non-broadcastable field in its query, GraphQL-Ruby will execute every subscription for every client independently. If you sure that all your fields are safe to be broadcasted, you can pass default_broadcastable option set to true (but be aware that it can have security impllications!)
classMySchema < GraphQL::SchemauseGraphQL::Execution::Interpreter# Required for graphql-ruby before 1.12. Remove it when upgrading to 2.0useGraphQL::Analysis::AST# Required for graphql-ruby before 1.12. Remove it when upgrading to 2.0useGraphQL::AnyCable,broadcast: true,default_broadcastable: truesubscriptionSubscriptionTypeend
YAML configuration files (note that this is config/graphql_anycable.yml, notconfig/anycable.yml):
# config/graphql_anycable.ymlproduction:
subscription_expiration_seconds: 300# 5 minutesuse_redis_object_on_cleanup: false # For restricted redis installationsuse_client_provided_uniq_id: false # To avoid problems with non-uniqueness of Apollo channel identifiers
And any other way provided by anyway_config. Check its documentation!
Data model
As in AnyCable there is no place to store subscription data in-memory, it should be persisted somewhere to be retrieved on GraphQLSchema.subscriptions.trigger and sent to subscribed clients. graphql-anycable uses the same Redis database as AnyCable itself.
Grouped event subscriptions: graphql-fingerprints:#{event.topic} sorted set. Used to find all subscriptions on GraphQLSchema.subscriptions.trigger.
Event subscriptions: graphql-subscriptions:#{event.fingerptint} set containing identifiers for all subscriptions for given operation with certain context and arguments (serialized in topic). Fingerprints are already scoped by topic.
Subscription data: graphql-subscription:#{subscription_id} hash contains everything required to evaluate subscription on trigger and create data for client.
Channel subscriptions: graphql-channel:#{channel_id} set containing identifiers for subscriptions created in ActionCable channel to delete them on client disconnect.
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.
Releasing new versions
Bump version number in lib/graphql/anycable/version.rb
In case of pre-releases keep in mind rubygems/rubygems#3086 and check version with command like Gem::Version.new(AfterCommitEverywhere::VERSION).to_s
Fill CHANGELOG.md with missing changes, add header with version and date.
请发表评论