Skip to content

Commit

Permalink
fix: wrap type in non-null reference if allow_nil? is set to false
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ committed Aug 22, 2023
1 parent 8f9e79e commit ff06d5a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
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(
constraints || [],
:allow_nil?,
true
) do
nested_type_name
else
%Absinthe.Blueprint.TypeReference.NonNull{
of_type: nested_type_name
}
end
}
| fields
]
}

_ ->
{type, constraints} ->
{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(
constraints || [],
: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(constraints || [], :allow_nil?, true) do
nested_type_name
else
%Absinthe.Blueprint.TypeReference.NonNull{
of_type: nested_type_name
}
end
}
| fields
]
}

_ ->
{type, constraints} ->
{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(constraints || [], :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
4 changes: 3 additions & 1 deletion test/support/resources/constrained_map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ defmodule AshGraphql.Test.ConstrainedMap do
fields: [
foo_bar: [
type: :string,
allow_nil?: false
constraints: [
allow_nil?: false
]
],
baz: [
type: :integer
Expand Down
10 changes: 8 additions & 2 deletions test/support/resources/map_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ defmodule AshGraphql.Test.MapTypes do
constraints(
fields: [
foo: [
type: :string
type: :string,
constraints: [
allow_nil?: false
]
],
bar: [
type: :integer
Expand Down Expand Up @@ -39,7 +42,10 @@ defmodule AshGraphql.Test.MapTypes do
constraints(
fields: [
foo: [
type: :string
type: :string,
constraints: [
allow_nil?: false
]
],
bar: [
type: :integer
Expand Down

0 comments on commit ff06d5a

Please sign in to comment.