Class: GraphQL::ListType

Inherits:
BaseType show all
Includes:
BaseType::ModifiesAnotherType
Defined in:
lib/graphql/list_type.rb

Overview

A list type modifies another type.

List types can be created with the type helper (types[InnerType]) or BaseType#to_list_type (InnerType.to_list_type)

For return types, it says that the returned value will be a list of the modified.

For input types, it says that the incoming value will be a list of the modified type.

Given a list type, you can always get the underlying type with BaseType::ModifiesAnotherType#unwrap.

Examples:

A field which returns a list of items

field :items, types[ItemType]
# or
field :items, ItemType.to_list_type

A field which accepts a list of strings

field :newNames do
  # ...
  argument :values, types[types.String]
  # or
  argument :values, types.String.to_list_type
end

Instance Attribute Summary collapse

Attributes inherited from BaseType

#description

Instance Method Summary collapse

Methods included from BaseType::ModifiesAnotherType

#unwrap

Methods inherited from BaseType

#==, #coerce_input, #connection_type, #define_connection, #define_edge, #edge_type, #get_field, resolve_related_type, #resolve_type, #to_list_type, #to_non_null_type, #unwrap, #valid_input?, #validate_input

Methods included from Define::InstanceDefinable

#define, #definition_proc=, #metadata

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initialize(of_type:) ⇒ ListType

Returns a new instance of ListType.



29
30
31
32
# File 'lib/graphql/list_type.rb', line 29

def initialize(of_type:)
  @name = "List"
  @of_type = of_type
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/graphql/list_type.rb', line 28

def name
  @name
end

#of_typeObject (readonly)

Returns the value of attribute of_type.



28
29
30
# File 'lib/graphql/list_type.rb', line 28

def of_type
  @of_type
end

Instance Method Details

#coerce_non_null_input(value) ⇒ Object



55
56
57
# File 'lib/graphql/list_type.rb', line 55

def coerce_non_null_input(value)
  ensure_array(value).map { |item| of_type.coerce_input(item) }
end

#coerce_result(value) ⇒ Object



59
60
61
# File 'lib/graphql/list_type.rb', line 59

def coerce_result(value)
  ensure_array(value).map { |item| item.nil? ? nil : of_type.coerce_result(item) }
end

#kindObject



34
35
36
# File 'lib/graphql/list_type.rb', line 34

def kind
  GraphQL::TypeKinds::LIST
end

#to_sObject



38
39
40
# File 'lib/graphql/list_type.rb', line 38

def to_s
  "[#{of_type.to_s}]"
end

#validate_non_null_input(value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql/list_type.rb', line 42

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

  ensure_array(value).each_with_index do |item, index|
    item_result = of_type.validate_input(item)
    if !item_result.valid?
      result.merge_result!(index, item_result)
    end
  end

  result
end