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



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/graphql/stitching/util.rb', line 98

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/graphql/stitching/util.rb', line 69

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



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

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)


35
36
37
# File 'lib/graphql/stitching/util.rb', line 35

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

.unwrap_non_null(type) ⇒ Object

strips non-null wrappers from a type



40
41
42
43
# File 'lib/graphql/stitching/util.rb', line 40

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