123456789_123456789_123456789_123456789_123456789_

Class: GraphQL::Client::Schema::PossibleTypes

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, BaseType
Inherits: Object
Defined in: lib/graphql/client/schema/possible_types.rb

Class Method Summary

Instance Attribute Summary

BaseType - Included

#schema_module

Internal: Get owner schema Module container.

#type

Public: Get associated GraphQL::BaseType with for this class.

Instance Method Summary

BaseType - Included

#cast

Internal: Cast JSON value to wrapped value.

#to_list_type

Internal: Get list wrapper of this type class.

#to_non_null_type

Internal: Get non-nullable wrapper of this type class.

Constructor Details

.new(type, types) ⇒ PossibleTypes

[ GitHub ]

  
# File 'lib/graphql/client/schema/possible_types.rb', line 13

def initialize(type, types)
  @type = type

  unless types.is_a?(Enumerable)
    raise TypeError, "expected types to be Enumerable, but was #{types.class}"
  end

  @possible_types = {}
  types.each do |klass|
    unless klass.is_a?(ObjectType)
      raise TypeError, "expected type to be #{ObjectType}, but was #{type.class}"
    end
    @possible_types[klass.type.graphql_name] = klass
  end
end

Instance Attribute Details

#possible_types (readonly)

[ GitHub ]

  
# File 'lib/graphql/client/schema/possible_types.rb', line 29

attr_reader :possible_types

Instance Method Details

#cast(value, errors)

Internal: Cast JSON value to wrapped value.

value - JSON value errors - ::GraphQL::Client::Errors instance

Returns BaseType instance.

[ GitHub ]

  
# File 'lib/graphql/client/schema/possible_types.rb', line 37

def cast(value, errors)
  case value
  when Hash
    typename = value["__typename"]
    if type = possible_types[typename]
      type.cast(value, errors)
    else
      raise InvariantError, "expected value to be one of (#{possible_types.keys.join(", ")}), but was #{typename.inspect}"
    end
  when NilClass
    nil
  else
    raise InvariantError, "expected value to be a Hash, but was #{value.class}"
  end
end