-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add doc for use-subscriptions-with-graphql.md
- Loading branch information
1 parent
d485f53
commit d7a49bc
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |