Module: GraphQL::Execution::Typecast

Defined in:
lib/graphql/execution/typecast.rb

Overview

GraphQL object {value, current_type} can be cast to potential_type when:

  • current_type == potential_type
  • current_type is a union and it contains potential_type
  • potential_type is a union and it contains current_type
  • current_type is an interface and potential_type implements it
  • potential_type is an interface and current_type implements it

Class Method Summary collapse

Class Method Details

.compatible?(current_type, potential_type, query_ctx) ⇒ Boolean

While value is exposed by GraphQL as an instance of current_type, should it also be treated as an instance of potential_type?

This is used for checking whether fragments apply to an object.

Parameters:

Returns:

  • (Boolean)

    true if value be evaluated as a potential_type



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graphql/execution/typecast.rb', line 20

def self.compatible?(current_type, potential_type, query_ctx)
  if current_type == potential_type
    true
  elsif current_type.kind.union?
    current_type.possible_types.include?(potential_type)
  elsif potential_type.kind.union?
    potential_type.include?(current_type)
  elsif current_type.kind.interface? && potential_type.kind.object?
    potential_type.interfaces.include?(current_type)
  elsif potential_type.kind.interface? && current_type.kind.object?
    current_type.interfaces.include?(potential_type)
  else
    false
  end
end