-
-
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.
create config that can use the filter to create a context_id for de-d…
…uplication
- Loading branch information
Showing
4 changed files
with
51 additions
and
4 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
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 |
---|---|---|
@@ -1,3 +1,40 @@ | ||
defmodule AshGraphql.Resource.Subscription.DefaultConfig do | ||
def config(_, _), do: dbg({:ok, topic: "*"}) | ||
alias AshGraphql.Resource.Subscription | ||
|
||
def create_config(%Subscription{} = subscription, api, resource) do | ||
config_module = String.to_atom(Macro.camelize(Atom.to_string(subscription.name)) <> ".Config") | ||
dbg() | ||
|
||
defmodule config_module do | ||
require Ash.Query | ||
|
||
@subscription subscription | ||
@resource resource | ||
@api api | ||
def config(_args, %{context: context}) do | ||
read_action = | ||
@subscription.read_action || Ash.Resource.Info.primary_action!(@resource, :read).name | ||
|
||
case Ash.Api.can( | ||
@api, | ||
Ash.Query.for_read(@resource, read_action) | ||
|> Ash.Query.filter(id == "test"), | ||
context[:actor], | ||
run_queries?: false, | ||
alter_source?: true | ||
) do | ||
{:ok, true} -> | ||
{:ok, topic: "*", context_id: "global"} | ||
|
||
{:ok, true, filter} -> | ||
{:ok, topic: "*", context_id: Base.encode64(:erlang.term_to_binary(filter))} | ||
|
||
_ -> | ||
{:error, "unauthorized"} | ||
end | ||
end | ||
end | ||
|
||
&config_module.config/2 | ||
end | ||
end |
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
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