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

Inherits:
Object
  • Object
show all
Includes:
BaseType
Defined in:
lib/graphql/client/schema/possible_types.rb

Instance Attribute Summary collapse

Attributes included from BaseType

#schema_module, #type

Instance Method Summary collapse

Methods included from BaseType

#to_list_type, #to_non_null_type

Constructor Details

#initialize(type, types) ⇒ PossibleTypes

Returns a new instance of PossibleTypes.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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.name] = klass
  end
end

Instance Attribute Details

#possible_typesObject (readonly)

Returns the value of attribute possible_types.



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

def possible_types
  @possible_types
end

Instance Method Details

#cast(value, errors) ⇒ Object

Internal: Cast JSON value to wrapped value.

value - JSON value errors - Errors instance

Returns BaseType instance.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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