mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-21 13:53:20 +12:00
30 lines
957 B
Markdown
30 lines
957 B
Markdown
|
# 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
|
||
|
```
|