Sha256: 420f37e7d7936cc1a341ee5bca67330125e9cb37e983db69c853fe8d6a4e7bc3

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

# 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, :inspect, :to_h

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

  private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-0.9.5 lib/graphql/query/arguments.rb