Sha256: fda4bc10e1322f086ef30e4683d219884ca7500cdd2d22bef176fb26f587d8e6

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

class GraphqlChannel < ActionCable::Channel::Base
  def subscribed
    @subscription_ids = []
  end

  def execute(data)
    result = StitchedSchema.execute(
      data["query"],
      context: { channel: self },
      variables: ensure_hash(data["variables"]),
      operation_name: data["operationName"],
    )

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

    if result.context[:subscription_id]
      @subscription_ids << result.context[:subscription_id]
    end

    transmit(payload)
  end

  def unsubscribed
    @subscription_ids.each { |sid|
      SubscriptionsSchema.subscriptions.delete_subscription(sid)
    }
  end

  private

  def ensure_hash(ambiguous_param)
    case ambiguous_param
    when String
      if ambiguous_param.present?
        ensure_hash(JSON.parse(ambiguous_param))
      else
        {}
      end
    when Hash, ActionController::Parameters
      ambiguous_param
    when nil
      {}
    else
      raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphql-stitching-1.5.2 examples/subscriptions/app/channels/graphql_channel.rb
graphql-stitching-1.5.1 examples/subscriptions/app/channels/graphql_channel.rb
graphql-stitching-1.5.0 examples/subscriptions/app/channels/graphql_channel.rb