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

#ast_node, #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, #default_relay?, #default_scalar?, #get_field, #initialize_copy, #introspection?, #non_null?, 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 Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

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



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

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

#list?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/graphql/list_type.rb', line 53

def list?
  true
end

#to_sObject Also known as: inspect, to_type_signature



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

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