Class: ElasticGraph::GraphQL::ScalarCoercionAdapters::DateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/graphql/scalar_coercion_adapters/date_time.rb

Constant Summary collapse

PRECISION =

millisecond precision

3

Class Method Summary collapse

Class Method Details

.coerce_input(value, ctx) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/elastic_graph/graphql/scalar_coercion_adapters/date_time.rb', line 17

def self.coerce_input(value, ctx)
  return value if value.nil?

  time = ::Time.iso8601(value)

  # Verify we do not have more than 4 digits for the year. The datastore `strict_date_time` format we use only supports 4 digit years:
  #
  # > Most of the below formats have a `strict` companion format, which means that year, month and day parts of the
  # > week must use respectively 4, 2 and 2 digits exactly, potentially prepending zeros.
  #
  # https://www.elastic.co/guide/en/elasticsearch/reference/8.12/mapping-date-format.html#built-in-date-formats
  raise_coercion_error(value) if time.year > 9999

  # We ultimately wind up passing input args to the datastore as our GraphQL engine receives
  # them (it doesn't do any formatting of DateTime args to what the datastore needs) so we do
  # that here instead. We have configured the datastore to expect DateTimes in `strict_date_time`
  # format, so here we convert it to that format (which is just ISO8601 format). Ultimately,
  # that means that this method just "roundtrips" the input string back to a string, but it validates
  # the string is formatted correctly and returns a string in the exact format we need for the datastore.
  time.iso8601(PRECISION)
rescue ::ArgumentError, ::TypeError
  raise_coercion_error(value)
end

.coerce_result(value, ctx) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/elastic_graph/graphql/scalar_coercion_adapters/date_time.rb', line 41

def self.coerce_result(value, ctx)
  case value
  when ::Time
    value.iso8601(PRECISION)
  when ::String
    ::Time.iso8601(value).iso8601(PRECISION)
  end
rescue ::ArgumentError
  nil
end