Class: GraphQL::Stitching::Util

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

Class Method Summary collapse

Class Method Details

.expand_abstract_type(schema, parent_type) ⇒ Object

expands interfaces and unions to an array of their memberships like ‘schema.possible_types`, but includes child interfaces



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/graphql/stitching/util.rb', line 30

def self.expand_abstract_type(schema, parent_type)
  return [] 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(*expand_abstract_type(schema, type)) if type.kind.interface?
  end
  result.uniq
end

.get_list_structure(type) ⇒ Object

gets a deep structural description of a list value type



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/stitching/util.rb', line 45

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

.is_leaf_type?(type) ⇒ Boolean

specifies if a type is a primitive leaf value

Returns:

  • (Boolean)


7
8
9
# File 'lib/graphql/stitching/util.rb', line 7

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

.named_type_for_field_node(schema, parent_type, node) ⇒ Object

gets a named type for a field node, including hidden root introspections



20
21
22
23
24
25
26
# File 'lib/graphql/stitching/util.rb', line 20

def self.named_type_for_field_node(schema, parent_type, node)
  if node.name == "__schema" && parent_type == schema.query
    schema.types["__Schema"]
  else
    parent_type.fields[node.name].type.unwrap
  end
end

.unwrap_non_null(type) ⇒ Object

strips non-null wrappers from a type



12
13
14
15
16
17
# File 'lib/graphql/stitching/util.rb', line 12

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