Class: GraphQL::Stitching::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/stitching/util.rb

Class Method Summary collapse

Class Method Details

.get_list_structure(type) ⇒ Object

gets a deep structural description of a list value type



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql/stitching/util.rb', line 24

def self.get_list_structure(type)
  structure = []
  previous = nil
  while type.respond_to?(:of_type)
    if type.is_a?(GraphQL::Schema::List)
      structure.push(previous.is_a?(GraphQL::Schema::NonNull) ? "non_null_list" : "list")
    end
    if structure.any?
      previous = type
      if !type.of_type.respond_to?(:of_type)
        structure.push(previous.is_a?(GraphQL::Schema::NonNull) ? "non_null_element" : "element")
      end
    end
    type = type.of_type
  end
  structure
end

.get_named_type(type) ⇒ Object

gets the named type at the bottom of a non-null/list wrapper tree



8
9
10
11
12
13
# File 'lib/graphql/stitching/util.rb', line 8

def self.get_named_type(type)
  while type.respond_to?(:of_type)
    type = type.of_type
  end
  type
end

.get_named_type_for_field_node(schema, parent_type, node) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/graphql/stitching/util.rb', line 62

def self.get_named_type_for_field_node(schema, parent_type, node)
  if node.name == "__schema" && parent_type == schema.query
    schema.types["__Schema"] # type mapped to phantom introspection field
  else
    Util.get_named_type(parent_type.fields[node.name].type)
  end
end

.get_possible_types(schema, parent_type) ⇒ Object

Gets all objects and interfaces that implement a given interface



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/graphql/stitching/util.rb', line 43

def self.get_possible_types(schema, parent_type)
  return [parent_type] unless parent_type.kind.abstract?
  return parent_type.possible_types if parent_type.kind.union?

  result = []
  schema.types.values.each do |type|
    next unless type <= GraphQL::Schema::Interface && type != parent_type
    next unless type.interfaces.include?(parent_type)
    result << type
    result.push(*get_possible_types(schema, type)) if type.kind.interface?
  end
  result.uniq
end

.is_leaf_type?(type) ⇒ Boolean

Specifies if a type is a leaf node (no children)

Returns:

  • (Boolean)


58
59
60
# File 'lib/graphql/stitching/util.rb', line 58

def self.is_leaf_type?(type)
  type.kind.scalar? || type.kind.enum?
end

.unwrap_non_null(type) ⇒ Object

strips non-null wrappers from a type



16
17
18
19
20
21
# File 'lib/graphql/stitching/util.rb', line 16

def self.unwrap_non_null(type)
  while type.is_a?(GraphQL::Schema::NonNull)
    type = type.of_type
  end
  type
end