Sha256: 973f88a4256b8709cc28a7272d47a2ae4fed781b347d2b9e9c5a5d4bb895ec86

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require 'money'
require 'graphql'

module GraphQL
  module Types
    ISO4217 = GraphQL::EnumType.define do
      name 'ISO4217'
      description 'A valid ISO 4217 currency code string.'

      ::Money::Currency.all.uniq(&:name).sort_by(&:iso_code).each do |curr|
        value curr.iso_code, curr.name
      end
    end

    Currency = GraphQL::ObjectType.define do
      name 'Currency'
      description 'A currency as defined by the ISO 4217 standard.'

      field :isoCode, ISO4217, 'The currency format as defined by IS0 4217.' do
        resolve ->(obj, *) { obj.iso_code }
      end

      field :symbol, types.String, 'The symbol for the currency (i.e. "€").'

      field :smallestDenomination do
        type types.Int
        description 'The smallest denomination of the currency.'
        resolve ->(obj, *) { obj.smallest_denomination }
      end

      field :subunitToUnit do
        type types.Int
        description 'Factor used to convert a subunit to a unit.'
        resolve ->(obj, *) { obj.subunit_to_unit }
      end
    end

    Money = GraphQL::ObjectType.define do
      name 'Money'
      description 'An object representing money, with an amount and currency.'

      field :fractional, types.Int, 'Fractional unit value of a given currency.'

      field :amount, types.Float, 'Numerical amount of the money.'

      field :currency, Currency

      field :displayString, types.String, 'Displayable string (i.e. "$1.00").' do
        resolve ->(obj, *) { obj.format }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-types-money-0.2.0 lib/graphql/types/money.rb
graphql-types-money-0.1.0 lib/graphql/types/money.rb