class GraphQL::ListType

A list type modifies another type.

List types can be created with the type helper (`types`) 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.

@example A field which returns a list of items

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

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

@example 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

Given a list type, you can always get the underlying type with {#unwrap}.

Attributes

of_type[R]

Public Class Methods

new(of_type:) click to toggle source
Calls superclass method GraphQL::BaseType::new
# File lib/graphql/list_type.rb, line 30
def initialize(of_type:)
  super()
  @of_type = of_type
end

Public Instance Methods

coerce_result(value, ctx = nil) click to toggle source
# 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
inspect()
Alias for: to_s
kind() click to toggle source
# File lib/graphql/list_type.rb, line 35
def kind
  GraphQL::TypeKinds::LIST
end
list?() click to toggle source
# File lib/graphql/list_type.rb, line 53
def list?
  true
end
to_s() click to toggle source
# File lib/graphql/list_type.rb, line 39
def to_s
  "[#{of_type.to_s}]"
end
Also aliased as: inspect, to_type_signature
to_type_signature()
Alias for: to_s

Private Instance Methods

coerce_non_null_input(value, ctx) click to toggle source
# File lib/graphql/list_type.rb, line 59
def coerce_non_null_input(value, ctx)
  ensure_array(value).map { |item| of_type.coerce_input(item, ctx) }
end
ensure_array(value) click to toggle source
# File lib/graphql/list_type.rb, line 76
def ensure_array(value)
  value.is_a?(Array) ? value : [value]
end
validate_non_null_input(value, ctx) click to toggle source
# File lib/graphql/list_type.rb, line 63
def validate_non_null_input(value, ctx)
  result = GraphQL::Query::InputValidationResult.new

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

  result
end