Triggers

From your application, you can push updates to GraphQL clients with .trigger.

Events are triggered by name, and the name must match fields on your Subscription Type

# Update the system with the new blog post:
MySchema.subscriptions.trigger("postAdded", {}, new_post)

The arguments are:

Scope

To send updates to certain clients only, you can use scope: to narrow the trigger’s reach.

Scopes are based on query context: a value in context: is used as the scope; an equivalent value must be passed with .trigger(... scope:) to update that client. (The value is serialized with GraphQL::Subscriptions::Serialize)

To specify that a topic is scoped, edit the field definition on your root Subscription type. Use the subscription_scope helper to name a context: key, for example:

# For a given viewer, this will be triggered
# whenever one of their posts gets a new comment
field :commentAdded, CommentType, "A comment was added to one of the viewer's posts" do
  subscription_scope :current_user_id
end

Then, subscription operations should have a context: { current_user_id: ... } value, for example:

# current_user_id will be the scope for some subscriptions:
MySchema.execute(query_string, context: { current_user_id: current_user.id })

Finally, when events happen in your app, you should provide the scoping value as scope:, for example:

# A new comment is added
comment = post.comments.create!(attrs)
# notify the author
author_id = post.author.id
MySchema.subscriptions.trigger("commentAdded", {}, comment, scope: author_id)

Since this trigger has a scope:, only subscribers with a matching scope value will be updated.