Sha256: dbe0f26665f8da4ced7af8ecee6646e7d75502e17fdc05cff49a9eedb7b9b70c

Contents?: true

Size: 990 Bytes

Versions: 5

Compression:

Stored size: 990 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, :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

5 entries across 5 versions & 1 rubygems

Version Path
graphql-0.9.4 lib/graphql/query/arguments.rb
graphql-0.9.3 lib/graphql/query/arguments.rb
graphql-0.9.2 lib/graphql/query/arguments.rb
graphql-0.8.1 lib/graphql/query/arguments.rb
graphql-0.8.0 lib/graphql/query/arguments.rb