Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/ash_graphql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,16 @@
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, [])

Check warning on line 755 in lib/ash_graphql.ex

View workflow job for this annotation

GitHub Actions / ash-ci / mix credo --strict

Avoid `apply/2` and `apply/3` when the number of arguments is known.
nested_attrs(type, all_domains, constraints, already_checked)
else
{[], already_checked}
end

true ->
{[], already_checked}
end
Expand Down
30 changes: 30 additions & 0 deletions test/read_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1625,4 +1625,34 @@
} = result
end
end

describe "calculations" do
test "loads calculated struct type that uses constraint" do

Check failure on line 1630 in test/read_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci / mix test

test calculations loads calculated struct type that uses constraint (AshGraphql.ReadTest)
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
10 changes: 10 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/support/types/unreferenced_type.ex
Original file line number Diff line number Diff line change
@@ -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
Loading