Skip to content

Commit

Permalink
docs: add doc for use-subscriptions-with-graphql.md
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Oct 2, 2023
1 parent d485f53 commit d7a49bc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions documentation/how_to/use-subscriptions-with-graphql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Using Subscriptions

The AshGraphql DSL does not currently support subscriptions. However, you can do this with Absinthe direclty, and use `AshGraphql.Subscription.query_for_subscription/3`. Here is an example of how you could do this for a subscription for a single record. This example could be extended to support lists of records as well.

```elixir
# in your absinthe schema file
subscription do
field :field, :type_name do
config(fn
_args, %{context: %{current_user: %{id: user_id}}} ->
{:ok, topic: user_id, context_id: "user/#{user_id}"}

_args, _context ->
{:error, :unauthorized}
end)

resolve(fn args, _, resolution ->
# loads all the data you need
AshGraphql.Subscription.query_for_subscription(
YourResource,
YourAPi,
resolution
)
|> Ash.Query.filter(id == ^args.id)
|> YourAPi.read(actor: resolution.context.current_user)
end)
end
end
```

0 comments on commit d7a49bc

Please sign in to comment.