improvement: Add float type (#20)

This commit is contained in:
WolfDan 2021-05-05 15:15:01 -05:00 committed by GitHub
parent 5a1df9f95a
commit c530245295
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

View file

@ -2103,6 +2103,7 @@ defmodule AshGraphql.Resource do
defp do_field_type(Ash.Type.UtcDatetime, _, _), do: :naive_datetime 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.UtcDatetimeUsec, _, _), do: :naive_datetime
defp do_field_type(Ash.Type.UUID, _, _), do: :string defp do_field_type(Ash.Type.UUID, _, _), do: :string
defp do_field_type(Ash.Type.Float, _, _), do: :float
# sobelow_skip ["DOS.StringToAtom"] # sobelow_skip ["DOS.StringToAtom"]
defp atom_enum_type(resource, attribute_name) do defp atom_enum_type(resource, attribute_name) do

View file

@ -14,6 +14,35 @@ defmodule AshGraphql.ReadTest do
end) end)
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 test "a read with arguments works" do
AshGraphql.Test.Post AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create, text: "foo", published: true) |> Ash.Changeset.for_create(:create, text: "foo", published: true)

View file

@ -11,6 +11,7 @@ defmodule AshGraphql.Test.Post do
queries do queries do
get :get_post, :read get :get_post, :read
list :post_library, :library list :post_library, :library
list :post_score, :score
list :paginated_posts, :paginated list :paginated_posts, :paginated
end end
@ -70,6 +71,12 @@ defmodule AshGraphql.Test.Post do
) )
end end
read :score do
argument(:score, :float, allow_nil?: true)
filter(expr(score == ^arg(:score)))
end
read :best_post do read :best_post do
filter(expr(best == true)) filter(expr(best == true))
end end
@ -87,6 +94,7 @@ defmodule AshGraphql.Test.Post do
attribute(:status, AshGraphql.Test.Status) attribute(:status, AshGraphql.Test.Status)
attribute(:status_enum, AshGraphql.Test.StatusEnum) attribute(:status_enum, AshGraphql.Test.StatusEnum)
attribute(:best, :boolean) attribute(:best, :boolean)
attribute(:score, :float)
end end
calculations do calculations do