New AshGraphql Feature: Subscriptions

You can now add subscriptions using the DSL.

subscriptions do
  pubsub YourAppWeb.Endpoint

  subscribe :something_changed do
    action_types [:create, :update, :destroy]
  end
end

Right now, you still need to add the dependency from github. A release will follow soon.
I created a small demo video showing the setup.

11 Likes

Amazing work! I will try it out asap!

Off-topic: I noticed that around 6:10 of the video, you saved a Ash resource and the formatter changed the resource code blocks order. How did you do that?

Also, another question.

Can you somehow add logic to filter out data based on some criteria?

For example, let’s say I have a resource that contains data for multiple organizations, I want to subscribe to that resource actions, but I don’t want it to allow an me to subscribe to all data from all organizations for that resource, only for the ones that are from my own organization.

What you subscribe to is a query built for a read action, so that read action can contain filters. Additionally, if you have policies that filter the data, they will be applied to all data before a notification is sent.

3 Likes

That is the Spark.Formatter plugin. You can configure it to set a section order for any Spark DSL including Ash.Resource.

https://hexdocs.pm/spark/Spark.Formatter.html

1 Like

Minimal Reproduction example

Including

  • AshGraphql
  • AshSqlite

In the meanwhile you can skip most steps shown in the Video. As the CLI is doing that for you. You just need to run some cli commands and then modify the resource

If you want to start from scratch you can run:

Setup new project (I think the commands provide on the ash-hq site would also work)

mix igniter.new my_subscription_app_sqlite --with phx.new --install ash,ash_sqlite,ash_graphql,absinthe_phoenix

Create a resource

mix ash.gen.resource MySubscriptionApp.Posts.Post --uuid-v7-primary-key id --attribute title:string:required:public --attribute text:string:required:public --extend graphql,sqlite

Modify the post resource:

Switch from simple GraphQL interface to playground - as the simple seem to not support the subscriptions: