Subscription Type

Subscription is the entry point for all subscriptions in a GraphQL system. Each field corresponds to an event which may be subscribed to:

type Subscription {
  # Triggered whenever a post is added
  postWasPublished: Post
  # Triggered whenever a comment is added;
  # to watch a certain post, provide a `postId`
  commentWasPublished(postId: ID): Comment
}

This type is the root for subscription operations, for example:

subscription {
  postWasPublished {
    # This data will be delivered whenever `postWasPublished`
    # is triggered by the server:
    title
    author {
      name
    }
  }
}

To add subscriptions to your system, define an ObjectType named Subscription:

# app/graphql/types/subscription_type.rb
class Types::SubscriptionType < GraphQL::Schema::Object
  field :post_was_published, subscription: Subscriptions::PostWasPublished
  # ...
end

Then, add it as the subscription root with subscription(...):

# app/graphql/my_schema.rb
class MySchema < GraphQL::Schema
  query(Types::QueryType)
  # ...
  # Add Subscription to
  subscription(Types::SubscriptionType)
end

See Implementing Subscriptions for more about actually delivering updates.

See Subscription Classes for more about implementing subscription root fields.