Skip to content

Commit

Permalink
create config that can use the filter to create a context_id for de-d…
Browse files Browse the repository at this point in the history
…uplication
  • Loading branch information
barnabasJ committed Feb 21, 2024
1 parent 3b6bbde commit ffd851e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,12 @@ defmodule AshGraphql.Resource do
%Absinthe.Blueprint.Schema.FieldDefinition{
identifier: name,
name: to_string(name),
config: &AshGraphql.Resource.Subscription.DefaultConfig.config/2,
config:
AshGraphql.Resource.Subscription.DefaultConfig.create_config(
subscription,
api,
resource
),
module: schema,
middleware:
action_middleware ++
Expand Down
39 changes: 38 additions & 1 deletion lib/resource/subscription/default_config.ex
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
8 changes: 6 additions & 2 deletions lib/resource/subscription/default_resolve.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
defmodule AshGraphql.Resource.Subscription.DefaultResolve do
require Ash.Query

def resolve(%Absinthe.Resolution{state: :resolved} = resolution, _),
do: resolution
def resolve(%Absinthe.Resolution{state: :resolved} = resolution, _) do
dbg()
resolution
end

def resolve(
%{arguments: arguments, context: context} = resolution,
{api, resource, %AshGraphql.Resource.Subscription{}, input?}
) do
dbg()

result =
AshGraphql.Subscription.query_for_subscription(
resource,
Expand Down
1 change: 1 addition & 0 deletions lib/resource/subscription/notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule AshGraphql.Resource.Subscription.Notifier do

@impl Ash.Notifier
def notify(notification) do
dbg()
pub_sub = Info.subscription_pubsub(notification.resource)

for subscription <- AshGraphql.Resource.Info.subscriptions(notification.resource) do
Expand Down

0 comments on commit ffd851e

Please sign in to comment.