Sha256: d75bb15fe689c87e278247630f49a25d19f6745ab2a9831552703967e509e7c0

Contents?: true

Size: 1.19 KB

Versions: 24

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Types
    # This scalar takes `Date`s and transmits them as strings,
    # using ISO 8601 format.
    #
    # Use it for fields or arguments as follows:
    #
    #     field :published_at, GraphQL::Types::ISO8601Date, null: false
    #
    #     argument :deliver_at, GraphQL::Types::ISO8601Date, null: false
    #
    # Alternatively, use this built-in scalar as inspiration for your
    # own Date type.
    class ISO8601Date < GraphQL::Schema::Scalar
      description "An ISO 8601-encoded date"

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

      # @param str_value [String, Date, DateTime, Time]
      # @return [Date]
      def self.coerce_input(value, ctx)
        if value.is_a?(::Date)
          value
        elsif value.is_a?(::DateTime)
          value.to_date
        elsif value.is_a?(::Time)
          value.to_date
        else
          Date.iso8601(value)
        end
      rescue ArgumentError, TypeError
        err = GraphQL::DateEncodingError.new(value)
        ctx.schema.type_error(err, ctx)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
graphql-1.13.23 lib/graphql/types/iso_8601_date.rb
graphql-1.13.22 lib/graphql/types/iso_8601_date.rb
graphql-1.13.21 lib/graphql/types/iso_8601_date.rb
graphql-1.13.20 lib/graphql/types/iso_8601_date.rb
graphql-1.13.19 lib/graphql/types/iso_8601_date.rb
graphql-1.13.18 lib/graphql/types/iso_8601_date.rb
graphql-1.13.17 lib/graphql/types/iso_8601_date.rb
graphql-1.13.16 lib/graphql/types/iso_8601_date.rb
graphql-1.13.15 lib/graphql/types/iso_8601_date.rb
graphql-1.13.14 lib/graphql/types/iso_8601_date.rb
graphql-1.13.13 lib/graphql/types/iso_8601_date.rb
graphql-1.13.12 lib/graphql/types/iso_8601_date.rb
graphql-2.0.5 lib/graphql/types/iso_8601_date.rb
graphql-2.0.4 lib/graphql/types/iso_8601_date.rb
graphql-2.0.3 lib/graphql/types/iso_8601_date.rb
graphql-1.13.11 lib/graphql/types/iso_8601_date.rb
graphql-2.0.2 lib/graphql/types/iso_8601_date.rb
graphql-2.0.1 lib/graphql/types/iso_8601_date.rb
graphql-1.13.10 lib/graphql/types/iso_8601_date.rb
graphql-2.0.0 lib/graphql/types/iso_8601_date.rb