Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-null-list-of-non-null error when root data is nil #1259

3 changes: 2 additions & 1 deletion lib/absinthe/phase/document/execution/resolution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ defmodule Absinthe.Phase.Document.Execution.Resolution do
|> propagate_null_trimming
end

defp maybe_add_non_null_error([], values, %Type.NonNull{of_type: %Type.List{}}) do
defp maybe_add_non_null_error([], values, %Type.NonNull{of_type: %Type.List{}})
when is_list(values) do
values
|> Enum.with_index()
|> Enum.filter(&is_nil(elem(&1, 0)))
Expand Down
34 changes: 32 additions & 2 deletions test/absinthe/phase/execution/non_null_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ defmodule Absinthe.Phase.Document.Execution.NonNullTest do

defp things_resolver(_, %{make_null: make_null}, _) do
if make_null do
{:ok, nil}
else
{:ok, [%{}]}
end
end

defp things_resolver(_, %{make_child_null: make_child_null}, _) do
if make_child_null do
{:ok, [nil]}
else
{:ok, [%{}]}
Expand Down Expand Up @@ -53,6 +61,7 @@ defmodule Absinthe.Phase.Document.Execution.NonNullTest do

field :non_null_list_of_non_null, non_null(list_of(non_null(:thing))) do
arg :make_null, :boolean
arg :make_child_null, :boolean
resolve &things_resolver/3
end
end
Expand All @@ -79,6 +88,7 @@ defmodule Absinthe.Phase.Document.Execution.NonNullTest do

field :non_null_list_of_non_null, non_null(list_of(non_null(:thing))) do
arg :make_null, :boolean
arg :make_child_null, :boolean
resolve &things_resolver/3
end

Expand Down Expand Up @@ -263,7 +273,7 @@ defmodule Absinthe.Phase.Document.Execution.NonNullTest do
assert {:ok, %{data: data, errors: errors}} == Absinthe.run(doc, Schema)
end

test "list of non null things works when child is null" do
test "list of non null things works when root is null" do
doc = """
{
nonNullListOfNonNull(makeNull: true) { __typename }
Expand All @@ -272,6 +282,26 @@ defmodule Absinthe.Phase.Document.Execution.NonNullTest do

data = nil

errors = [
%{
locations: [%{column: 3, line: 2}],
message: "Cannot return null for non-nullable field",
path: ["nonNullListOfNonNull"]
}
]

assert {:ok, %{data: data, errors: errors}} == Absinthe.run(doc, Schema)
benwilson512 marked this conversation as resolved.
Show resolved Hide resolved
end

test "list of non null things works when child is null" do
doc = """
{
nonNullListOfNonNull(makeChildNull: true) { __typename }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this test is modified.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH I see the semantics of make_null was changed.

}
"""

data = nil

errors = [
%{
locations: [%{column: 3, line: 2}],
Expand All @@ -290,7 +320,7 @@ defmodule Absinthe.Phase.Document.Execution.NonNullTest do
nonNullListOfNonNull {
nonNullListOfNonNull {
nonNullListOfNonNull {
nonNullListOfNonNull(makeNull: true) { __typename }
nonNullListOfNonNull(makeChildNull: true) { __typename }
benwilson512 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down