Class: GraphQL::Types::ISO8601DateTime

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

Overview

This scalar takes Times and transmits them as strings, using ISO 8601 format.

Use it for fields or arguments as follows:

field :created_at, GraphQL::Types::ISO8601DateTime, null: false

argument :deliver_at, GraphQL::Types::ISO8601DateTime, null: false

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

Constant Summary collapse

DEFAULT_TIME_PRECISION =

It's not compatible with Rails' default, i.e. ActiveSupport::JSON::Encoder.time_precision (3 by default)

0

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 inherited from Schema::Member

authorizes?

Methods included from Schema::Member::BaseDSLMethods

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

Methods included from Schema::FindInheritedValue

extended, included

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

extended, #inherited

Methods included from Schema::Member::HasDirectives

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

Class Method Details

.coerce_input(str_value, _ctx) ⇒ Object

Parameters

  • str_value (String)

Returns

  • Time

:call-seq:

coerce_input(String str_value, _ctx) -> Time

: (untyped str_value, GraphQL::Query::Context _ctx) -> ::Time?



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/graphql/types/iso_8601_date_time.rb', line 86

def self.coerce_input(str_value, _ctx)
  Time.iso8601(str_value)
rescue ArgumentError, TypeError
  begin
    dt = Date.iso8601(str_value).to_time
    # For compatibility, continue accepting dates given without times
    # But without this, it would zero out given any time part of `str_value` (hours and/or minutes)
    if dt.iso8601.start_with?(str_value)
      dt
    elsif str_value.length == 8 && str_value.match?(/\A\d{8}\Z/)
      # Allow dates that are missing the "-". eg. "20220404"
      dt
    else
      nil
    end
  rescue ArgumentError, TypeError
    # Invalid input
    nil
  end
end

.coerce_result(value, _ctx) ⇒ Object

Parameters

  • value (Time, Date, DateTime, String)

Returns

  • String

:call-seq:

coerce_result(Time | Date | DateTime | String value, _ctx) -> String

: (untyped value, GraphQL::Query::Context _ctx) -> ::String



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphql/types/iso_8601_date_time.rb', line 61

def self.coerce_result(value, _ctx)
  case value
  when Date
    return value.to_time.iso8601(time_precision)
  when ::String
    return Time.parse(value).iso8601(time_precision)
  else
    # Time, DateTime or compatible is given:
    return value.iso8601(time_precision)
  end
rescue StandardError => error
  raise GraphQL::Error, "An incompatible object (#{value.class}) was given to #{self}. Make sure that only Times, Dates, DateTimes, and well-formatted Strings are used with this type. (#{error.message})"
end

.time_precision ⇒ Object

Returns

  • Integer

:call-seq:

time_precision() -> Integer

: -> Integer



35
36
37
# File 'lib/graphql/types/iso_8601_date_time.rb', line 35

def self.time_precision
  @time_precision || DEFAULT_TIME_PRECISION
end

.time_precision=(value) ⇒ Object

Parameters

  • value (Integer)

:call-seq:

time_precision=(Integer value)

: (Integer value) -> void



46
47
48
# File 'lib/graphql/types/iso_8601_date_time.rb', line 46

def self.time_precision=(value)
  @time_precision = value #: Integer?
end