From c530245295e1d08fd4cb2e965e7890d1b69820ff Mon Sep 17 00:00:00 2001 From: WolfDan <5377526+WolfDan@users.noreply.github.com> Date: Wed, 5 May 2021 15:15:01 -0500 Subject: [PATCH] improvement: Add float type (#20) --- lib/resource/resource.ex | 1 + test/read_test.exs | 29 +++++++++++++++++++++++++++++ test/support/resources/post.ex | 8 ++++++++ 3 files changed, 38 insertions(+) diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 6059db4..ead743c 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -2103,6 +2103,7 @@ defmodule AshGraphql.Resource do defp do_field_type(Ash.Type.UtcDatetime, _, _), do: :naive_datetime defp do_field_type(Ash.Type.UtcDatetimeUsec, _, _), do: :naive_datetime defp do_field_type(Ash.Type.UUID, _, _), do: :string + defp do_field_type(Ash.Type.Float, _, _), do: :float # sobelow_skip ["DOS.StringToAtom"] defp atom_enum_type(resource, attribute_name) do diff --git a/test/read_test.exs b/test/read_test.exs index 9bfd09f..e27227f 100644 --- a/test/read_test.exs +++ b/test/read_test.exs @@ -14,6 +14,35 @@ defmodule AshGraphql.ReadTest do end) end + test "float fields works correctly" do + AshGraphql.Test.Post + |> Ash.Changeset.for_create(:create, text: "foo", published: true, score: 9.8) + |> AshGraphql.Test.Api.create!() + + AshGraphql.Test.Post + |> Ash.Changeset.for_create(:create, text: "bar", published: true, score: 9.85) + |> AshGraphql.Test.Api.create!() + + resp = + """ + query PostScore($score: Float) { + postScore(score: $score) { + text + } + } + """ + |> Absinthe.run(AshGraphql.Test.Schema, + variables: %{ + "score" => 9.8 + } + ) + + assert {:ok, result} = resp + + refute Map.has_key?(result, :errors) + assert %{data: %{"postScore" => [%{"text" => "foo"}]}} = result + end + test "a read with arguments works" do AshGraphql.Test.Post |> Ash.Changeset.for_create(:create, text: "foo", published: true) diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index 962f172..02147a0 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -11,6 +11,7 @@ defmodule AshGraphql.Test.Post do queries do get :get_post, :read list :post_library, :library + list :post_score, :score list :paginated_posts, :paginated end @@ -70,6 +71,12 @@ defmodule AshGraphql.Test.Post do ) end + read :score do + argument(:score, :float, allow_nil?: true) + + filter(expr(score == ^arg(:score))) + end + read :best_post do filter(expr(best == true)) end @@ -87,6 +94,7 @@ defmodule AshGraphql.Test.Post do attribute(:status, AshGraphql.Test.Status) attribute(:status_enum, AshGraphql.Test.StatusEnum) attribute(:best, :boolean) + attribute(:score, :float) end calculations do