Skip to content

Commit 05163d1

Browse files
committed
Use JSON ingestion as schema definition extension
1 parent 87c88c1 commit 05163d1

57 files changed

Lines changed: 207 additions & 981 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CODEBASE_OVERVIEW.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ graph LR;
111111
rackup["rackup"];
112112
rake["rake"];
113113
webrick["webrick"];
114-
elasticgraph-json_ingestion["eg-json_ingestion"];
115114
elasticgraph-schema_artifacts["eg-schema_artifacts"];
116115
graphql["graphql"];
117116
elasticgraph --> elasticgraph-support;
@@ -126,7 +125,6 @@ graph LR;
126125
elasticgraph-local --> webrick;
127126
elasticgraph-schema_definition --> elasticgraph-graphql;
128127
elasticgraph-schema_definition --> elasticgraph-indexer;
129-
elasticgraph-schema_definition --> elasticgraph-json_ingestion;
130128
elasticgraph-schema_definition --> elasticgraph-schema_artifacts;
131129
elasticgraph-schema_definition --> elasticgraph-support;
132130
elasticgraph-schema_definition --> graphql;
@@ -143,7 +141,6 @@ graph LR;
143141
class rackup externalGemCatStyle;
144142
class rake externalGemCatStyle;
145143
class webrick externalGemCatStyle;
146-
class elasticgraph-json_ingestion otherEgGemStyle;
147144
class elasticgraph-schema_artifacts otherEgGemStyle;
148145
class graphql externalGemCatStyle;
149146
click thor href "https://rubygems.org/gems/thor" "Open on RubyGems.org" _blank;

Gemfile.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ PATH
198198
elasticgraph-schema_definition (1.2.1.pre)
199199
elasticgraph-graphql (= 1.2.1.pre)
200200
elasticgraph-indexer (= 1.2.1.pre)
201-
elasticgraph-json_ingestion (= 1.2.1.pre)
202201
elasticgraph-schema_artifacts (= 1.2.1.pre)
203202
elasticgraph-support (= 1.2.1.pre)
204203
graphql (~> 2.6.2)

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
require "delegate"
1010
require "elastic_graph/apollo/schema_definition/api_extension"
11+
require "elastic_graph/json_ingestion/schema_definition/api_extension"
1112
require "elastic_graph/warehouse/schema_definition/api_extension"
1213
require "elastic_graph/local/rake_tasks"
1314
require "elastic_graph/schema_definition/rake_tasks"
@@ -49,6 +50,7 @@ configure_local_rake_tasks = ->(tasks) do
4950
tasks.index_document_sizes = true
5051
tasks.env_port_mapping = {test: test_port}
5152
tasks.output = schema_def_output
53+
tasks.schema_definition_extension_modules = [ElasticGraph::JSONIngestion::SchemaDefinition::APIExtension]
5254

5355
tasks.define_fake_data_batch_for(:widgets) do
5456
require "rspec/core" # the factories file expects RSpec to be loaded, so load it.

config/site/support/doctest_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# frozen_string_literal: true
88

99
require "elastic_graph/apollo/schema_definition/api_extension"
10+
require "elastic_graph/json_ingestion/schema_definition/api_extension"
1011
require "elastic_graph/schema_artifacts/runtime_metadata/schema_element_names"
1112
require "elastic_graph/schema_definition/api"
1213
require "elastic_graph/schema_definition/schema_artifact_manager"
@@ -60,7 +61,7 @@ module ElasticGraph
6061
@api = SchemaDefinition::API.new(
6162
SchemaArtifacts::RuntimeMetadata::SchemaElementNames.new(form: :camelCase, overrides: {}),
6263
true,
63-
extension_modules: extension_modules
64+
extension_modules: [JSONIngestion::SchemaDefinition::APIExtension] + extension_modules
6465
)
6566

6667
# This is required in all schemas, but we don't want to have to put in all our examples,
@@ -95,6 +96,7 @@ module ElasticGraph
9596
# `schema.json_schema_version` raises an error when the version is set more than once.
9697
# By default we set it above. Here we clear it to allow our example to set it.
9798
schema.state.json_schema_version = nil
99+
schema.state.json_schema_version_setter_location = nil
98100
end
99101
end
100102

elasticgraph-apollo/spec/unit/elastic_graph/apollo/apollo_directives_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# frozen_string_literal: true
88

99
require "elastic_graph/apollo/schema_definition/api_extension"
10+
require "elastic_graph/json_ingestion/schema_definition/api_extension"
1011
require "elastic_graph/spec_support/schema_definition_helpers"
1112

1213
module ElasticGraph
@@ -552,7 +553,7 @@ def self.with_both_casing_forms(&block)
552553
end
553554

554555
def define_schema(&block)
555-
extension_modules = [SchemaDefinition::APIExtension]
556+
extension_modules = [::ElasticGraph::JSONIngestion::SchemaDefinition::APIExtension, SchemaDefinition::APIExtension]
556557
super(schema_element_name_form: schema_element_name_form, extension_modules: extension_modules, &block)
557558
end
558559
end

elasticgraph-apollo/spec/unit/elastic_graph/apollo/schema_definition_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# frozen_string_literal: true
88

99
require "elastic_graph/apollo/schema_definition/api_extension"
10+
require "elastic_graph/json_ingestion/schema_definition/api_extension"
1011
require "elastic_graph/spec_support/runtime_metadata_support"
1112
require "elastic_graph/spec_support/schema_definition_helpers"
1213
require "graphql"
@@ -1431,7 +1432,8 @@ def expect_identifiable_type_tagging_of_token(&type_def_for)
14311432
end
14321433

14331434
def define_schema(with_apollo: true, &block)
1434-
extension_modules = with_apollo ? [SchemaDefinition::APIExtension] : []
1435+
extension_modules = [::ElasticGraph::JSONIngestion::SchemaDefinition::APIExtension]
1436+
extension_modules += [SchemaDefinition::APIExtension] if with_apollo
14351437
super(schema_element_name_form: schema_element_name_form, extension_modules: extension_modules, &block)
14361438
end
14371439
end

elasticgraph-json_ingestion/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@ graph LR;
1717
elasticgraph-support["elasticgraph-support"];
1818
elasticgraph-json_ingestion --> elasticgraph-support;
1919
class elasticgraph-support otherEgGemStyle;
20-
elasticgraph-schema_definition["elasticgraph-schema_definition"];
21-
elasticgraph-schema_definition --> elasticgraph-json_ingestion;
22-
class elasticgraph-schema_definition otherEgGemStyle;
2320
```

elasticgraph-json_ingestion/lib/elastic_graph/json_ingestion/schema_definition/api_extension.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ def self.extended(api)
9999
#
100100
# @note While this is an important part of how ElasticGraph is designed to support schema evolution, it can be annoying constantly
101101
# have to increment this while rapidly changing the schema during prototyping. You can disable the requirement to increment this
102-
# on every JSON schema change by setting `enforce_json_schema_version` to `false` in your `Rakefile`.
102+
# on every JSON schema change with {#enforce_json_schema_version}.
103103
#
104104
# @param version [Integer] current version number of the JSON schema artifact
105105
# @return [void]
106-
# @see Local::RakeTasks#enforce_json_schema_version
106+
# @see #enforce_json_schema_version
107107
def json_schema_version(version)
108108
state = json_ingestion_state
109109

@@ -120,6 +120,26 @@ def json_schema_version(version)
120120
nil
121121
end
122122

123+
# Configures whether JSON schema artifact dumping enforces that {#json_schema_version} gets bumped every time the JSON schemas
124+
# artifact changes. This should generally remain enabled for production applications, but disabling it can be useful during early
125+
# prototyping.
126+
#
127+
# @param value [Boolean] whether JSON schema version bumps should be enforced
128+
# @return [void]
129+
#
130+
# @example Disable JSON schema version enforcement while prototyping
131+
# ElasticGraph.define_schema do |schema|
132+
# schema.enforce_json_schema_version false
133+
# end
134+
def enforce_json_schema_version(value)
135+
unless value == true || value == false
136+
raise Errors::SchemaError, "`enforce_json_schema_version` must be a boolean. Specified value: #{value.inspect}"
137+
end
138+
139+
json_ingestion_state.enforce_json_schema_version = value
140+
nil
141+
end
142+
123143
# Defines strictness of the JSON schema validation. By default, the JSON schema will require all fields to be provided by the
124144
# publisher (but they can be nullable) and will ignore extra fields that are not defined in the schema. Use this method to
125145
# configure this behavior.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def json_schema_indexing_field_types_by_name
121121
end
122122
.sort_by(&:name)
123123
.to_h do |type|
124-
# @type var indexing_field_type: ElasticGraph::SchemaDefinition::Indexing::_FieldType
124+
# @type var indexing_field_type: Indexing::_JSONFieldType
125125
indexing_field_type = _ = type.to_indexing_field_type
126126
[type.name, indexing_field_type]
127127
end

elasticgraph-json_ingestion/lib/elastic_graph/json_ingestion/schema_definition/schema_artifact_manager_extension.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ module SchemaDefinition
1818
#
1919
# @private
2020
module SchemaArtifactManagerExtension
21-
# Configuration key consumed from {ElasticGraph::SchemaDefinition::SchemaArtifactManager}'s
22-
# `extension_artifact_options:` hash. When set to `false`, JSON schema artifact dumps don't
23-
# require an accompanying `json_schema_version` bump.
24-
ENFORCE_JSON_SCHEMA_VERSION_KEY = :enforce_json_schema_version
25-
2621
# Overrides `dump_artifacts` to add JSON schema version bump checking before dumping.
2722
def dump_artifacts
2823
schema_results = json_ingestion_schema_definition_results
2924

3025
check_if_needs_json_schema_version_bump do |recommended_json_schema_version|
31-
if @extension_artifact_options.fetch(ENFORCE_JSON_SCHEMA_VERSION_KEY, true)
26+
if schema_results.state.enforce_json_schema_version
3227
# @type var setter_location: ::Thread::Backtrace::Location
3328
# We use `_ =` because while `json_schema_version_setter_location` can be nil,
3429
# it'll never be nil if we get here and we want the type to be non-nilable.
@@ -39,13 +34,12 @@ def dump_artifacts
3934
"increase the schema's version, and then run the `bundle exec rake schema_artifacts:dump` command again.\n\n" \
4035
"To update the schema version to the expected version, change line #{setter_location.lineno} at `#{setter_location_path}` to:\n" \
4136
" `schema.json_schema_version #{recommended_json_schema_version}`\n\n" \
42-
"Alternately, pass `extension_artifact_options: {enforce_json_schema_version: false}` to `ElasticGraph::SchemaDefinition::RakeTasks.new` " \
43-
"(or set `tasks.enforce_json_schema_version = false` on `ElasticGraph::Local::RakeTasks`) to allow the JSON schemas file to " \
44-
"change without requiring a version bump, but that is only recommended for non-production applications during initial schema prototyping."
37+
"Alternately, call `schema.enforce_json_schema_version false` in your schema definition to allow the JSON schemas file " \
38+
"to change without requiring a version bump, but that is only recommended for non-production applications during initial schema prototyping."
4539
else
4640
@output.puts <<~EOS
4741
WARNING: the `json_schemas.yaml` artifact is being updated without the `json_schema_version` being correspondingly incremented.
48-
This is not recommended for production applications, but is currently allowed because you have set `enforce_json_schema_version: false`.
42+
This is not recommended for production applications, but is currently allowed because you have called `schema.enforce_json_schema_version false`.
4943
EOS
5044
end
5145
end

0 commit comments

Comments
 (0)