Class: GraphQL::Stitching::Supergraph

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

Constant Summary collapse

LOCATION =
"__super"
INTROSPECTION_TYPES =
[
  "__Schema",
  "__Type",
  "__Field",
  "__Directive",
  "__EnumValue",
  "__InputValue",
  "__TypeKind",
  "__DirectiveLocation",
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, fields:, boundaries:, executables: {}) ⇒ Supergraph

Returns a new instance of Supergraph.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/graphql/stitching/supergraph.rb', line 20

def initialize(schema:, fields:, boundaries:, executables: {})
  @schema = schema
  @boundaries = boundaries
  @locations_by_type_and_field = INTROSPECTION_TYPES.each_with_object(fields) do |type_name, memo|
    introspection_type = schema.get_type(type_name)
    next unless introspection_type.kind.fields?

    memo[type_name] = introspection_type.fields.keys.each_with_object({}) do |field_name, m|
      m[field_name] = [LOCATION]
    end
  end

  @possible_keys_by_type = {}
  @possible_keys_by_type_and_location = {}
  @executables = { LOCATION => @schema }.merge!(executables)
end

Instance Attribute Details

#boundariesObject (readonly)

Returns the value of attribute boundaries.



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

def boundaries
  @boundaries
end

#executablesObject (readonly)

Returns the value of attribute executables.



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

def executables
  @executables
end

#locations_by_type_and_fieldObject (readonly)

Returns the value of attribute locations_by_type_and_field.



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

def locations_by_type_and_field
  @locations_by_type_and_field
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Class Method Details

.from_export(schema, delegation_map, executables: {}) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/graphql/stitching/supergraph.rb', line 48

def self.from_export(schema, delegation_map, executables: {})
  schema = GraphQL::Schema.from_definition(schema) if schema.is_a?(String)
  new(
    schema: schema,
    fields: delegation_map["fields"],
    boundaries: delegation_map["boundaries"],
    executables: executables,
  )
end

Instance Method Details

#assign_executable(location, executable = nil, &block) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/graphql/stitching/supergraph.rb', line 58

def assign_executable(location, executable = nil, &block)
  executable ||= block
  unless executable.is_a?(Class) && executable <= GraphQL::Schema
    raise StitchingError, "A client or block handler must be provided." unless executable
    raise StitchingError, "A client must be callable" unless executable.respond_to?(:call)
  end
  @executables[location] = executable
end

#execute_at_location(location, query, variables, context) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/graphql/stitching/supergraph.rb', line 67

def execute_at_location(location, query, variables, context)
  executable = executables[location]

  if executable.nil?
    raise StitchingError, "No executable assigned for #{location} location."
  elsif executable.is_a?(Class) && executable <= GraphQL::Schema
    executable.execute(
      query: query,
      variables: variables,
      context: context.frozen? ? context.dup : context,
      validate: false,
    )
  elsif executable.respond_to?(:call)
    executable.call(location, query, variables, context)
  else
    raise StitchingError, "Missing valid executable for #{location} location."
  end
end

#exportObject



41
42
43
44
45
46
# File 'lib/graphql/stitching/supergraph.rb', line 41

def export
  return GraphQL::Schema::Printer.print_schema(@schema), {
    "fields" => fields,
    "boundaries" => @boundaries,
  }
end

#fieldsObject



37
38
39
# File 'lib/graphql/stitching/supergraph.rb', line 37

def fields
  @locations_by_type_and_field.reject { |k, _v| INTROSPECTION_TYPES.include?(k) }
end

#fields_by_type_and_locationObject

inverts fields map to provide fields for a type/location “Type” => “location” => [“field1”, “field2”, …]



88
89
90
91
92
93
94
95
96
97
# File 'lib/graphql/stitching/supergraph.rb', line 88

def fields_by_type_and_location
  @fields_by_type_and_location ||= @locations_by_type_and_field.each_with_object({}) do |(type_name, fields), memo|
    memo[type_name] = fields.each_with_object({}) do |(field_name, locations), memo|
      locations.each do |location|
        memo[location] ||= []
        memo[location] << field_name
      end
    end
  end
end

#locations_by_typeObject

{ “Type” => [“location1”, “location2”, …] }



100
101
102
103
104
# File 'lib/graphql/stitching/supergraph.rb', line 100

def locations_by_type
  @locations_by_type ||= @locations_by_type_and_field.each_with_object({}) do |(type_name, fields), memo|
    memo[type_name] = fields.values.flatten.uniq
  end
end

#possible_keys_for_type(type_name) ⇒ Object

collects all possible boundary keys for a given type { “Type” => [“id”, …] }



108
109
110
111
112
113
114
# File 'lib/graphql/stitching/supergraph.rb', line 108

def possible_keys_for_type(type_name)
  @possible_keys_by_type[type_name] ||= begin
    keys = @boundaries[type_name].map { _1["selection"] }
    keys.uniq!
    keys
  end
end

#possible_keys_for_type_and_location(type_name, location) ⇒ Object

collects possible boundary keys for a given type and location (“Type”, “location”) => [“id”, …]



118
119
120
121
122
123
124
# File 'lib/graphql/stitching/supergraph.rb', line 118

def possible_keys_for_type_and_location(type_name, location)
  possible_keys_by_type = @possible_keys_by_type_and_location[type_name] ||= {}
  possible_keys_by_type[location] ||= begin
    location_fields = fields_by_type_and_location[type_name][location] || []
    location_fields & possible_keys_for_type(type_name)
  end
end

#route_type_to_locations(type_name, start_location, goal_locations) ⇒ Object

For a given type, route from one origin location to one or more remote locations used to connect a partial type across locations via boundary queries



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/graphql/stitching/supergraph.rb', line 128

def route_type_to_locations(type_name, start_location, goal_locations)
  if possible_keys_for_type(type_name).length > 1
    # multiple keys use an a-star search to traverse intermediary locations
    return route_type_to_locations_via_search(type_name, start_location, goal_locations)
  end

  # types with a single key attribute must all be within a single hop of each other,
  # so can use a simple match to collect boundaries for the goal locations.
  @boundaries[type_name].each_with_object({}) do |boundary, memo|
    if goal_locations.include?(boundary["location"])
      memo[boundary["location"]] = [boundary]
    end
  end
end