Class: GraphQL::Stitching::Supergraph
- Inherits:
-
Object
- Object
- GraphQL::Stitching::Supergraph
- 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
-
#boundaries ⇒ Object
readonly
Returns the value of attribute boundaries.
-
#executables ⇒ Object
readonly
Returns the value of attribute executables.
-
#locations_by_type_and_field ⇒ Object
readonly
Returns the value of attribute locations_by_type_and_field.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
- .from_export(schema:, delegation_map:, executables:) ⇒ Object
- .validate_executable!(location, executable) ⇒ Object
Instance Method Summary collapse
- #execute_at_location(location, source, variables, context) ⇒ Object
- #export ⇒ Object
- #fields ⇒ Object
-
#fields_by_type_and_location ⇒ Object
inverts fields map to provide fields for a type/location “Type” => “location” => [“field1”, “field2”, …].
-
#initialize(schema:, fields:, boundaries:, executables:) ⇒ Supergraph
constructor
A new instance of Supergraph.
- #locations ⇒ Object
-
#locations_by_type ⇒ Object
“Type” => [“location1”, “location2”, …].
-
#possible_keys_for_type(type_name) ⇒ Object
collects all possible boundary keys for a given type (“Type”) => [“id”, …].
-
#possible_keys_for_type_and_location(type_name, location) ⇒ Object
collects possible boundary keys for a given type and location (“Type”, “location”) => [“id”, …].
-
#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.
Constructor Details
#initialize(schema:, fields:, boundaries:, executables:) ⇒ Supergraph
Returns a new instance of Supergraph.
44 45 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/supergraph.rb', line 44 def initialize(schema:, fields:, boundaries:, executables:) @schema = schema @boundaries = boundaries @possible_keys_by_type = {} @possible_keys_by_type_and_location = {} # add introspection types into the fields mapping @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.freeze # validate and normalize executable references @executables = executables.each_with_object({ LOCATION => @schema }) do |(location, executable), memo| if self.class.validate_executable!(location, executable) memo[location.to_s] = executable end end.freeze end |
Instance Attribute Details
#boundaries ⇒ Object (readonly)
Returns the value of attribute boundaries.
42 43 44 |
# File 'lib/graphql/stitching/supergraph.rb', line 42 def boundaries @boundaries end |
#executables ⇒ Object (readonly)
Returns the value of attribute executables.
42 43 44 |
# File 'lib/graphql/stitching/supergraph.rb', line 42 def executables @executables end |
#locations_by_type_and_field ⇒ Object (readonly)
Returns the value of attribute locations_by_type_and_field.
42 43 44 |
# File 'lib/graphql/stitching/supergraph.rb', line 42 def locations_by_type_and_field @locations_by_type_and_field end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
42 43 44 |
# File 'lib/graphql/stitching/supergraph.rb', line 42 def schema @schema end |
Class Method Details
.from_export(schema:, delegation_map:, executables:) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/graphql/stitching/supergraph.rb', line 24 def self.from_export(schema:, delegation_map:, executables:) schema = GraphQL::Schema.from_definition(schema) if schema.is_a?(String) executables = delegation_map["locations"].each_with_object({}) do |location, memo| executable = executables[location] || executables[location.to_sym] if validate_executable!(location, executable) memo[location] = executable end end new( schema: schema, fields: delegation_map["fields"], boundaries: delegation_map["boundaries"], executables: executables, ) end |
.validate_executable!(location, executable) ⇒ Object
18 19 20 21 22 |
# File 'lib/graphql/stitching/supergraph.rb', line 18 def self.validate_executable!(location, executable) return true if executable.is_a?(Class) && executable <= GraphQL::Schema return true if executable && executable.respond_to?(:call) raise StitchingError, "Invalid executable provided for location `#{location}`." end |
Instance Method Details
#execute_at_location(location, source, variables, context) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/graphql/stitching/supergraph.rb', line 84 def execute_at_location(location, source, 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: source, variables: variables, context: context.frozen? ? context.dup : context, validate: false, ) elsif executable.respond_to?(:call) executable.call(location, source, variables, context) else raise StitchingError, "Missing valid executable for #{location} location." end end |
#export ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/graphql/stitching/supergraph.rb', line 76 def export return GraphQL::Schema::Printer.print_schema(@schema), { "locations" => locations, "fields" => fields, "boundaries" => @boundaries, } end |
#fields ⇒ Object
68 69 70 |
# File 'lib/graphql/stitching/supergraph.rb', line 68 def fields @locations_by_type_and_field.reject { |k, _v| INTROSPECTION_TYPES.include?(k) } end |
#fields_by_type_and_location ⇒ Object
inverts fields map to provide fields for a type/location “Type” => “location” => [“field1”, “field2”, …]
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/graphql/stitching/supergraph.rb', line 105 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 ⇒ Object
72 73 74 |
# File 'lib/graphql/stitching/supergraph.rb', line 72 def locations @executables.keys.reject { _1 == LOCATION } end |
#locations_by_type ⇒ Object
“Type” => [“location1”, “location2”, …]
117 118 119 120 121 |
# File 'lib/graphql/stitching/supergraph.rb', line 117 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”, …]
125 126 127 128 129 130 131 |
# File 'lib/graphql/stitching/supergraph.rb', line 125 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”, …]
135 136 137 138 139 140 141 |
# File 'lib/graphql/stitching/supergraph.rb', line 135 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/graphql/stitching/supergraph.rb', line 145 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 |