-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfloat.rb
More file actions
30 lines (25 loc) · 846 Bytes
/
float.rb
File metadata and controls
30 lines (25 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true
module GraphQL
module Types
class Float < GraphQL::Schema::Scalar
description "Represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point)."
def self.coerce_input(value, _ctx)
value.is_a?(Numeric) ? value.to_f : nil
end
def self.coerce_result(value, ctx)
coerced_value = Float(value, exception: false)
if coerced_value.nil? || !coerced_value.finite?
error = GraphQL::ScalarCoercionError.new(
"Float cannot represent non numeric value: #{value.inspect}",
value: value,
context: ctx
)
ctx.schema.type_error(error, ctx)
else
coerced_value
end
end
default_scalar true
end
end
end