Module: GraphQL

Defined in:
lib/graphql/subscriptions/anycable_subscriptions.rb,
lib/graphql-anycable.rb,
lib/graphql/anycable/config.rb,
lib/graphql/anycable/errors.rb,
lib/graphql/anycable/cleaner.rb,
lib/graphql/anycable/railtie.rb,
lib/graphql/anycable/version.rb

Overview

A subscriptions implementation that sends data as AnyCable broadcastings.

Since AnyCable is aimed to be compatible with ActionCable, this adapter may be used as (practically) drop-in replacement to ActionCable adapter shipped with graphql-ruby.

Examples:

Adding AnyCableSubscriptions to your schema

MySchema = GraphQL::Schema.define do
  use GraphQL::Subscriptions::AnyCableSubscriptions
end

Implementing a channel for GraphQL Subscriptions

class GraphqlChannel < ApplicationCable::Channel
  def execute(data)
    query = data["query"]
    variables = ensure_hash(data["variables"])
    operation_name = data["operationName"]
    context = {
      current_user: current_user,
      # Make sure the channel is in the context
      channel: self,
    }

    result = MySchema.execute({
      query: query,
      context: context,
      variables: variables,
      operation_name: operation_name
    })

    payload = {
      result: result.subscription? ? {data: nil} : result.to_h,
      more: result.subscription?,
    }

    transmit(payload)
  end

  def unsubscribed
    MySchema.subscriptions.delete_channel_subscriptions(self)
  end
end

Defined Under Namespace

Modules: AnyCable Classes: Subscriptions