1# frozen_string_literal: true
2
3module Types
4  class DurationType < BaseScalar
5    graphql_name 'Duration'
6    description <<~DESC
7      Duration between two instants, represented as a fractional number of seconds.
8
9      For example: 12.3334
10    DESC
11
12    def self.coerce_input(value, ctx)
13      case value
14      when Float
15        value
16      when Integer
17        value.to_f
18      when NilClass
19        raise GraphQL::CoercionError, 'Cannot be nil'
20      else
21        raise GraphQL::CoercionError, "Expected number: got #{value.class}"
22      end
23    end
24
25    def self.coerce_result(value, ctx)
26      value.to_f
27    end
28  end
29end
30