Class: GraphQL::Types::ISO8601Duration

Inherits:
Schema::Scalar show all
Defined in:
lib/graphql/types/iso_8601_duration.rb

Overview

This scalar takes Durations and transmits them as strings, using ISO 8601 format. ActiveSupport >= 5.0 must be loaded to use this scalar.

Use it for fields or arguments as follows:

field :age, GraphQL::Types::ISO8601Duration, null: false

argument :interval, GraphQL::Types::ISO8601Duration, null: false

Alternatively, use this built-in scalar as inspiration for your own Duration type.

Constant Summary

Constants included from Schema::Member::GraphQLTypeNames

Schema::Member::GraphQLTypeNames::Boolean, Schema::Member::GraphQLTypeNames::ID, Schema::Member::GraphQLTypeNames::Int

Instance Attribute Summary

Attributes included from Schema::Member::BaseDSLMethods

#default_graphql_name, #graphql_name

Attributes included from Schema::Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Attributes included from Schema::Member::HasAstNode

#ast_node

Class Method Summary collapse

Methods inherited from Schema::Scalar

default_scalar, default_scalar?, kind, specified_by_url, validate_non_null_input

Methods included from Schema::Member::ValidatesInput

#coerce_isolated_input, #coerce_isolated_result, #valid_input?, #valid_isolated_input?, #validate_input

Methods included from Schema::Member::BaseDSLMethods

#authorized?, #default_relay, #description, #introspection, #introspection?, #mutation, #name, #visible?

Methods included from Schema::Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Schema::Member::TypeSystemHelpers

#initialize, #kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Schema::Member::Scoped

#inherited, #reauthorize_scoped_objects, #scope_items

Methods included from Schema::Member::HasPath

#path

Methods included from Schema::Member::HasAstNode

#inherited

Methods included from Schema::Member::HasDirectives

add_directive, #directive, #directives, get_directives, #inherited, #remove_directive, remove_directive

Class Method Details

.coerce_input(value, ctx) ⇒ ActiveSupport::Duration?

Parameters:

  • value (String, ActiveSupport::Duration)

Returns:

  • (ActiveSupport::Duration, nil)

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/graphql/types/iso_8601_duration.rb', line 57

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

  begin
    if value.is_a?(ActiveSupport::Duration)
      value
    elsif value.nil?
      nil
    else
      ActiveSupport::Duration.parse(value)
    end
  rescue ArgumentError, TypeError
    err = GraphQL::DurationEncodingError.new(value)
    ctx.schema.type_error(err, ctx)
  end
end

.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

.seconds_precisionInteger?

Returns:

  • (Integer, nil)


20
21
22
23
# File 'lib/graphql/types/iso_8601_duration.rb', line 20

def self.seconds_precision
  # ActiveSupport::Duration precision defaults to whatever input was given
  @seconds_precision
end

.seconds_precision=(value) ⇒ Object

Parameters:

  • value (Integer, nil)


26
27
28
# File 'lib/graphql/types/iso_8601_duration.rb', line 26

def self.seconds_precision=(value)
  @seconds_precision = value
end