-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathstate.rb
More file actions
237 lines (209 loc) · 8.56 KB
/
state.rb
File metadata and controls
237 lines (209 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
84
85
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Copyright 2024 - 2026 Block, Inc.
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
#
# frozen_string_literal: true
require "elastic_graph/constants"
require "elastic_graph/errors"
require "elastic_graph/schema_definition/factory"
require "elastic_graph/schema_definition/mixins/has_readable_to_s_and_inspect"
require "elastic_graph/schema_definition/schema_elements/enum_value_namer"
require "elastic_graph/schema_definition/schema_elements/field_path"
require "elastic_graph/schema_definition/schema_elements/type_namer"
require "elastic_graph/schema_definition/schema_elements/sub_aggregation_path"
module ElasticGraph
module SchemaDefinition
# Encapsulates all state that needs to be managed while a schema is defined.
# This is separated from `API` to make it easy to expose some state management
# helper methods to our internal code without needing to expose it as part of
# the public API.
#
# @private
class State < Struct.new(
:api,
:schema_elements,
:index_document_sizes,
:types_by_name,
:object_types_by_name,
:scalar_types_by_name,
:enum_types_by_name,
:implementations_by_interface_ref,
:union_types_by_member_ref,
:sdl_parts,
:paginated_collection_element_types,
:user_defined_fields,
:renamed_types_by_old_name,
:deleted_types_by_old_name,
:renamed_fields_by_type_name_and_old_field_name,
:deleted_fields_by_type_name_and_old_field_name,
:json_schema_version,
:json_schema_version_setter_location,
:graphql_extension_modules,
:graphql_resolvers_by_name,
:built_in_graphql_resolvers,
:initially_registered_built_in_types,
:built_in_types_customization_blocks,
:user_definition_complete,
:user_definition_complete_callbacks,
:sub_aggregation_paths_by_type,
:type_refs_by_name,
:output,
:type_namer,
:enum_value_namer,
:allow_omitted_json_schema_fields,
:allow_extra_json_schema_fields,
:enums_in_transition
)
include Mixins::HasReadableToSAndInspect.new
def self.with(
api:,
schema_elements:,
index_document_sizes:,
derived_type_name_formats:,
type_name_overrides:,
enum_value_overrides_by_type:,
enums_in_transition: ::Set.new,
output: $stdout
)
# @type var types_by_name: SchemaElements::typesByNameHash
types_by_name = {}
new(
api: api,
schema_elements: schema_elements,
index_document_sizes: index_document_sizes,
types_by_name: types_by_name,
object_types_by_name: {},
scalar_types_by_name: {},
enum_types_by_name: {},
implementations_by_interface_ref: ::Hash.new { |h, k| h[k] = ::Set.new },
union_types_by_member_ref: ::Hash.new { |h, k| h[k] = ::Set.new },
sdl_parts: [],
paginated_collection_element_types: ::Set.new,
user_defined_fields: ::Set.new,
renamed_types_by_old_name: {},
deleted_types_by_old_name: {},
renamed_fields_by_type_name_and_old_field_name: ::Hash.new { |h, k| h[k] = {} },
deleted_fields_by_type_name_and_old_field_name: ::Hash.new { |h, k| h[k] = {} },
json_schema_version_setter_location: nil,
json_schema_version: nil,
graphql_extension_modules: [],
graphql_resolvers_by_name: {},
built_in_graphql_resolvers: ::Set.new,
initially_registered_built_in_types: ::Set.new,
built_in_types_customization_blocks: [],
user_definition_complete: false,
user_definition_complete_callbacks: [],
sub_aggregation_paths_by_type: {},
type_refs_by_name: {},
type_namer: SchemaElements::TypeNamer.new(
format_overrides: derived_type_name_formats,
name_overrides: type_name_overrides
),
enum_value_namer: SchemaElements::EnumValueNamer.new(enum_value_overrides_by_type),
output: output,
allow_omitted_json_schema_fields: false,
allow_extra_json_schema_fields: true,
enums_in_transition: enums_in_transition.to_set
)
end
# @dynamic index_document_sizes?
alias_method :index_document_sizes?, :index_document_sizes
def type_ref(name)
# Type references are immutable and can be safely cached. Here we cache them because we've observed
# it having a noticeable impact on our test suite runtime.
type_refs_by_name[name] ||= factory.new_type_reference(name)
end
def register_object_interface_or_union_type(type)
register_type(type, object_types_by_name)
end
def register_enum_type(type)
register_type(type, enum_types_by_name)
end
def register_scalar_type(type)
register_type(type, scalar_types_by_name)
end
def register_input_type(type)
register_type(type)
end
def register_renamed_type(type_name, from:, defined_at:, defined_via:)
renamed_types_by_old_name[from] = factory.new_deprecated_element(
type_name,
defined_at: defined_at,
defined_via: defined_via
)
end
def register_deleted_type(type_name, defined_at:, defined_via:)
deleted_types_by_old_name[type_name] = factory.new_deprecated_element(
type_name,
defined_at: defined_at,
defined_via: defined_via
)
end
def register_renamed_field(type_name, from:, to:, defined_at:, defined_via:)
renamed_fields_by_old_field_name = renamed_fields_by_type_name_and_old_field_name[type_name] # : ::Hash[::String, SchemaElements::DeprecatedElement]
renamed_fields_by_old_field_name[from] = factory.new_deprecated_element(
to,
defined_at: defined_at,
defined_via: defined_via
)
end
def register_deleted_field(type_name, field_name, defined_at:, defined_via:)
deleted_fields_by_old_field_name = deleted_fields_by_type_name_and_old_field_name[type_name] # : ::Hash[::String, SchemaElements::DeprecatedElement]
deleted_fields_by_old_field_name[field_name] = factory.new_deprecated_element(
field_name,
defined_at: defined_at,
defined_via: defined_via
)
end
# Registers the given `field` as a user-defined field, unless the user definitions are complete.
def register_user_defined_field(field)
user_defined_fields << field
end
def user_defined_field_references_by_type_name
@user_defined_field_references_by_type_name ||= begin
unless user_definition_complete
raise Errors::SchemaError, "Cannot access `user_defined_field_references_by_type_name` until the schema definition is complete."
end
user_defined_fields.group_by { |f| f.type.fully_unwrapped.name }
end
end
def factory
@factory ||= Factory.new(self)
end
def enums_for_root_document_types
@enums_for_root_document_types ||= factory.new_enums_for_root_document_types
end
def sub_aggregation_paths_for(type)
sub_aggregation_paths_by_type.fetch(type) do
SchemaElements::SubAggregationPath.paths_for(type, schema_def_state: self).uniq.tap do |paths|
# Cache our results if the user has finished their schema definition. Otherwise, it's not safe to cache.
# :nocov: -- we never execute this with `user_definition_complete == false`
sub_aggregation_paths_by_type[type] = paths if user_definition_complete
# :nocov:
end
end
end
def after_user_definition_complete(&block)
user_definition_complete_callbacks << block
end
def field_path_resolver
@field_path_resolver ||= SchemaElements::FieldPath::Resolver.new(self)
end
private
RESERVED_TYPE_NAMES = [EVENT_ENVELOPE_JSON_SCHEMA_NAME].to_set
def register_type(type, additional_type_index = nil)
name = (_ = type).name
if RESERVED_TYPE_NAMES.include?(name)
raise Errors::SchemaError, "`#{name}` cannot be used as a schema type because it is a reserved name."
end
if types_by_name.key?(name)
raise Errors::SchemaError, "Duplicate definition for type #{name} detected. Each type can only be defined once."
end
additional_type_index[name] = type if additional_type_index
types_by_name[name] = type
end
end
end
end