Class: GraphQL::Stitching::Supergraph
- Inherits:
-
Object
- Object
- GraphQL::Stitching::Supergraph
- Defined in:
- lib/graphql/stitching/supergraph.rb,
lib/graphql/stitching/supergraph/key_directive.rb,
lib/graphql/stitching/supergraph/to_definition.rb,
lib/graphql/stitching/supergraph/source_directive.rb,
lib/graphql/stitching/supergraph/resolver_directive.rb
Overview
Supergraph is the singuar representation of a stitched graph. It provides the combined GraphQL schema and delegation maps used to route selections across subgraph locations.
Defined Under Namespace
Classes: KeyDirective, PathNode, ResolverDirective, SourceDirective
Constant Summary collapse
- SUPERGRAPH_LOCATION =
"__super"
Instance Attribute Summary collapse
-
#executables ⇒ Hash<String, Executable>
readonly
A map of executable resources by location.
-
#locations_by_type_and_field ⇒ Object
readonly
Returns the value of attribute locations_by_type_and_field.
-
#resolvers ⇒ Object
readonly
Returns the value of attribute resolvers.
-
#schema ⇒ GraphQL::Schema
readonly
The composed schema for the supergraph.
Class Method Summary collapse
- .from_definition(schema, executables:) ⇒ Object
- .validate_executable!(location, executable) ⇒ Object
Instance Method Summary collapse
- #execute_at_location(location, source, variables, request) ⇒ 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: {}, resolvers: {}, executables: {}) ⇒ Supergraph
constructor
A new instance of Supergraph.
- #locations ⇒ Object
-
#locations_by_type ⇒ Object
"Type" => ["location1", "location2", ...].
- #memoized_introspection_types ⇒ Object
- #memoized_schema_fields(type_name) ⇒ Object
- #memoized_schema_types ⇒ Object
-
#possible_keys_for_type(type_name) ⇒ Object
collects all possible resolver keys for a given type ("Type") => [Key("id"), ...].
-
#possible_keys_for_type_and_location(type_name, location) ⇒ Object
collects possible resolver keys for a given type and location ("Type", "location") => [Key("id"), ...].
- #resolvers_by_version ⇒ Object
-
#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 resolver queries.
-
#static_validator ⇒ GraphQL::StaticValidation::Validator
Static validator for the supergraph schema.
- #to_definition ⇒ Object
Constructor Details
#initialize(schema:, fields: {}, resolvers: {}, executables: {}) ⇒ Supergraph
Returns a new instance of Supergraph.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graphql/stitching/supergraph.rb', line 21 def initialize(schema:, fields: {}, resolvers: {}, executables: {}) @schema = schema @schema.use(GraphQL::Schema::AlwaysVisible) @resolvers = resolvers @resolvers_by_version = nil @fields_by_type_and_location = nil @locations_by_type = nil @memoized_introspection_types = nil @memoized_schema_fields = {} @memoized_schema_types = nil @possible_keys_by_type = {} @possible_keys_by_type_and_location = {} @static_validator = nil # add introspection types into the fields mapping @locations_by_type_and_field = memoized_introspection_types.each_with_object(fields) do |(type_name, type), memo| next unless type.kind.fields? memo[type_name] = type.fields.keys.each_with_object({}) do |field_name, m| m[field_name] = [SUPERGRAPH_LOCATION] end end.freeze # validate and normalize executable references @executables = executables.each_with_object({ SUPERGRAPH_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
#executables ⇒ Hash<String, Executable> (readonly)
Returns a map of executable resources by location.
17 18 19 |
# File 'lib/graphql/stitching/supergraph.rb', line 17 def executables @executables end |
#locations_by_type_and_field ⇒ Object (readonly)
Returns the value of attribute locations_by_type_and_field.
19 20 21 |
# File 'lib/graphql/stitching/supergraph.rb', line 19 def locations_by_type_and_field @locations_by_type_and_field end |
#resolvers ⇒ Object (readonly)
Returns the value of attribute resolvers.
19 20 21 |
# File 'lib/graphql/stitching/supergraph.rb', line 19 def resolvers @resolvers end |
#schema ⇒ GraphQL::Schema (readonly)
Returns the composed schema for the supergraph.
14 15 16 |
# File 'lib/graphql/stitching/supergraph.rb', line 14 def schema @schema end |
Class Method Details
.from_definition(schema, executables:) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 78 79 80 81 82 83 |
# File 'lib/graphql/stitching/supergraph/to_definition.rb', line 15 def from_definition(schema, executables:) schema = GraphQL::Schema.from_definition(schema) if schema.is_a?(String) field_map = {} resolver_map = {} possible_locations = {} introspection_types = schema.introspection_system.types.keys schema.types.each do |type_name, type| next if introspection_types.include?(type_name) # Collect/build key definitions for each type locations_by_key = type.directives.each_with_object({}) do |directive, memo| next unless directive.graphql_name == KeyDirective.graphql_name kwargs = directive.arguments.keyword_arguments memo[kwargs[:key]] ||= [] memo[kwargs[:key]] << kwargs[:location] end key_definitions = locations_by_key.each_with_object({}) do |(key, locations), memo| memo[key] = TypeResolver.parse_key(key, locations) end # Collect/build resolver definitions for each type type.directives.each do |directive| next unless directive.graphql_name == ResolverDirective.graphql_name kwargs = directive.arguments.keyword_arguments resolver_map[type_name] ||= [] resolver_map[type_name] << TypeResolver.new( location: kwargs[:location], type_name: kwargs.fetch(:type_name, type_name), field: kwargs[:field], list: kwargs[:list] || false, key: key_definitions[kwargs[:key]], arguments: TypeResolver.parse_arguments_with_type_defs(kwargs[:arguments], kwargs[:argument_types]), ) end next unless type.kind.fields? type.fields.each do |field_name, field| # Collection locations for each field definition field.directives.each do |d| next unless d.graphql_name == SourceDirective.graphql_name location = d.arguments.keyword_arguments[:location] field_map[type_name] ||= {} field_map[type_name][field_name] ||= [] field_map[type_name][field_name] << location possible_locations[location] = true end end end executables = possible_locations.keys.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: field_map, resolvers: resolver_map, executables: executables, ) end |
.validate_executable!(location, executable) ⇒ Object
9 10 11 12 13 |
# File 'lib/graphql/stitching/supergraph/to_definition.rb', line 9 def 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, request) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/graphql/stitching/supergraph.rb', line 97 def execute_at_location(location, source, variables, request) 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: request.context.to_h, validate: false, ) elsif executable.respond_to?(:call) executable.call(request, source, variables) else raise StitchingError, "Missing valid executable for #{location} location." end end |
#fields ⇒ Object
64 65 66 |
# File 'lib/graphql/stitching/supergraph.rb', line 64 def fields @locations_by_type_and_field.reject { |k, _v| memoized_introspection_types[k] } end |
#fields_by_type_and_location ⇒ Object
inverts fields map to provide fields for a type/location "Type" => "location" => ["field1", "field2", ...]
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/graphql/stitching/supergraph.rb', line 118 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
68 69 70 |
# File 'lib/graphql/stitching/supergraph.rb', line 68 def locations @executables.keys.reject { _1 == SUPERGRAPH_LOCATION } end |
#locations_by_type ⇒ Object
"Type" => ["location1", "location2", ...]
130 131 132 133 134 |
# File 'lib/graphql/stitching/supergraph.rb', line 130 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 |
#memoized_introspection_types ⇒ Object
72 73 74 |
# File 'lib/graphql/stitching/supergraph.rb', line 72 def memoized_introspection_types @memoized_introspection_types ||= schema.introspection_system.types end |
#memoized_schema_fields(type_name) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/graphql/stitching/supergraph.rb', line 80 def memoized_schema_fields(type_name) @memoized_schema_fields[type_name] ||= begin fields = memoized_schema_types[type_name].fields @schema.introspection_system.dynamic_fields.each do |field| fields[field.name] ||= field # adds __typename end if type_name == @schema.query.graphql_name @schema.introspection_system.entry_points.each do |field| fields[field.name] ||= field # adds __schema, __type end end fields end end |
#memoized_schema_types ⇒ Object
76 77 78 |
# File 'lib/graphql/stitching/supergraph.rb', line 76 def memoized_schema_types @memoized_schema_types ||= @schema.types end |
#possible_keys_for_type(type_name) ⇒ Object
collects all possible resolver keys for a given type ("Type") => [Key("id"), ...]
138 139 140 141 142 143 144 145 146 |
# File 'lib/graphql/stitching/supergraph.rb', line 138 def possible_keys_for_type(type_name) @possible_keys_by_type[type_name] ||= begin if type_name == @schema.query.graphql_name GraphQL::Stitching::EMPTY_ARRAY else @resolvers[type_name].map(&:key).uniq(&:to_definition) end end end |
#possible_keys_for_type_and_location(type_name, location) ⇒ Object
collects possible resolver keys for a given type and location ("Type", "location") => [Key("id"), ...]
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/graphql/stitching/supergraph.rb', line 150 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] ||= possible_keys_for_type(type_name).select do |key| next true if key.locations.include?(location) # Outbound-only locations without resolver queries may dynamically match primitive keys location_fields = fields_by_type_and_location[type_name][location] || GraphQL::Stitching::EMPTY_ARRAY location_fields.include?(key.primitive_name) end end |
#resolvers_by_version ⇒ Object
58 59 60 61 62 |
# File 'lib/graphql/stitching/supergraph.rb', line 58 def resolvers_by_version @resolvers_by_version ||= resolvers.values.tap(&:flatten!).each_with_object({}) do |resolver, memo| memo[resolver.version] = resolver 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 resolver queries
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/graphql/stitching/supergraph.rb', line 163 def route_type_to_locations(type_name, start_location, goal_locations) key_count = possible_keys_for_type(type_name).length if key_count.zero? # nested root scopes have no resolver keys and just return a location goal_locations.each_with_object({}) do |goal_location, memo| memo[goal_location] = [TypeResolver.new(location: goal_location)] end elsif key_count > 1 # multiple keys use an A* search to traverse intermediary locations route_type_to_locations_via_search(type_name, start_location, goal_locations) else # types with a single key attribute must all be within a single hop of each other, # so can use a simple match to collect resolvers for the goal locations. @resolvers[type_name].each_with_object({}) do |resolver, memo| if goal_locations.include?(resolver.location) memo[resolver.location] = [resolver] end end end end |
#static_validator ⇒ GraphQL::StaticValidation::Validator
Returns static validator for the supergraph schema.
54 55 56 |
# File 'lib/graphql/stitching/supergraph.rb', line 54 def static_validator @static_validator ||= @schema.static_validator end |
#to_definition ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/graphql/stitching/supergraph/to_definition.rb', line 86 def to_definition if @schema.directives[KeyDirective.graphql_name].nil? @schema.directive(KeyDirective) end if @schema.directives[ResolverDirective.graphql_name].nil? @schema.directive(ResolverDirective) end if @schema.directives[SourceDirective.graphql_name].nil? @schema.directive(SourceDirective) end @schema.types.each do |type_name, type| if resolvers_for_type = @resolvers.dig(type_name) # Apply key directives for each unique type/key/location # (this allows keys to be composite selections and/or omitted from the supergraph schema) keys_for_type = resolvers_for_type.each_with_object({}) do |resolver, memo| memo[resolver.key.to_definition] ||= Set.new memo[resolver.key.to_definition].merge(resolver.key.locations) end keys_for_type.each do |key, locations| locations.each do |location| params = { key: key, location: location } unless has_directive?(type, KeyDirective.graphql_name, params) type.directive(KeyDirective, **params) end end end # Apply resolver directives for each unique query resolver resolvers_for_type.each do |resolver| params = { location: resolver.location, field: resolver.field, list: resolver.list? || nil, key: resolver.key.to_definition, arguments: resolver.arguments.map(&:to_definition).join(", "), argument_types: resolver.arguments.map(&:to_type_definition).join(", "), type_name: (resolver.type_name if resolver.type_name != type_name), } unless has_directive?(type, ResolverDirective.graphql_name, params) type.directive(ResolverDirective, **params.tap(&:compact!)) end end end next unless type.kind.fields? type.fields.each do |field_name, field| locations_for_field = @locations_by_type_and_field.dig(type_name, field_name) next if locations_for_field.nil? # Apply source directives to annotate the possible locations of each field locations_for_field.each do |location| params = { location: location } unless has_directive?(field, SourceDirective.graphql_name, params) field.directive(SourceDirective, **params) end end end end @schema.to_definition end |