Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

require "delegate"
require "elastic_graph/apollo/schema_definition/api_extension"
require "elastic_graph/json_ingestion/schema_definition/api_extension"
require "elastic_graph/warehouse/schema_definition/api_extension"
require "elastic_graph/local/rake_tasks"
require "elastic_graph/schema_definition/rake_tasks"
Expand Down Expand Up @@ -49,6 +50,7 @@ configure_local_rake_tasks = ->(tasks) do
tasks.index_document_sizes = true
tasks.env_port_mapping = {test: test_port}
tasks.output = schema_def_output
tasks.schema_definition_extension_modules = [ElasticGraph::JSONIngestion::SchemaDefinition::APIExtension]

tasks.define_fake_data_batch_for(:widgets) do
require "rspec/core" # the factories file expects RSpec to be loaded, so load it.
Expand Down
4 changes: 3 additions & 1 deletion config/site/support/doctest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# frozen_string_literal: true

require "elastic_graph/apollo/schema_definition/api_extension"
require "elastic_graph/json_ingestion/schema_definition/api_extension"
require "elastic_graph/schema_artifacts/runtime_metadata/schema_element_names"
require "elastic_graph/schema_definition/api"
require "elastic_graph/schema_definition/schema_artifact_manager"
Expand Down Expand Up @@ -60,7 +61,7 @@ module ElasticGraph
@api = SchemaDefinition::API.new(
SchemaArtifacts::RuntimeMetadata::SchemaElementNames.new(form: :camelCase, overrides: {}),
true,
extension_modules: extension_modules
extension_modules: [JSONIngestion::SchemaDefinition::APIExtension] + extension_modules
)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# frozen_string_literal: true

require "elastic_graph/apollo/schema_definition/api_extension"
require "elastic_graph/json_ingestion/schema_definition/api_extension"
require "elastic_graph/spec_support/schema_definition_helpers"

module ElasticGraph
Expand Down Expand Up @@ -552,7 +553,7 @@ def self.with_both_casing_forms(&block)
end

def define_schema(&block)
extension_modules = [SchemaDefinition::APIExtension]
extension_modules = [::ElasticGraph::JSONIngestion::SchemaDefinition::APIExtension, SchemaDefinition::APIExtension]
super(schema_element_name_form: schema_element_name_form, extension_modules: extension_modules, &block)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# frozen_string_literal: true

require "elastic_graph/apollo/schema_definition/api_extension"
require "elastic_graph/json_ingestion/schema_definition/api_extension"
require "elastic_graph/spec_support/runtime_metadata_support"
require "elastic_graph/spec_support/schema_definition_helpers"
require "graphql"
Expand Down Expand Up @@ -1431,7 +1432,8 @@ def expect_identifiable_type_tagging_of_token(&type_def_for)
end

def define_schema(with_apollo: true, &block)
extension_modules = with_apollo ? [SchemaDefinition::APIExtension] : []
extension_modules = [::ElasticGraph::JSONIngestion::SchemaDefinition::APIExtension]
extension_modules += [SchemaDefinition::APIExtension] if with_apollo
super(schema_element_name_form: schema_element_name_form, extension_modules: extension_modules, &block)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def self.extended(api)
#
# @note While this is an important part of how ElasticGraph is designed to support schema evolution, it can be annoying constantly
# have to increment this while rapidly changing the schema during prototyping. You can disable the requirement to increment this
# on every JSON schema change by setting `enforce_json_schema_version` to `false` in your `Rakefile`.
# on every JSON schema change with {#enforce_json_schema_version}.
#
# @param version [Integer] current version number of the JSON schema artifact
# @return [void]
# @see Local::RakeTasks#enforce_json_schema_version
# @see #enforce_json_schema_version
def json_schema_version(version)
state = json_ingestion_state

Expand All @@ -120,6 +120,26 @@ def json_schema_version(version)
nil
end

# Configures whether JSON schema artifact dumping enforces that {#json_schema_version} gets bumped every time the JSON schemas
# artifact changes. This should generally remain enabled for production applications, but disabling it can be useful during early
# prototyping.
#
# @param value [Boolean] whether JSON schema version bumps should be enforced
# @return [void]
#
# @example Disable JSON schema version enforcement while prototyping
# ElasticGraph.define_schema do |schema|
# schema.enforce_json_schema_version false
# end
def enforce_json_schema_version(value)
unless value == true || value == false
raise Errors::SchemaError, "`enforce_json_schema_version` must be a boolean. Specified value: #{value.inspect}"
end

json_ingestion_state.enforce_json_schema_version = value
nil
end

# Defines strictness of the JSON schema validation. By default, the JSON schema will require all fields to be provided by the
# publisher (but they can be nullable) and will ignore extra fields that are not defined in the schema. Use this method to
# configure this behavior.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def json_schema_indexing_field_types_by_name
end
.sort_by(&:name)
.to_h do |type|
# @type var indexing_field_type: ElasticGraph::SchemaDefinition::Indexing::_FieldType
# @type var indexing_field_type: Indexing::_JSONFieldType
indexing_field_type = _ = type.to_indexing_field_type
[type.name, indexing_field_type]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ module SchemaDefinition
#
# @private
module SchemaArtifactManagerExtension
# Configuration key consumed from {ElasticGraph::SchemaDefinition::SchemaArtifactManager}'s
# `extension_artifact_options:` hash. When set to `false`, JSON schema artifact dumps don't
# require an accompanying `json_schema_version` bump.
ENFORCE_JSON_SCHEMA_VERSION_KEY = :enforce_json_schema_version

# Overrides `dump_artifacts` to add JSON schema version bump checking before dumping.
def dump_artifacts
schema_results = json_ingestion_schema_definition_results

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

def self.extended(state)
state.json_schema_version = nil
state.json_schema_version_setter_location = nil
state.enforce_json_schema_version = true
state.allow_omitted_json_schema_fields = false
state.allow_extra_json_schema_fields = true
end
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module ElasticGraph
BUILT_IN_SCALAR_JSON_SCHEMA_OPTIONS_BY_NAME: ::Hash[::String, ::Hash[::Symbol, untyped]]

def json_schema_version: (::Integer) -> void
def enforce_json_schema_version: (bool) -> void
def json_schema_strictness: (?allow_omitted_fields: bool, ?allow_extra_fields: bool) -> void

def self.extended: (::ElasticGraph::SchemaDefinition::API & APIExtension) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module ElasticGraph

attr_reader unused_deprecated_elements: ::Set[ElasticGraph::SchemaDefinition::SchemaElements::DeprecatedElement]

def initialize: (ElasticGraph::SchemaDefinition::Results) -> void
def initialize: ((ElasticGraph::SchemaDefinition::Results & ResultsExtension)) -> void
def merge_metadata_into: (::Hash[::String, untyped]) -> JSONSchemaWithMetadata

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ module ElasticGraph
@json_schema_field_metadata_by_type_and_field_name: ::Hash[::String, ::Hash[::String, Indexing::JSONSchemaFieldMetadata]]?
@current_public_json_schema: ::Hash[::String, untyped]?
@json_schema_with_metadata_merger: Indexing::JSONSchemaWithMetadata::Merger?
@json_schema_indexing_field_types_by_name: ::Hash[::String, ::ElasticGraph::SchemaDefinition::Indexing::_FieldType]?
@json_schema_indexing_field_types_by_name: ::Hash[::String, Indexing::_JSONFieldType]?

def json_ingestion_state: () -> (::ElasticGraph::SchemaDefinition::State & StateExtension)
def json_schema_with_metadata_merger: () -> Indexing::JSONSchemaWithMetadata::Merger
def build_public_json_schema: () -> ::Hash[::String, untyped]
def json_schema_indexing_field_types_by_name: () -> ::Hash[::String, ::ElasticGraph::SchemaDefinition::Indexing::_FieldType]
def json_schema_indexing_field_types_by_name: () -> ::Hash[::String, Indexing::_JSONFieldType]
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ module ElasticGraph
module JSONIngestion
module SchemaDefinition
module SchemaArtifactManagerExtension : ::ElasticGraph::SchemaDefinition::SchemaArtifactManager
ENFORCE_JSON_SCHEMA_VERSION_KEY: ::Symbol

private

@json_schemas_artifact: ::ElasticGraph::SchemaDefinition::SchemaArtifact[untyped]
@json_schemas_artifact: ::ElasticGraph::SchemaDefinition::SchemaArtifact[untyped]?

def json_ingestion_schema_definition_results: () -> (::ElasticGraph::SchemaDefinition::Results & ResultsExtension)
def artifacts_from_schema_def: () -> ::Array[::ElasticGraph::SchemaDefinition::SchemaArtifact[untyped]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ElasticGraph
module StateExtension: ::ElasticGraph::SchemaDefinition::State
attr_accessor json_schema_version: ::Integer?
attr_accessor json_schema_version_setter_location: ::Thread::Backtrace::Location?
attr_accessor enforce_json_schema_version: bool
attr_accessor allow_omitted_json_schema_fields: bool
attr_accessor allow_extra_json_schema_fields: bool

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ Gem::Specification.new do |spec|

spec.add_dependency "elasticgraph-graphql", ElasticGraph::VERSION # needed since we validate that scalar `coerce_with` options are valid (which loads scalar coercion adapters)
spec.add_dependency "elasticgraph-indexer", ElasticGraph::VERSION # needed since we validate that scalar `prepare_for_indexing_with` options are valid (which loads indexing preparer adapters)
# `elasticgraph-json_ingestion` is the default ingestion serializer extension. It's a soft dep here
# so `default_extension_modules` can fall back to `[]` when the gem isn't installed (e.g. for apps
# that supply a different serializer via `Gemfile-custom`), but most apps want it.
spec.add_dependency "elasticgraph-json_ingestion", ElasticGraph::VERSION
spec.add_dependency "elasticgraph-schema_artifacts", ElasticGraph::VERSION
spec.add_dependency "elasticgraph-support", ElasticGraph::VERSION
Expand Down
Loading
Loading