diff --git a/lib/ash_graphql.ex b/lib/ash_graphql.ex index a64c8039..84f18e13 100644 --- a/lib/ash_graphql.ex +++ b/lib/ash_graphql.ex @@ -748,6 +748,16 @@ defmodule AshGraphql do type = Ash.Type.NewType.subtype_of(type) nested_attrs(type, all_domains, constraints, already_checked) + type == Ash.Type.Struct && Keyword.has_key?(constraints, :instance_of) -> + type = Keyword.get(constraints, :instance_of) + + if function_exported?(type, :subtype_constraints, 0) do + constraints = apply(type, :subtype_constraints, []) + nested_attrs(type, all_domains, constraints, already_checked) + else + {[], already_checked} + end + true -> {[], already_checked} end diff --git a/test/read_test.exs b/test/read_test.exs index e0ad5c1e..150395e9 100644 --- a/test/read_test.exs +++ b/test/read_test.exs @@ -1625,4 +1625,34 @@ defmodule AshGraphql.ReadTest do } = result end end + + describe "calculations" do + test "loads calculated struct type that uses constraint" do + post = + AshGraphql.Test.Post + |> Ash.Changeset.for_create(:create, + text: "a", + published: true + ) + |> Ash.create!() + + resp = + """ + query postLibrary { + postLibrary(sort: {field: TEXT}) { + structCalc { + some + } + } + } + """ + |> Absinthe.run(AshGraphql.Test.Schema) + + assert {:ok, result} = resp + + refute Map.has_key?(result, :errors) + + assert %{data: %{"postLibrary" => [%{"structCalc" => %{"some" => "string"}}]}} = result + end + end end diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index 3179ee91..095e4a70 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -625,6 +625,16 @@ defmodule AshGraphql.Test.Post do end, public?: true ) + + calculate( + :struct_calc, + :struct, + fn records, _ -> + Enum.map(records, fn _ -> %{some: "string"} end) + end, + public?: true, + constraints: [instance_of: AshGraphql.Test.UnreferencedType] + ) end aggregates do diff --git a/test/support/types/unreferenced_type.ex b/test/support/types/unreferenced_type.ex new file mode 100644 index 00000000..1232001f --- /dev/null +++ b/test/support/types/unreferenced_type.ex @@ -0,0 +1,13 @@ +defmodule AshGraphql.Test.UnreferencedType do + @moduledoc false + use Ash.Type.NewType, + subtype_of: :map, + constraints: [ + fields: [ + some: [ + type: :string, + allow_nil?: false + ] + ] + ] +end