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

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

Instance Method Summary collapse

Methods included from BaseType::ModifiesAnotherType

#unwrap

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, #initialize_copy, #introspection?, resolve_related_type, #resolve_type, #to_definition, #to_list_type, #to_non_null_type, #unwrap, #valid_input?, #valid_isolated_input?, #validate_input, #validate_isolated_input

Methods included from Define::InstanceDefinable

#define, #initialize_copy, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initialize(of_type:) ⇒ ListType

Returns a new instance of ListType.



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

def initialize(of_type:)
  super()
  @of_type = of_type
end

Instance Attribute Details

#of_typeObject (readonly)

Returns the value of attribute of_type.



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

def of_type
  @of_type
end

Instance Method Details

#coerce_result(value, ctx = nil) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/graphql/list_type.rb', line 44

def coerce_result(value, ctx = nil)
  if ctx.nil?
    warn_deprecated_coerce("coerce_isolated_result")
    ctx = GraphQL::Query::NullContext
  end
  ensure_array(value).map { |item| item.nil? ? nil : of_type.coerce_result(item, ctx) }
end

#kindObject



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

def kind
  GraphQL::TypeKinds::LIST
end

#to_sObject Also known as: inspect



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

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