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: wrap type in non-null reference if allow_nil? is set to false #90

Merged
merged 2 commits into from
Aug 22, 2023
Merged
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
48 changes: 42 additions & 6 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2454,21 +2454,43 @@ defmodule AshGraphql.Resource do
identifier: name,
__reference__: AshGraphql.Resource.ref(env),
name: to_string(name),
type: nested_type_name
type:
if Keyword.get(
attribute,
:allow_nil?,
true
) do
nested_type_name
else
%Absinthe.Blueprint.TypeReference.NonNull{
of_type: nested_type_name
}
end
}
| fields
]
}

_ ->
{type, _} ->
{types,
[
%Absinthe.Blueprint.Schema.FieldDefinition{
module: schema,
identifier: name,
__reference__: AshGraphql.Resource.ref(env),
name: to_string(name),
type: do_field_type(attribute[:type], nil, nil, false)
type:
if Keyword.get(
attribute,
:allow_nil?,
true
) do
do_field_type(type, nil, nil, false)
else
%Absinthe.Blueprint.TypeReference.NonNull{
of_type: do_field_type(type, nil, nil, false)
}
end
}
| fields
]}
Expand Down Expand Up @@ -2515,21 +2537,35 @@ defmodule AshGraphql.Resource do
identifier: name,
__reference__: AshGraphql.Resource.ref(env),
name: to_string(name),
type: nested_type_name
type:
if Keyword.get(attribute, :allow_nil?, true) do
nested_type_name
else
%Absinthe.Blueprint.TypeReference.NonNull{
of_type: nested_type_name
}
end
}
| fields
]
}

_ ->
{type, _} ->
{types,
[
%Absinthe.Blueprint.Schema.InputValueDefinition{
module: schema,
identifier: name,
__reference__: AshGraphql.Resource.ref(env),
name: to_string(name),
type: do_field_type(attribute[:type], nil, nil, false)
type:
if Keyword.get(attribute, :allow_nil?, true) do
do_field_type(type, nil, nil, false)
else
%Absinthe.Blueprint.TypeReference.NonNull{
of_type: do_field_type(type, nil, nil, false)
}
end
}
| fields
]}
Expand Down
19 changes: 15 additions & 4 deletions test/attribute_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ defmodule AshGraphql.AttributeTest do
data["__type"]["inputFields"]
|> Enum.find(fn field -> field["name"] == "foo" end)

assert foo_field["type"]["kind"] == "SCALAR"
assert foo_field["type"]["name"] == "String"
# non null field
assert foo_field["type"]["kind"] == "NON_NULL"

assert foo_field["type"]["ofType"]["kind"] == "SCALAR"
assert foo_field["type"]["ofType"]["name"] == "String"

bar_field =
data["__type"]["inputFields"]
Expand Down Expand Up @@ -257,7 +260,11 @@ defmodule AshGraphql.AttributeTest do
},
%{
"name" => "fooBar",
"type" => %{"kind" => "SCALAR", "name" => "String", "ofType" => nil}
"type" => %{
"kind" => "NON_NULL",
"name" => nil,
"ofType" => %{"kind" => "SCALAR", "name" => "String"}
}
}
]
}
Expand Down Expand Up @@ -301,7 +308,11 @@ defmodule AshGraphql.AttributeTest do
},
%{
"name" => "fooBar",
"type" => %{"kind" => "SCALAR", "name" => "String", "ofType" => nil}
"type" => %{
"kind" => "NON_NULL",
"name" => nil,
"ofType" => %{"kind" => "SCALAR", "name" => "String"}
}
}
]
}
Expand Down
6 changes: 4 additions & 2 deletions test/support/resources/map_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ defmodule AshGraphql.Test.MapTypes do
constraints(
fields: [
foo: [
type: :string
type: :string,
allow_nil?: false
],
bar: [
type: :integer
Expand Down Expand Up @@ -39,7 +40,8 @@ defmodule AshGraphql.Test.MapTypes do
constraints(
fields: [
foo: [
type: :string
type: :string,
allow_nil?: false
],
bar: [
type: :integer
Expand Down
Loading