From ff06d5a540a3c5d5039a4f4b99440128096d4c53 Mon Sep 17 00:00:00 2001 From: Barnabas Jovanovics Date: Tue, 22 Aug 2023 10:05:49 +0200 Subject: [PATCH 1/2] fix: wrap type in non-null reference if allow_nil? is set to false --- lib/resource/resource.ex | 48 ++++++++++++++++++++--- test/attribute_test.exs | 19 +++++++-- test/support/resources/constrained_map.ex | 4 +- test/support/resources/map_types.ex | 10 ++++- 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 1fc0e87d..ffae4a0b 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -2454,13 +2454,24 @@ 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{ @@ -2468,7 +2479,18 @@ defmodule AshGraphql.Resource do 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 ]} @@ -2515,13 +2537,20 @@ 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{ @@ -2529,7 +2558,14 @@ defmodule AshGraphql.Resource do 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 ]} diff --git a/test/attribute_test.exs b/test/attribute_test.exs index 84e84d5c..e23e667d 100644 --- a/test/attribute_test.exs +++ b/test/attribute_test.exs @@ -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"] @@ -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"} + } } ] } @@ -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"} + } } ] } diff --git a/test/support/resources/constrained_map.ex b/test/support/resources/constrained_map.ex index 1a91775e..70328759 100644 --- a/test/support/resources/constrained_map.ex +++ b/test/support/resources/constrained_map.ex @@ -6,7 +6,9 @@ defmodule AshGraphql.Test.ConstrainedMap do fields: [ foo_bar: [ type: :string, - allow_nil?: false + constraints: [ + allow_nil?: false + ] ], baz: [ type: :integer diff --git a/test/support/resources/map_types.ex b/test/support/resources/map_types.ex index 361ea8d5..bc66e1d1 100644 --- a/test/support/resources/map_types.ex +++ b/test/support/resources/map_types.ex @@ -11,7 +11,10 @@ defmodule AshGraphql.Test.MapTypes do constraints( fields: [ foo: [ - type: :string + type: :string, + constraints: [ + allow_nil?: false + ] ], bar: [ type: :integer @@ -39,7 +42,10 @@ defmodule AshGraphql.Test.MapTypes do constraints( fields: [ foo: [ - type: :string + type: :string, + constraints: [ + allow_nil?: false + ] ], bar: [ type: :integer From fa84e7eccade7106c5f8503e8a99554a2ebdbcf8 Mon Sep 17 00:00:00 2001 From: Barnabas Jovanovics Date: Tue, 22 Aug 2023 10:17:07 +0200 Subject: [PATCH 2/2] fix: get allow_nil? from the correct place --- lib/resource/resource.ex | 12 ++++++------ test/support/resources/constrained_map.ex | 4 +--- test/support/resources/map_types.ex | 8 ++------ 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index ffae4a0b..d34d1e8f 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -2456,7 +2456,7 @@ defmodule AshGraphql.Resource do name: to_string(name), type: if Keyword.get( - constraints || [], + attribute, :allow_nil?, true ) do @@ -2471,7 +2471,7 @@ defmodule AshGraphql.Resource do ] } - {type, constraints} -> + {type, _} -> {types, [ %Absinthe.Blueprint.Schema.FieldDefinition{ @@ -2481,7 +2481,7 @@ defmodule AshGraphql.Resource do name: to_string(name), type: if Keyword.get( - constraints || [], + attribute, :allow_nil?, true ) do @@ -2538,7 +2538,7 @@ defmodule AshGraphql.Resource do __reference__: AshGraphql.Resource.ref(env), name: to_string(name), type: - if Keyword.get(constraints || [], :allow_nil?, true) do + if Keyword.get(attribute, :allow_nil?, true) do nested_type_name else %Absinthe.Blueprint.TypeReference.NonNull{ @@ -2550,7 +2550,7 @@ defmodule AshGraphql.Resource do ] } - {type, constraints} -> + {type, _} -> {types, [ %Absinthe.Blueprint.Schema.InputValueDefinition{ @@ -2559,7 +2559,7 @@ defmodule AshGraphql.Resource do __reference__: AshGraphql.Resource.ref(env), name: to_string(name), type: - if Keyword.get(constraints || [], :allow_nil?, true) do + if Keyword.get(attribute, :allow_nil?, true) do do_field_type(type, nil, nil, false) else %Absinthe.Blueprint.TypeReference.NonNull{ diff --git a/test/support/resources/constrained_map.ex b/test/support/resources/constrained_map.ex index 70328759..1a91775e 100644 --- a/test/support/resources/constrained_map.ex +++ b/test/support/resources/constrained_map.ex @@ -6,9 +6,7 @@ defmodule AshGraphql.Test.ConstrainedMap do fields: [ foo_bar: [ type: :string, - constraints: [ - allow_nil?: false - ] + allow_nil?: false ], baz: [ type: :integer diff --git a/test/support/resources/map_types.ex b/test/support/resources/map_types.ex index bc66e1d1..f11ae12c 100644 --- a/test/support/resources/map_types.ex +++ b/test/support/resources/map_types.ex @@ -12,9 +12,7 @@ defmodule AshGraphql.Test.MapTypes do fields: [ foo: [ type: :string, - constraints: [ - allow_nil?: false - ] + allow_nil?: false ], bar: [ type: :integer @@ -43,9 +41,7 @@ defmodule AshGraphql.Test.MapTypes do fields: [ foo: [ type: :string, - constraints: [ - allow_nil?: false - ] + allow_nil?: false ], bar: [ type: :integer