Skip to content

Subscriptions

Declare which lifecycle events should trigger real-time subscriptions:

class ArticleResource
include Quail::Resource
attributes :id, :title
subscribe_on :create, :update, :delete
end

Mutations automatically trigger the corresponding subscription events.

Scoped subscriptions are supported too:

subscribe_on :update, scope: :author_id
subscribe_on :create, scope: { team_id: ->(record) { record.team.id } }

Replace an auto-generated subscription with a hand-written one:

class ArticleResource
include Quail::Resource
attributes :id, :title
# Don't declare subscribe_on — we'll wire it up manually
end
app/graphql/subscriptions/article_published.rb
class ArticlePublished < GraphQL::Schema::Subscription
field :article, ArticleResource.graphql_type, null: false
def subscribe
# Custom auth or filtering logic
raise GraphQL::ExecutionError, "Not authorized" unless context[:current_user]
super
end
def update
{ article: object }
end
end

Wire it into your schema manually:

class AppSchema < GraphQL::Schema
Quail::SchemaBuilder.call(self) do |schema|
# Add custom subscription fields alongside auto-generated ones
end
end

Generate a customizable ActionCable channel:

Terminal window
rails generate quail:channel

This creates a channel that inherits from Quail::Channel where you can inject auth context:

app/channels/graphql_channel.rb
class GraphqlChannel < Quail::Channel
private
def context_for_subscription
{
channel: self,
current_user: find_verified_user
}
end
def find_verified_user
User.find_by(id: connection.session[:user_id]) ||
reject_unauthorized_connection
end
end