Sha256: 2899c774924cc660882714aa1cf1101d6e226aff3bbb326b2afe820d18f1dc5d

Contents?: true

Size: 902 Bytes

Versions: 2

Compression:

Stored size: 902 Bytes

Contents

require 'forwardable'

# Provide read-only access to arguments by string or symbol names.
class GraphQL::Query::Arguments
  extend Forwardable

  def initialize(ast_arguments, argument_hash, variables)
    @hash = ast_arguments.reduce({}) do |memo, arg|
      arg_defn = argument_hash[arg.name]
      value = reduce_value(arg.value, arg_defn, variables)
      memo[arg.name] = value
      memo
    end
  end

  def_delegators :@hash, :keys, :values

  def [](key)
    @hash[key.to_s]
  end

  private

  def reduce_value(value, arg_defn, variables)
    if value.is_a?(GraphQL::Nodes::VariableIdentifier)
      value = variables[value.name]
    elsif value.is_a?(GraphQL::Nodes::Enum)
      value = arg_defn.type.coerce(value.name)
    elsif value.is_a?(GraphQL::Nodes::InputObject)
      value = self.class.new(value.pairs, arg_defn.type.input_fields, variables)
    else
      value
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-0.5.0 lib/graph_ql/query/arguments.rb
graphql-0.4.0 lib/graph_ql/query/arguments.rb