Class: GraphQL::Stitching::Util

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

Defined Under Namespace

Classes: TypeStructure

Constant Summary collapse

GRAPHQL_VERSION =
GraphQL::VERSION.split(".").map(&:to_i).freeze

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphql/stitching/util.rb', line 61

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

.flatten_type_structure(type) ⇒ Object

builds a single-dimensional representation of a wrapped type structure



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/graphql/stitching/util.rb', line 37

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

.graphql_version?(major, minor = nil, patch = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def graphql_version?(major, minor = nil, patch = nil)
  result = GRAPHQL_VERSION[0] >= major
  result &&= GRAPHQL_VERSION[1] >= minor if minor
  result &&= GRAPHQL_VERSION[2] >= patch if patch
  result
end

.is_leaf_type?(type) ⇒ Boolean

specifies if a type is a primitive leaf value

Returns:

  • (Boolean)


26
27
28
# File 'lib/graphql/stitching/util.rb', line 26

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

.unwrap_non_null(type) ⇒ Object

strips non-null wrappers from a type



31
32
33
34
# File 'lib/graphql/stitching/util.rb', line 31

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