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 Duration
s 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) ⇒ ActiveSupport::Duration?
- .coerce_result(value, _ctx) ⇒ String
- .seconds_precision ⇒ Integer?
- .seconds_precision=(value) ⇒ Object
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?, #comment, #default_relay, #description, #introspection, #introspection?, #mutation, #name, #visible?
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, get_directives, #inherited, #remove_directive, remove_directive
Class Method Details
.coerce_input(value, ctx) ⇒ ActiveSupport::Duration?
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
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.})" end end |
.seconds_precision ⇒ Integer?
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
26 27 28 |
# File 'lib/graphql/types/iso_8601_duration.rb', line 26 def self.seconds_precision=(value) @seconds_precision = value end |