Skip to content

Commit e215bc0

Browse files
authored
Generate proto messages for non-indexed sourced_from source types (#1380)
## Summary Closes #1360. `schema.proto` only had messages for the indexed types and the types they reference, so a publisher could not send an event for a pure-source type: one that feeds a `sourced_from` field but has no index of its own. #1346 widened the JSON schema event envelope to accept these events, but proto ingestion never got the same treatment. Nothing in an indexed type's proto representation refers to a pure-source type, so the traversal never reached it. Both JSON and proto now use `Results#ingestible_types_by_name` to select indexed document types and `sourced_from` source types, excluding derived indexing types. `Results` resolves the completed schema once and passes the shared map to both builders. Proto traverses referenced types and emits abstract wrappers; JSON selects concrete event names. The messages themselves needed no change, as #1360 predicted. `proto_fields` builds from `indexing_fields_by_name_in_index`, which does not depend on the type having an index: ```protobuf message ComponentDesign { string id = 1; string component_id = 2; string designer_name = 3; // Next field number: 4 } ``` ## Derived indexing types (a Buf-breaking change) Derived indexing types are now excluded from the seed. ElasticGraph builds their documents from the events of other types, so a publisher never sends one, and the JSON schema artifact has never described them either. Proto ingestion described them only because a derived type has its own index, which put it in the old seed. Removing a message is a breaking change under the Buf check that #1378 adds. No published schema depends on these messages yet, so this is the moment to align the two formats. ## Abstract source types A `sourced_from` source type can be an interface or a union. Such a type keeps the `oneof` wrapper message it would get as an indexed abstract type, and its subtypes get messages too. Note the JSON event envelope does not accept an abstract type by name, and it does not accept the concrete subtypes either, because they are neither indexed nor source types. So an abstract source type is not publishable over JSON today. That gap is tracked in #1386 and is not addressed here. ## Notes - No schema artifact churn. The root `Rakefile` does not register the proto extension, and this repo commits no `schema.proto`. - Proto schema generation now runs the `sourced_from` resolver, so it can raise relationship and `sourced_from` validation errors that it previously skipped. No existing proto spec defines a relationship, so no fixture needed a repair.
1 parent 2932af6 commit e215bc0

11 files changed

Lines changed: 215 additions & 20 deletions

File tree

‎elasticgraph-json_ingestion/lib/elastic_graph/json_ingestion/schema_definition/json_schema_builder.rb‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ module SchemaDefinition
1717
#
1818
# @private
1919
class JSONSchemaBuilder
20-
def initialize(state:, all_types:, derived_indexing_type_names:, sourced_from_source_type_names:)
20+
def initialize(state:, all_types:, derived_indexing_type_names:, ingestible_types_by_name:)
2121
@state = state
2222
@all_types = all_types
2323
@derived_indexing_type_names = derived_indexing_type_names
24-
@sourced_from_source_type_names = sourced_from_source_type_names
24+
@ingestible_types_by_name = ingestible_types_by_name
2525
end
2626

2727
def public_json_schema
@@ -45,14 +45,9 @@ def field_metadata_by_type_and_field_name
4545

4646
private
4747

48-
# The types the indexer can ingest events for: indexed document types plus `sourced_from` source types
49-
# (which need not be indexed themselves). Derived indexing types are excluded because their documents
50-
# are built from other types' events rather than ingested directly.
48+
# JSON events identify a concrete type; abstract types have no event envelope entry.
5149
def ingestible_type_names
52-
@state.object_types_by_name.values
53-
.select { |type| type.root_document_type? || @sourced_from_source_type_names.include?(type.name) }
54-
.reject { |type| type.abstract? || @derived_indexing_type_names.include?(type.name) }
55-
.map(&:name)
50+
@ingestible_types_by_name.values.reject(&:abstract?).map(&:name)
5651
end
5752

5853
def definitions_by_name

‎elasticgraph-json_ingestion/lib/elastic_graph/json_ingestion/schema_definition/results_extension.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def json_ingestion_state
7676

7777
def json_ingestion_json_schema_builder
7878
@json_ingestion_json_schema_builder ||= begin
79-
# Resolve `sourced_from` update targets before touching `all_types` so that `sourced_from`
79+
# Resolve `sourced_from` update targets (via `ingestible_types_by_name`) before touching `all_types` so that `sourced_from`
8080
# validation errors take precedence over any errors raised while generating derived types.
81-
sourced_from_source_type_names = sourced_update_targets_by_source_type_name.keys.to_set
81+
ingestible_types = ingestible_types_by_name
8282

8383
# Force `all_types` to materialize before iterating `state.types_by_name`. Reading `all_types`
8484
# runs the `on_built_in_types` callbacks, including the GeoLocation JSON schema field
@@ -89,7 +89,7 @@ def json_ingestion_json_schema_builder
8989
state: json_ingestion_state,
9090
all_types: materialized_all_types,
9191
derived_indexing_type_names: derived_indexing_type_names,
92-
sourced_from_source_type_names: sourced_from_source_type_names
92+
ingestible_types_by_name: ingestible_types
9393
)
9494
end
9595
end

‎elasticgraph-json_ingestion/sig/elastic_graph/json_ingestion/schema_definition/json_schema_builder.rbs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module ElasticGraph
66
state: ::ElasticGraph::SchemaDefinition::State & StateExtension,
77
all_types: ::Array[::ElasticGraph::SchemaDefinition::SchemaElements::graphQLType],
88
derived_indexing_type_names: ::Set[::String],
9-
sourced_from_source_type_names: ::Set[::String]
9+
ingestible_types_by_name: ::Hash[::String, ::ElasticGraph::SchemaDefinition::indexableType]
1010
) -> void
1111

1212
def public_json_schema: () -> ::Hash[::String, untyped]
@@ -17,7 +17,7 @@ module ElasticGraph
1717
@state: ::ElasticGraph::SchemaDefinition::State & StateExtension
1818
@all_types: ::Array[::ElasticGraph::SchemaDefinition::SchemaElements::graphQLType]
1919
@derived_indexing_type_names: ::Set[::String]
20-
@sourced_from_source_type_names: ::Set[::String]
20+
@ingestible_types_by_name: ::Hash[::String, ::ElasticGraph::SchemaDefinition::indexableType]
2121
@indexing_field_types_by_name: ::Hash[::String, Indexing::_JSONFieldType]?
2222

2323
def ingestible_type_names: () -> ::Array[::String]

‎elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/results_extension.rb‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ def protobuf_schema_generator
3737
# runtime in {APIExtension.extended}.
3838
extension_state = state # : ElasticGraph::SchemaDefinition::State & StateExtension
3939

40+
# Resolve `sourced_from` update targets (via `ingestible_types_by_name`) before touching `all_types` so that `sourced_from`
41+
# validation errors take precedence over any errors raised while generating derived types.
42+
ingestible_types = ingestible_types_by_name
43+
4044
Schema.new(
4145
state: extension_state,
4246
all_types: all_types,
43-
ingestion_state: extension_state.proto_ingestion_state
47+
ingestion_state: extension_state.proto_ingestion_state,
48+
ingestible_types_by_name: ingestible_types
4449
)
4550
end
4651
end

‎elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema.rb‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ def self.validate_header_lines(header_lines)
6464
# @param state [ElasticGraph::SchemaDefinition::State]
6565
# @param all_types [Array<ElasticGraph::SchemaDefinition::SchemaElements::graphQLType>]
6666
# @param ingestion_state [ProtoIngestionState] this extension's configured schema definition state
67-
def initialize(state:, all_types:, ingestion_state:)
67+
# @param ingestible_types_by_name [Hash<String, Object>] ingestible schema types, including abstract types
68+
def initialize(
69+
state:,
70+
all_types:,
71+
ingestion_state:,
72+
ingestible_types_by_name:
73+
)
6874
@state = state
6975
@all_types = all_types
76+
@ingestible_types_by_name = ingestible_types_by_name
7077
@package_name = ingestion_state.package_name
7178
@syntax = self.class.validate_syntax(ingestion_state.syntax)
7279
@header_lines = self.class.validate_header_lines(ingestion_state.header_lines)
@@ -136,10 +143,12 @@ def field_label_prefix(repeated:)
136143

137144
private
138145

139-
# Selects indexed root types and every type transitively referenced by their protobuf
146+
# Selects the ingestible types and every type transitively referenced by their protobuf
140147
# representations. All traversal state is local so repeated calls are independent.
141148
def proto_types
142-
types_to_visit = _ = @state.indexed_types_by_index_name.values.dup
149+
# Types gain proto methods through instance extensions that Steep cannot see.
150+
# @type var types_to_visit: ::Array[untyped]
151+
types_to_visit = @ingestible_types_by_name.values
143152
type_names_to_render = ::Set.new
144153

145154
while (type = types_to_visit.shift)

‎elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema.rbs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ module ElasticGraph
1616
@all_types: ::Array[::ElasticGraph::SchemaDefinition::SchemaElements::graphQLType]
1717
@package_name: ::String
1818
@field_number_mappings: FieldNumberMappings
19+
@ingestible_types_by_name: ::Hash[::String, ::ElasticGraph::SchemaDefinition::indexableType]
1920
@previous_field_names_by_type_name_and_field_name: ::Hash[::String, ::Hash[::String, ::Array[::String]]]?
2021

2122
def initialize: (
2223
state: ::ElasticGraph::SchemaDefinition::State,
2324
all_types: ::Array[::ElasticGraph::SchemaDefinition::SchemaElements::graphQLType],
24-
ingestion_state: ProtoIngestionState
25+
ingestion_state: ProtoIngestionState,
26+
ingestible_types_by_name: ::Hash[::String, ::ElasticGraph::SchemaDefinition::indexableType]
2527
) -> void
2628

2729
def to_proto: () -> ::String

‎elasticgraph-proto_ingestion/spec/support/proto_schema_support.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def run_protoc(proto_schema, operation, input)
8888
end
8989
end
9090

91+
def proto_types_defined_in(proto)
92+
proto.scan(/^(?:enum|message) (\w+) \{/).flatten
93+
end
94+
9195
def proto_type_def_from(proto, type)
9296
lines = proto.lines
9397
definition_start = /^(?:enum|message) #{Regexp.escape(type)} \{/
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Copyright 2024 - 2026 Block, Inc.
2+
#
3+
# Use of this source code is governed by an MIT-style
4+
# license that can be found in the LICENSE file or at
5+
# https://opensource.org/licenses/MIT.
6+
#
7+
# frozen_string_literal: true
8+
9+
require "elastic_graph/proto_ingestion/schema_definition/api_extension"
10+
11+
module ElasticGraph
12+
module ProtoIngestion
13+
module SchemaDefinition
14+
# Covers which types get a message: the types a publisher can send events for, and every type
15+
# they reference.
16+
RSpec.describe Schema, "ingestible types" do
17+
it "generates a message for a `sourced_from` source type that has no index of its own" do
18+
proto = define_proto_schema do |s|
19+
define_component_type(s)
20+
21+
s.object_type "ComponentDesign" do |t|
22+
t.field "id", "ID!"
23+
t.field "component_id", "ID"
24+
t.field "designer_name", "String"
25+
end
26+
end
27+
28+
expect(proto_type_def_from(proto, "ComponentDesign")).to eq(<<~PROTO.strip)
29+
message ComponentDesign {
30+
optional string id = 1;
31+
optional string component_id = 2;
32+
optional string designer_name = 3;
33+
// Next field number: 4
34+
}
35+
PROTO
36+
end
37+
38+
it "generates a message for a type referenced by a non-indexed source type" do
39+
proto = define_proto_schema do |s|
40+
define_component_type(s)
41+
42+
s.object_type "ComponentDesign" do |t|
43+
t.field "id", "ID!"
44+
t.field "component_id", "ID"
45+
t.field "designer_name", "String"
46+
t.field "studio", "DesignStudio"
47+
end
48+
49+
s.object_type "DesignStudio" do |t|
50+
t.field "name", "String"
51+
end
52+
end
53+
54+
expect(proto_type_def_from(proto, "DesignStudio")).to eq(<<~PROTO.strip)
55+
message DesignStudio {
56+
optional string name = 1;
57+
// Next field number: 2
58+
}
59+
PROTO
60+
end
61+
62+
it "generates a oneof wrapper and subtype messages for an abstract source type" do
63+
proto = define_proto_schema do |s|
64+
define_component_type(s)
65+
66+
s.interface_type "ComponentDesign" do |t|
67+
t.field "id", "ID!"
68+
t.field "component_id", "ID"
69+
t.field "designer_name", "String"
70+
end
71+
72+
s.object_type "InternalComponentDesign" do |t|
73+
t.implements "ComponentDesign"
74+
t.field "id", "ID!"
75+
t.field "component_id", "ID"
76+
t.field "designer_name", "String"
77+
end
78+
end
79+
80+
expect(proto_type_def_from(proto, "ComponentDesign")).to eq(<<~PROTO.strip)
81+
message ComponentDesign {
82+
oneof value {
83+
.elasticgraph.InternalComponentDesign internal_component_design = 1;
84+
}
85+
// Next field number: 2
86+
}
87+
PROTO
88+
89+
expect(proto_type_def_from(proto, "InternalComponentDesign")).to eq(<<~PROTO.strip)
90+
message InternalComponentDesign {
91+
optional string id = 1;
92+
optional string component_id = 2;
93+
optional string designer_name = 3;
94+
// Next field number: 4
95+
}
96+
PROTO
97+
end
98+
99+
it "generates no message for a derived indexing type, since no publisher sends its events" do
100+
proto = define_proto_schema do |s|
101+
s.object_type "Widget" do |t|
102+
t.field "id", "ID!"
103+
t.field "workspace_id", "ID"
104+
t.field "name", "String"
105+
t.index "widgets"
106+
107+
t.derive_indexed_type_fields "WidgetWorkspace", from_id: "workspace_id" do |derive|
108+
derive.immutable_value "name", from: "name"
109+
end
110+
end
111+
112+
s.object_type "WidgetWorkspace" do |t|
113+
t.field "id", "ID!"
114+
t.field "name", "String"
115+
t.index "widget_workspaces"
116+
end
117+
end
118+
119+
expect(proto_types_defined_in(proto)).to contain_exactly("Widget")
120+
end
121+
122+
it "generates no message for a type that no ingestible type references" do
123+
proto = define_proto_schema do |s|
124+
s.object_type "Widget" do |t|
125+
t.field "id", "ID!"
126+
t.field "name", "String"
127+
t.index "widgets"
128+
end
129+
130+
s.object_type "Unreferenced" do |t|
131+
t.field "id", "ID!"
132+
end
133+
end
134+
135+
expect(proto_types_defined_in(proto)).to contain_exactly("Widget")
136+
end
137+
138+
# Defines an indexed type with a top-level `sourced_from` field fed by `ComponentDesign`.
139+
def define_component_type(schema)
140+
schema.object_type "Component" do |t|
141+
t.field "id", "ID!"
142+
t.field "name", "String"
143+
144+
t.field "designer_name", "String" do |f|
145+
f.sourced_from "design", "designer_name"
146+
end
147+
148+
t.relates_to_one "design", "ComponentDesign", via: "component_id", dir: :in, indexing_only: true
149+
150+
t.index "components" do |i|
151+
i.has_had_multiple_sources!
152+
end
153+
end
154+
end
155+
end
156+
end
157+
end
158+
end

‎elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/schema_spec.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ module SchemaDefinition
135135
generator = Schema.new(
136136
state: results.state,
137137
all_types: results.send(:all_types),
138-
ingestion_state: results.state.proto_ingestion_state
138+
ingestion_state: results.state.proto_ingestion_state,
139+
ingestible_types_by_name: results.ingestible_types_by_name
139140
)
140141

141142
first_generation = generator.to_proto

‎elasticgraph-schema_definition/lib/elastic_graph/schema_definition/results.rb‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ def runtime_metadata
4646
# @private
4747
STATIC_SCRIPT_REPO = Scripting::FileSystemRepository.new(::File.join(__dir__.to_s, "scripting", "scripts"))
4848

49+
# The indexed document types and `sourced_from` source types, excluding derived indexing
50+
# types whose documents are built from other types' events. Abstract types are included
51+
# when they declare an index or act as a source, but not merely for inheriting an index.
52+
# Each ingestion format decides how to represent them.
53+
#
54+
# @private
55+
def ingestible_types_by_name
56+
@ingestible_types_by_name ||= begin
57+
source_type_names = sourced_update_targets_by_source_type_name.keys.to_set
58+
59+
state.object_types_by_name.select do |name, type|
60+
indexed = type.abstract? ? type.has_own_index_def? : type.root_document_type?
61+
(indexed || source_type_names.include?(name)) &&
62+
!derived_indexing_type_names.include?(name)
63+
end
64+
end
65+
end
66+
4967
# @private
5068
def derived_indexing_type_names
5169
@derived_indexing_type_names ||= state

0 commit comments

Comments
 (0)