Trying figure out, while generating GQL schema,
why if :float
data in source var square_plot
exists (say 50
),
there is no ERR
but if it is NIL the code generates error
while i converting value so sqft with such code:
may be it seems no validation passes
CODE
field(:square_plot, :float) // my var is `50.0` in DB
field :square_plot_ft, :float do
if square_plot > 0 do
resolve(fn _args, %{source: source} -> {:ok, Float.round(source.square_plot*10.7639, 0)} end)
end
end
if value NOT NIL the GQL response
{
"data": {
"unit": {
"squareTotalFt": 1615.0,
"squareTotal": 150.0,
"squarePlotFt": 538.0,
"squarePlot": 50.0
}
}
}
if value is NIL the GQL response
"Internal server error"
and server ERR
** (exit) an exception was raised:
** (ArithmeticError) bad argument in arithmetic expression
(platform) lib/units_web/schema/object.ex:228: anonymous fn/2 in UnitsWeb.Schema.Unit.__abs
inthe_type__/1
Surprisingly enough, in erlang, and therefore in elixir, comparisons <
/>
involving different types are legal.
Even more surprisingly, any atom is greater than any number (see Atom Ordering in the documentation.)
nil > 0
#⇒ true
and we get into if
, attempting square_plot*10.7639
which is nil*10.7639
, which obviously raises.
The proper code would be:
field :square_plot_ft, :float do
if is_number(square_plot) and square_plot > 0 do
resolve(...)
end
end