class GraphQL::Schema::InputObject

Attributes

arguments_class[RW]

@return [Class<GraphQL::Arguments>]

arguments[R]

@return [GraphQL::Query::Arguments] The underlying arguments instance

context[R]

@return [GraphQL::Query::Context] The context for this query

Public Class Methods

argument(*args) click to toggle source
Calls superclass method
# File lib/graphql/schema/input_object.rb, line 76
def argument(*args)
  argument_defn = super
  # Add a method access
  arg_name = argument_defn.graphql_definition.name
  define_method(Member::BuildType.underscore(arg_name)) do
    @arguments.public_send(arg_name)
  end
end
kind() click to toggle source
# File lib/graphql/schema/input_object.rb, line 101
def kind
  GraphQL::TypeKinds::INPUT_OBJECT
end
new(values, context:, defaults_used:) click to toggle source
# File lib/graphql/schema/input_object.rb, line 8
def initialize(values, context,, defaults_used))
  @context = context
  @arguments = self.class.arguments_class.new(values, context: context, defaults_used: defaults_used)
  # Symbolized, underscored hash:
  @ruby_style_hash = @arguments.to_kwargs
  # Apply prepares, not great to have it duplicated here.
  self.class.arguments.each do |name, arg_defn|
    ruby_kwargs_key = arg_defn.keyword
    if @ruby_style_hash.key?(ruby_kwargs_key) && arg_defn.prepare
      @ruby_style_hash[ruby_kwargs_key] = arg_defn.prepare_value(self, @ruby_style_hash[ruby_kwargs_key])
    end
  end
end
to_graphql() click to toggle source
# File lib/graphql/schema/input_object.rb, line 85
def to_graphql
  type_defn = GraphQL::InputObjectType.new
  type_defn.name = graphql_name
  type_defn.description = description
  type_defn.metadata[:type_class] = self
  type_defn.mutation = mutation
  arguments.each do |name, arg|
    type_defn.arguments[arg.graphql_definition.name] = arg.graphql_definition
  end
  # Make a reference to a classic-style Arguments class
  self.arguments_class = GraphQL::Query::Arguments.construct_arguments_class(type_defn)
  # But use this InputObject class at runtime
  type_defn.arguments_class = self
  type_defn
end

Public Instance Methods

[](key) click to toggle source

Lookup a key on this object, it accepts new-style underscored symbols Or old-style camelized identifiers. @param key [Symbol, String]

# File lib/graphql/schema/input_object.rb, line 55
def [](key)
  if @ruby_style_hash.key?(key)
    @ruby_style_hash[key]
  else
    @arguments[key]
  end
end
key?(key) click to toggle source
# File lib/graphql/schema/input_object.rb, line 63
def key?(key)
  @ruby_style_hash.key?(key) || @arguments.key?(key)
end
to_h() click to toggle source
# File lib/graphql/schema/input_object.rb, line 31
def to_h
  @ruby_style_hash.inject({}) do |h, (key, value)|
    h.merge(key => unwrap_value(value))
  end
end
to_kwargs() click to toggle source

A copy of the Ruby-style hash

# File lib/graphql/schema/input_object.rb, line 68
def to_kwargs
  @ruby_style_hash.dup
end
unwrap_value(value) click to toggle source
# File lib/graphql/schema/input_object.rb, line 37
def unwrap_value(value)
  case value
  when Array
    value.map { |item| unwrap_value(item) }
  when Hash
    value.inject({}) do |h, (key, value)|
      h.merge(key => unwrap_value(value))
    end
  when InputObject
    value.to_h
  else
    value
  end
end