Skip to content

Commit 5ab7951

Browse files
committed
fix: suppress embedded type warning in arrays when building unions
fixes #376
1 parent 3fe8b4c commit 5ab7951

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

lib/resource/resource.ex

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3722,7 +3722,8 @@ defmodule AshGraphql.Resource do
37223722
config[:type],
37233723
%{attribute | name: String.to_atom("#{attribute.name}_#{name}")},
37243724
resource,
3725-
true
3725+
true,
3726+
warn_unknown?: false
37263727
)
37273728
}
37283729
end)
@@ -4721,7 +4722,7 @@ defmodule AshGraphql.Resource do
47214722
end
47224723

47234724
@doc false
4724-
def field_type(type, field, resource, input? \\ false) do
4725+
def field_type(type, field, resource, input? \\ false, opts \\ []) do
47254726
case field do
47264727
%Ash.Resource.Attribute{name: name} ->
47274728
override =
@@ -4734,68 +4735,76 @@ defmodule AshGraphql.Resource do
47344735
if override do
47354736
unwrap_literal_type(override)
47364737
else
4737-
do_field_type(type, field, resource, input?)
4738+
do_field_type(type, field, resource, input?, nil, opts)
47384739
end
47394740

47404741
_ ->
4741-
do_field_type(type, field, resource, input?)
4742+
do_field_type(type, field, resource, input?, nil, opts)
47424743
end
47434744
end
47444745

4745-
defp do_field_type(type, field, resource, input?, constraints \\ nil)
4746+
defp do_field_type(type, field, resource, input?, constraints \\ nil, opts \\ [])
47464747

47474748
defp do_field_type(
47484749
{:array, type},
47494750
%Ash.Resource.Aggregate{kind: :list} = aggregate,
47504751
resource,
47514752
input?,
4752-
_
4753+
_,
4754+
opts
47534755
) do
47544756
with related when not is_nil(related) <-
47554757
Ash.Resource.Info.related(resource, aggregate.relationship_path),
47564758
attr when not is_nil(related) <- Ash.Resource.Info.attribute(related, aggregate.field) do
47574759
if attr.allow_nil? do
47584760
%Absinthe.Blueprint.TypeReference.List{
4759-
of_type: do_field_type(type, aggregate, resource, input?)
4761+
of_type: do_field_type(type, aggregate, resource, input?, opts)
47604762
}
47614763
else
47624764
%Absinthe.Blueprint.TypeReference.List{
47634765
of_type: %Absinthe.Blueprint.TypeReference.NonNull{
4764-
of_type: do_field_type(type, aggregate, resource, input?)
4766+
of_type: do_field_type(type, aggregate, resource, input?, opts)
47654767
}
47664768
}
47674769
end
47684770
end
47694771
end
47704772

4771-
defp do_field_type({:array, type}, %Ash.Resource.Aggregate{} = aggregate, resource, input?, _) do
4773+
defp do_field_type(
4774+
{:array, type},
4775+
%Ash.Resource.Aggregate{} = aggregate,
4776+
resource,
4777+
input?,
4778+
_,
4779+
opts
4780+
) do
47724781
%Absinthe.Blueprint.TypeReference.List{
4773-
of_type: do_field_type(type, aggregate, resource, input?)
4782+
of_type: do_field_type(type, aggregate, resource, input?, opts)
47744783
}
47754784
end
47764785

4777-
defp do_field_type({:array, type}, nil, resource, input?, constraints) do
4786+
defp do_field_type({:array, type}, nil, resource, input?, constraints, opts) do
47784787
# Due to ash not automatically adding the default array constraints to
47794788
# types defined outside of an `attribute` we need to default to true here
47804789
# and not to false.
47814790
nil_items? = Keyword.get(constraints || [], :nil_items?, true)
47824791

47834792
field_type =
4784-
do_field_type(type, nil, resource, input?, constraints[:items] || [])
4793+
do_field_type(type, nil, resource, input?, constraints[:items] || [], opts)
47854794
|> maybe_wrap_non_null(!nil_items?)
47864795

47874796
%Absinthe.Blueprint.TypeReference.List{
47884797
of_type: field_type
47894798
}
47904799
end
47914800

4792-
defp do_field_type({:array, type}, attribute, resource, input?, _) do
4801+
defp do_field_type({:array, type}, attribute, resource, input?, _, opts) do
47934802
new_constraints = attribute.constraints[:items] || []
47944803
new_attribute = %{attribute | constraints: new_constraints, type: type}
47954804

47964805
field_type =
47974806
type
4798-
|> do_field_type(new_attribute, resource, input?, new_constraints)
4807+
|> do_field_type(new_attribute, resource, input?, new_constraints, opts)
47994808
|> maybe_wrap_non_null(!attribute.constraints[:nil_items?] || embedded?(attribute.type))
48004809

48014810
%Absinthe.Blueprint.TypeReference.List{
@@ -4804,7 +4813,7 @@ defmodule AshGraphql.Resource do
48044813
end
48054814

48064815
# sobelow_skip ["DOS.BinToAtom"]
4807-
defp do_field_type(type, attribute, resource, input?, constraints) do
4816+
defp do_field_type(type, attribute, resource, input?, constraints, opts) do
48084817
type = Ash.Type.get_type(type)
48094818

48104819
constraints =
@@ -4828,9 +4837,11 @@ defmodule AshGraphql.Resource do
48284837
if input? && type(type) do
48294838
case embedded_type_input(resource, attribute, type, nil) do
48304839
nil ->
4831-
IO.warn(
4832-
"Embedded type #{inspect(type)} does not have an input type defined, but is accepted as input in at least one location."
4833-
)
4840+
if Keyword.get(opts, :warn_unknown?, true) do
4841+
IO.warn(
4842+
"Embedded type #{inspect(type)} does not have an input type defined, but is accepted as input in at least one location."
4843+
)
4844+
end
48344845

48354846
Application.get_env(:ash_graphql, :json_type) || :json_string
48364847

@@ -4882,7 +4893,8 @@ defmodule AshGraphql.Resource do
48824893
constraints: Ash.Type.NewType.constraints(type, constraints)
48834894
},
48844895
resource,
4885-
input?
4896+
input?,
4897+
opts
48864898
)
48874899
else
48884900
raise """

0 commit comments

Comments
 (0)