Module: GraphQL::Execution::Typecast Private

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

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.subtype?(parent_type, child_type) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/graphql/execution/typecast.rb', line 7

def self.subtype?(parent_type, child_type)
  if parent_type == child_type
    # Equivalent types are subtypes
    true
  elsif child_type.is_a?(GraphQL::NonNullType)
    # A non-null type is a subtype of a nullable type
    # if its inner type is a subtype of that type
    if parent_type.is_a?(GraphQL::NonNullType)
      subtype?(parent_type.of_type, child_type.of_type)
    else
      subtype?(parent_type, child_type.of_type)
    end
  else
    case parent_type
    when GraphQL::InterfaceType
      # A type is a subtype of an interface
      # if it implements that interface
      case child_type
      when GraphQL::ObjectType
        child_type.interfaces.include?(parent_type)
      else
        false
      end
    when GraphQL::UnionType
      # A type is a subtype of that union
      # if the union includes that type
      parent_type.possible_types.include?(child_type)
    when GraphQL::ListType
      # A list type is a subtype of another list type
      # if its inner type is a subtype of the other inner type
      case child_type
      when GraphQL::ListType
        subtype?(parent_type.of_type, child_type.of_type)
      else
        false
      end
    else
      false
    end
  end
end