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

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 DefinitionHelpers::DefinedByConfig

included

Methods included from DefinitionHelpers::NonNullWithBang

#!

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/graphql/input_object_type.rb', line 11

def description
  @description
end

#input_fieldsObject Also known as: arguments

Returns the value of attribute input_fields.



11
12
13
# File 'lib/graphql/input_object_type.rb', line 11

def input_fields
  @input_fields
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/graphql/input_object_type.rb', line 11

def name
  @name
end

Instance Method Details

#coerce_non_null_input(value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/graphql/input_object_type.rb', line 44

def coerce_non_null_input(value)
  input_values = {}

  input_fields.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



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

def kind
  GraphQL::TypeKinds::INPUT_OBJECT
end

#validate_non_null_input(input) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/graphql/input_object_type.rb', line 23

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

  # Items in the input that are unexpected
  input.each do |name, value|
    if input_fields[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 = input_fields.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