Giter Club home page Giter Club logo

graphql-anycable's Introduction

GraphQL subscriptions for AnyCable

A (mostly) drop-in replacement for default ActionCable subscriptions adapter shipped with graphql gem but works with AnyCable!

Gem Version

Sponsored by Evil Martians

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

  1. Plug it into the schema (replace from ActionCable adapter if you have one):

    class MySchema < GraphQL::Schema
      use GraphQL::Subscriptions::AnyCableSubscriptions
    
      subscription SubscriptionType
    end
  2. 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.

  3. Trigger events as usual:

    MySchema.subscriptions.trigger(:product_updated, {}, Product.first!, scope: account.id)

Operations

To avoid filling Redis storage with stale subscription data:

  1. 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.

  2. Execute rake graphql:anycable:clean once in a while to clean up stale subscription data.

    Heroku users should set up GRAPHQL_ANYCABLE_USE_REDIS_OBJECT_ON_CLEANUP environment variable to false due to limitations in Heroku Redis.

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.

graphql-anycable's People

Contributors

envek avatar fx-hao avatar bibendi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.