Class: GraphQL::Query::Arguments

Inherits:
Object
  • Object
show all
Extended by:
Delegate
Defined in:
lib/graphql/query/arguments.rb

Overview

Read-only access to values, normalizing all keys to strings

Arguments recursively wraps the input in Arguments instances.

Defined Under Namespace

Classes: ArgumentValue

Constant Summary collapse

NO_ARGS =
self.new({}, argument_definitions: [])

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegate

def_delegators

Constructor Details

#initialize(values, argument_definitions:) ⇒ Arguments

Returns a new instance of Arguments.



57
58
59
60
61
62
63
64
65
66
# File 'lib/graphql/query/arguments.rb', line 57

def initialize(values, argument_definitions:)
  @argument_values = values.inject({}) do |memo, (inner_key, inner_value)|
    arg_defn = argument_definitions[inner_key.to_s]

    arg_value = wrap_value(inner_value, arg_defn.type)
    string_key = arg_defn.expose_as
    memo[string_key] = ArgumentValue.new(string_key, arg_value, arg_defn)
    memo
  end
end

Class Method Details

.construct_arguments_class(argument_owner) ⇒ Object



52
53
54
55
# File 'lib/graphql/query/arguments.rb', line 52

def self.construct_arguments_class(argument_owner)
  arguments_class = GraphQL::Query::StaticArguments.new(argument_definitions: argument_owner.arguments)
  argument_owner.arguments_class = arguments_class
end

Instance Method Details

#[](key) ⇒ Object

Returns the argument at that key.

Parameters:

  • key (String, Symbol)

    name or index of value to access

Returns:

  • (Object)

    the argument at that key



70
71
72
73
# File 'lib/graphql/query/arguments.rb', line 70

def [](key)
  key_s = key.is_a?(String) ? key : key.to_s
  @argument_values.fetch(key_s, NULL_ARGUMENT_VALUE).value
end

#each_value {|argument_value| ... } ⇒ Object

Access each key, value and type for the arguments in this set.

Yields:

Yield Parameters:



100
101
102
103
104
# File 'lib/graphql/query/arguments.rb', line 100

def each_value
  @argument_values.each_value do |argument_value|
    yield(argument_value)
  end
end

#key?(key) ⇒ Boolean

Returns true if the argument was present in this field.

Parameters:

  • key (String, Symbol)

    name of value to access

Returns:

  • (Boolean)

    true if the argument was present in this field



77
78
79
80
# File 'lib/graphql/query/arguments.rb', line 77

def key?(key)
  key_s = key.is_a?(String) ? key : key.to_s
  @argument_values.key?(key_s)
end

#to_hHash

Get the hash of all values, with stringified keys

Returns:

  • (Hash)

    the stringified hash



84
85
86
87
88
89
90
91
92
93
# File 'lib/graphql/query/arguments.rb', line 84

def to_h
  @to_h ||= begin
    h = {}
    each_value do |arg_value|
      arg_key = arg_value.definition.expose_as
      h[arg_key] = unwrap_value(arg_value.value)
    end
    h
  end
end