Class: GraphQL::InputObjectType

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

Overview

A complex input type for a field argument.

Examples:

An input type with name and number

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

Instance Attribute Summary collapse

Attributes inherited from BaseType

#description, #name

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #resolve_type, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #validate_input

Methods included from Define::InstanceDefinable

included

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeInputObjectType

Returns a new instance of InputObjectType.



22
23
24
# File 'lib/graphql/input_object_type.rb', line 22

def initialize
  @arguments = {}
end

Instance Attribute Details

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

Returns Map String argument names to their Argument implementations.

Returns:



18
19
20
# File 'lib/graphql/input_object_type.rb', line 18

def arguments
  @arguments
end

Instance Method Details

#coerce_non_null_input(value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/graphql/input_object_type.rb', line 51

def coerce_non_null_input(value)
  input_values = {}

  arguments.each do |input_key, input_field_defn|
    field_value = value[input_key]
    field_value = input_field_defn.type.coerce_input(field_value)

    # Try getting the default value
    if field_value.nil?
      field_value = input_field_defn.default_value
    end

    if !field_value.nil?
      input_values[input_key] = field_value
    end
  end

  GraphQL::Query::Arguments.new(input_values)
end

#kindObject



26
27
28
# File 'lib/graphql/input_object_type.rb', line 26

def kind
  GraphQL::TypeKinds::INPUT_OBJECT
end

#validate_non_null_input(input) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql/input_object_type.rb', line 30

def validate_non_null_input(input)
  result = GraphQL::Query::InputValidationResult.new

  # Items in the input that are unexpected
  input.each do |name, value|
    if arguments[name].nil?
      result.add_problem("Field is not defined on #{self.name}", [name])
    end
  end

  # Items in the input that are expected, but have invalid values
  invalid_fields = arguments.map do |name, field|
    field_result = field.type.validate_input(input[name])
    if !field_result.valid?
      result.merge_result!(name, field_result)
    end
  end

  result
end