diff --git a/lib/ash_graphql.ex b/lib/ash_graphql.ex index f5dcbc31..7808176e 100644 --- a/lib/ash_graphql.ex +++ b/lib/ash_graphql.ex @@ -133,6 +133,11 @@ defmodule AshGraphql do @dialyzer {:nowarn_function, {:run, 2}} def run(blueprint, _opts) do + # IO.inspect( + # blueprint.schema_definitions + # |> Enum.find(&(&1.name == "RootSubscriptionType").fields) + # ) + api = unquote(api) action_middleware = unquote(action_middleware) diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index be8154fa..05a75efe 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -886,10 +886,18 @@ defmodule AshGraphql.Resource do def subscriptions(api, resource, action_middleware, schema) do resource |> subscriptions() - |> Enum.map(fn %Subscription{name: name, config: config} -> + |> Enum.map(fn %Subscription{name: name, config: config} = subscription -> %Absinthe.Blueprint.Schema.FieldDefinition{ identifier: name, name: to_string(name), + config: config, + module: schema, + middleware: + action_middleware ++ + [ + {{AshGraphql.Resource.Subscription.DefaultResolve, :resolve}, + {api, resource, subscription, true}} + ], type: AshGraphql.Resource.Info.type(resource), __reference__: ref(__ENV__) } diff --git a/lib/resource/subscription.ex b/lib/resource/subscription.ex index 6f8ae30b..8d74d5c0 100644 --- a/lib/resource/subscription.ex +++ b/lib/resource/subscription.ex @@ -12,17 +12,13 @@ defmodule AshGraphql.Resource.Subscription do doc: "The name to use for the subscription." ], config: [ - type: - {:spark_function_behaviour, AshGraphql.Resource.Subscription.Config, - {AshGraphql.Resource.Subscription.Config.Function, 2}}, + type: {:mfa_or_fun, 2}, doc: """ Function that creates the config for the subscription """ ], resolve: [ - type: - {:spark_function_behaviour, AshGraphql.Resource.Subscription.Resolve, - {AshGraphql.Resource.Subscription.Resolve.Function, 3}}, + type: {:mfa_or_fun, 3}, doc: """ Function that creates the config for the subscription """, diff --git a/lib/resource/subscription/config.ex b/lib/resource/subscription/config.ex deleted file mode 100644 index 0b924231..00000000 --- a/lib/resource/subscription/config.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule AshGraphql.Resource.Subscription.Config do - @callback config(args :: map(), info :: map()) :: {:ok, Keyword.t()} | {:error, Keyword.t()} - - defmacro __using__(_) do - quote do - @behaviour AshGraphql.Resource.Subscription.Config - end - end -end diff --git a/lib/resource/subscription/config_function.ex b/lib/resource/subscription/config_function.ex deleted file mode 100644 index e5d5dfa9..00000000 --- a/lib/resource/subscription/config_function.ex +++ /dev/null @@ -1,13 +0,0 @@ -defmodule AshGraphql.Resource.Subscription.ConfigFunction do - use AshGraphql.Resource.Subscription.Config - - @impl true - def config(changeset, [fun: {m, f, a}], context) do - apply(m, f, [changeset, context | a]) - end - - @impl true - def config(changeset, [fun: fun], context) do - fun.(changeset, context) - end -end diff --git a/lib/resource/subscription/default_resolve.ex b/lib/resource/subscription/default_resolve.ex index 2be89cd1..980481d7 100644 --- a/lib/resource/subscription/default_resolve.ex +++ b/lib/resource/subscription/default_resolve.ex @@ -1,13 +1,27 @@ defmodule AshGraphql.Resource.Subscription.DefaultResolve do require Ash.Query - def resolve(args, _, resolution) do - AshGraphql.Subscription.query_for_subscription( - Post, - Api, - resolution - ) - |> Ash.Query.filter(id == ^args.id) - |> Api.read(actor: resolution.context.current_user) + def resolve(%Absinthe.Resolution{state: :resolved} = resolution, _), + do: resolution + + def resolve( + %{arguments: arguments, context: context} = resolution, + {api, resource, %AshGraphql.Resource.Subscription{}, input?} + ) do + dbg() + + result = + AshGraphql.Subscription.query_for_subscription( + resource, + api, + resolution + ) + # |> Ash.Query.filter(id == ^args.id) + |> Ash.Query.limit(1) + |> api.read_one(actor: resolution.context[:current_user]) + |> IO.inspect() + + resolution + |> Absinthe.Resolution.put_result(result) end end diff --git a/lib/resource/subscription/resolve.ex b/lib/resource/subscription/resolve.ex deleted file mode 100644 index 0c4a08c0..00000000 --- a/lib/resource/subscription/resolve.ex +++ /dev/null @@ -1,10 +0,0 @@ -defmodule AshGraphql.Resource.Subscription.Resolve do - @callback resolve(args :: map(), info :: map(), resolution :: map()) :: - {:ok, list()} | {:error, binary()} - - defmacro __using__(_) do - quote do - @behaviour AshGraphql.Resource.Subscription.Resolve - end - end -end diff --git a/lib/resource/subscription/resolve_function.ex b/lib/resource/subscription/resolve_function.ex deleted file mode 100644 index 290ab766..00000000 --- a/lib/resource/subscription/resolve_function.ex +++ /dev/null @@ -1,13 +0,0 @@ -defmodule AshGraphql.Resource.Subscription.ResolveFunction do - use AshGraphql.Resource.Subscription.Resolve - - @impl true - def resolve(changeset, [fun: {m, f, a}], context) do - apply(m, f, [changeset, context | a]) - end - - @impl true - def resolve(changeset, [fun: fun], context) do - fun.(changeset, context) - end -end