Class: GraphQL::Stitching::Util

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

Overview

General utilities to aid with stitching.

Defined Under Namespace

Classes: TypeStructure

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



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/graphql/stitching/util.rb', line 81

def 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.each_value 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.tap(&:uniq!)
end

.flatten_ast_type_structure(ast, structure: []) ⇒ Object

builds a single-dimensional representation of a wrapped type structure from AST



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/graphql/stitching/util.rb', line 52

def flatten_ast_type_structure(ast, structure: [])
  null = true

  while ast.is_a?(GraphQL::Language::Nodes::NonNullType)
    ast = ast.of_type
    null = false
  end

  if ast.is_a?(GraphQL::Language::Nodes::ListType)
    structure << TypeStructure.new(
      list: true,
      null: null,
      name: nil,
    )

    flatten_ast_type_structure(ast.of_type, structure: structure)
  else
    structure << TypeStructure.new(
      list: false,
      null: null,
      name: ast.name,
    )
  end

  structure
end

.flatten_type_structure(type) ⇒ Object

builds a single-dimensional representation of a wrapped type structure



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql/stitching/util.rb', line 29

def flatten_type_structure(type)
  structure = []

  while type.list?
    structure << TypeStructure.new(
      list: true,
      null: !type.non_null?,
      name: nil,
    )

    type = unwrap_non_null(type).of_type
  end

  structure << TypeStructure.new(
    list: false,
    null: !type.non_null?,
    name: type.unwrap.graphql_name,
  )

  structure
end

.is_leaf_type?(type) ⇒ Boolean

specifies if a type is a primitive leaf value

Returns:

  • (Boolean)


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

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

.unwrap_non_null(type) ⇒ Object

strips non-null wrappers from a type



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

def unwrap_non_null(type)
  type = type.of_type while type.non_null?
  type
end