Class: GraphQL::InputObjectType

Inherits:
BaseType
  • Object
show all
Defined in:
lib/graphql/input_object_type.rb

Overview

InputObjectTypes are key-value inputs for fields.

Input objects have arguments which are identical to Field arguments. They map names to types and support default values. Their input types can be any input types, including InputObjectTypes.

In a resolve function, you can access the values by making nested lookups on args.

Examples:

An input type with name and number

PlayerInput = GraphQL::InputObjectType.define do
  name("Player")
  argument :name, !types.String
  argument :number, !types.Int
end

Accessing input values in a resolve function

resolve ->(obj, args, ctx) {
  args[:player][:name]    # => "Tony Gwynn"
  args[:player][:number]  # => 19
  args[:player].to_h      # { "name" => "Tony Gwynn", "number" => 19 }
  # ...
}

Instance Attribute Summary collapse

Attributes inherited from BaseType

#default_relay, #default_scalar, #description, #introspection, #name

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #coerce_isolated_input, #coerce_isolated_result, #connection_type, #default_relay?, #default_scalar?, #define_connection, #define_edge, #edge_type, #get_field, #introspection?, resolve_related_type, #resolve_type, #to_definition, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #valid_isolated_input?, #validate_input, #validate_isolated_input

Methods included from Define::InstanceDefinable

#define, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeInputObjectType

Returns a new instance of InputObjectType



44
45
46
47
# File 'lib/graphql/input_object_type.rb', line 44

def initialize
  super
  @arguments = {}
end

Instance Attribute Details

#argumentsHash<String => GraphQL::Argument> Also known as: input_fields

Returns Map String argument names to their Argument implementations

Returns:



40
41
42
# File 'lib/graphql/input_object_type.rb', line 40

def arguments
  @arguments
end

#mutationGraphQL::Relay::Mutation?

Returns The mutation this field was derived from, if it was derived from a mutation

Returns:



37
38
39
# File 'lib/graphql/input_object_type.rb', line 37

def mutation
  @mutation
end

Instance Method Details

#coerce_result(value, ctx = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/graphql/input_object_type.rb', line 58

def coerce_result(value, ctx = nil)
  if ctx.nil?
    warn_deprecated_coerce("coerce_isolated_result")
    ctx = GraphQL::Query::NullContext
  end

  # Allow the application to provide values as :symbols, and convert them to the strings
  value = value.reduce({}) { |memo, (k, v)| memo[k.to_s] = v; memo }

  result = {}

  arguments.each do |input_key, input_field_defn|
    input_value = value[input_key]
    if value.key?(input_key)
      result[input_key] = if input_value.nil?
        nil
      else
        input_field_defn.type.coerce_result(input_value, ctx)
      end
    end
  end

  result
end

#initialize_copy(other) ⇒ Object



49
50
51
52
# File 'lib/graphql/input_object_type.rb', line 49

def initialize_copy(other)
  super
  @arguments = other.arguments.dup
end

#kindObject



54
55
56
# File 'lib/graphql/input_object_type.rb', line 54

def kind
  GraphQL::TypeKinds::INPUT_OBJECT
end