improvement: support new struct fields constraint

This commit is contained in:
Zach Daniel 2024-08-09 17:29:52 -04:00
parent b79cd6c562
commit 190a7a1f75
2 changed files with 26 additions and 7 deletions

View file

@ -680,13 +680,27 @@ defmodule AshGraphql do
}
case attribute.type do
Ash.Type.Map ->
if attribute.constraints[:fields] do
{source_resource, attribute}
type when type in [Ash.Type.Map, Ash.Type.Keyword, Ash.Type.Struct] ->
if fields = attribute.constraints[:fields] do
Enum.flat_map(fields, fn {name, config} ->
if AshGraphql.Resource.embedded?(config[:type]) do
[
{source_resource,
%{
attribute
| type: config[:type],
constraints: config[:constraints],
name: :"#{attribute.name}_#{name}"
}}
]
else
[]
end
end)
else
[]
end
[]
Ash.Type.Union ->
attribute.constraints[:types]
|> Kernel.||([])

View file

@ -2979,9 +2979,14 @@ defmodule AshGraphql.Resource do
{types, fields} =
Enum.reduce(constraints[:fields] || [], {[], []}, fn {name, attribute}, {types, fields} ->
map_type? =
attribute[:type] in [:map, Ash.Type.Map] ||
attribute[:type] in [:map, Ash.Type.Map, :struct, Ash.Type.Struct] ||
(Ash.Type.NewType.new_type?(attribute[:type]) &&
Ash.Type.NewType.subtype_of(attribute[:type]) in [:map, Ash.Type.Map])
Ash.Type.NewType.subtype_of(attribute[:type]) in [
:map,
Ash.Type.Map,
:struct,
Ash.Type.Struct
])
if map_type? && attribute[:constraints] not in [nil, []] do
nested_type_name =