From 64737aa5055a41c5f1ea50ffac46a5e10314f11a Mon Sep 17 00:00:00 2001 From: Barnabas Jovanovics Date: Mon, 8 Jan 2024 17:47:53 +0100 Subject: [PATCH] add subscription field to schema --- lib/ash_graphql.ex | 11 +++++++++-- lib/domain/domain.ex | 1 + lib/resource/resource.ex | 10 ++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/ash_graphql.ex b/lib/ash_graphql.ex index 7d779f1..d26ad38 100644 --- a/lib/ash_graphql.ex +++ b/lib/ash_graphql.ex @@ -215,6 +215,13 @@ defmodule AshGraphql do end) end) + blueprint_with_subscriptions = + api + |> AshGraphql.Api.subscriptions(unquote(resources), action_middleware, __MODULE__) + |> Enum.reduce(blueprint_with_mutations, fn subscription, blueprint -> + Absinthe.Blueprint.add_field(blueprint, "RootSubscriptionType", subscription) + end) + managed_relationship_types = AshGraphql.Resource.managed_relationship_definitions( Process.get(:managed_relationship_requirements, []), @@ -304,7 +311,7 @@ defmodule AshGraphql do end new_defs = - List.update_at(blueprint_with_mutations.schema_definitions, 0, fn schema_def -> + List.update_at(blueprint_with_subscriptions.schema_definitions, 0, fn schema_def -> %{ schema_def | type_definitions: @@ -313,7 +320,7 @@ defmodule AshGraphql do } end) - {:ok, %{blueprint_with_mutations | schema_definitions: new_defs}} + {:ok, %{blueprint_with_subscriptions | schema_definitions: new_defs}} end end diff --git a/lib/domain/domain.ex b/lib/domain/domain.ex index ca48e2a..a4b824d 100644 --- a/lib/domain/domain.ex +++ b/lib/domain/domain.ex @@ -211,6 +211,7 @@ defmodule AshGraphql.Domain do def subscriptions(api, resources, action_middleware, schema) do resources + |> IO.inspect(label: :subscriptions) |> Enum.filter(fn resource -> AshGraphql.Resource in Spark.extensions(resource) end) diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 6a59a0a..3d24e9e 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -1153,9 +1153,15 @@ defmodule AshGraphql.Resource do def subscriptions(api, resource, action_middleware, schema) do resource |> subscriptions() + |> Enum.map(fn %Subscription{name: name, config: config} -> + %Absinthe.Blueprint.Schema.FieldDefinition{ + identifier: name, + name: to_string(name), + type: AshGraphql.Resource.Info.type(resource), + __reference__: ref(__ENV__) + } + end) |> dbg() - - [] end @doc false