Class: GraphQL::Types::ISO8601Duration
- Inherits:
-
Schema::Scalar
- Object
- Schema::Member
- Schema::Scalar
- GraphQL::Types::ISO8601Duration
- 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
Class Method Summary collapse
-
.coerce_input(value, ctx) ⇒ Object
Parameters.
-
.coerce_result(value, _ctx) ⇒ Object
Parameters.
-
.seconds_precision ⇒ Object
Returns.
-
.seconds_precision=(value) ⇒ Object
Parameters.
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 inherited from Schema::Member
Methods included from Schema::Member::BaseDSLMethods
#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation, #visible?
Methods included from Schema::FindInheritedValue
Methods included from Schema::Member::BaseDSLMethods::ConfigurationExtension
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
Methods included from Schema::Member::HasAstNode
Methods included from Schema::Member::HasDirectives
add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive
Class Method Details
.coerce_input(value, ctx) ⇒ Object
Parameters
value(String, ActiveSupport::Duration)
Returns
ActiveSupport::Duration, nil
Raises
GraphQL::Error— if ActiveSupport::Duration is not definedGraphQL::DurationEncodingError— if duration cannot be parsed
:call-seq:
coerce_input(String | ActiveSupport::Duration value, ctx) -> ActiveSupport::Duration | nil | GraphQL::Error | GraphQL::DurationEncodingError
: (untyped value, GraphQL::Query::Context ctx) -> untyped
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/graphql/types/iso_8601_duration.rb', line 95 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) ⇒ Object
Parameters
value(ActiveSupport::Duration, String)
Returns
String
Raises
GraphQL::Error— if ActiveSupport::Duration is not defined or if an incompatible object is passed
:call-seq:
coerce_result(ActiveSupport::Duration | String value, _ctx) -> String | GraphQL::Error
: (untyped value, GraphQL::Query::Context _ctx) -> ::String
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/graphql/types/iso_8601_duration.rb', line 59 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.})" end end |
.seconds_precision ⇒ Object
Returns
Integer, nil
:call-seq:
seconds_precision() -> Integer | nil
: -> Integer?
28 29 30 31 |
# File 'lib/graphql/types/iso_8601_duration.rb', line 28 def self.seconds_precision # ActiveSupport::Duration precision defaults to whatever input was given @seconds_precision end |
.seconds_precision=(value) ⇒ Object
Parameters
value(Integer, nil)
:call-seq:
seconds_precision=(Integer | nil value)
: (Integer? value) -> Integer?
40 41 42 |
# File 'lib/graphql/types/iso_8601_duration.rb', line 40 def self.seconds_precision=(value) @seconds_precision = value #: Integer? end |