Subscriptions
subscribe_on
Section titled “subscribe_on”Declare which lifecycle events should trigger real-time subscriptions:
class ArticleResource include Quail::Resource
attributes :id, :title
subscribe_on :create, :update, :deleteendMutations automatically trigger the corresponding subscription events.
Scoped Subscriptions
Section titled “Scoped Subscriptions”Scoped subscriptions are supported too:
subscribe_on :update, scope: :author_idsubscribe_on :create, scope: { team_id: ->(record) { record.team.id } }Custom Subscription Override
Section titled “Custom Subscription Override”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 manuallyendclass 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 } endendWire it into your schema manually:
class AppSchema < GraphQL::Schema Quail::SchemaBuilder.call(self) do |schema| # Add custom subscription fields alongside auto-generated ones endendCustom Channel Override
Section titled “Custom Channel Override”Generate a customizable ActionCable channel:
rails generate quail:channelThis creates a channel that inherits from Quail::Channel where you can inject auth context:
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 endend