Sha256: ae31ae219f5df3bae6d02e9430ea59fce2f43c0f6ede276ea1a2892db135a2fb

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Types
    # This scalar takes `DateTime`s and transmits them as strings,
    # using ISO 8601 format.
    #
    # To use it, require it in your project:
    #
    #     require "graphql/types/iso_8601_date_time"
    #
    # Then use it for fields or arguments:
    #
    #     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.
    class ISO8601DateTime < GraphQL::Schema::Scalar
      description "An ISO 8601-encoded datetime"

      # @param value [DateTime]
      # @return [String]
      def self.coerce_result(value, _ctx)
        value.iso8601
      end

      # @param str_value [String]
      # @return [DateTime]
      def self.coerce_input(str_value, _ctx)
        DateTime.iso8601(str_value)
      rescue ArgumentError
        # Invalid input
        nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-1.8.4 lib/graphql/types/iso_8601_date_time.rb
graphql-1.8.3 lib/graphql/types/iso_8601_date_time.rb