Method: GraphQL::Types::ISO8601Duration.coerce_result

Defined in:
lib/graphql/types/iso_8601_duration.rb

.coerce_result(value, _ctx) ⇒ String

Parameters:

  • value (ActiveSupport::Duration, String)

Returns:

Raises:

  • (GraphQL::Error)

    if ActiveSupport::Duration is not defined or if an incompatible object is passed



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/graphql/types/iso_8601_duration.rb', line 33

def self.coerce_result(value, _ctx)
  unless defined?(ActiveSupport::Duration)
    raise GraphQL::Error, "ActiveSupport >= 5.0 must be loaded to use the built-in ISO8601Duration type."
  end

  begin
    case value
    when ActiveSupport::Duration
      value.iso8601(precision: seconds_precision)
    when ::String
      ActiveSupport::Duration.parse(value).iso8601(precision: seconds_precision)
    else
      # Try calling as ActiveSupport::Duration compatible as a fallback
      value.iso8601(precision: seconds_precision)
    end
  rescue StandardError => error
    raise GraphQL::Error, "An incompatible object (#{value.class}) was given to #{self}. Make sure that only ActiveSupport::Durations and well-formatted Strings are used with this type. (#{error.message})"
  end
end