Skip to content

Commit

Permalink
Refactor: merge multiple runtime metadata overrides.
Browse files Browse the repository at this point in the history
This allows us to set runtime metadata overrides in multiple spots and
trust that the merged result will be used.
  • Loading branch information
myronmarston committed Feb 24, 2025
1 parent ac83c3b commit b999574
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def new_aggregated_values_type_for_index_leaf_type(index_leaf_type)
new_object_type @state.type_ref(index_leaf_type).as_aggregated_values.name do |type|
type.graphql_only true
type.documentation "A return type used from aggregations to provided aggregated values over `#{index_leaf_type}` fields."
type.runtime_metadata_overrides = {elasticgraph_category: :scalar_aggregated_values}
type.override_runtime_metadata(elasticgraph_category: :scalar_aggregated_values)

type.field @state.schema_elements.approximate_distinct_value_count, "JsonSafeLong", graphql_only: true do |f|
# Note: the 1-6% accuracy figure comes from the Elasticsearch docs:
Expand Down Expand Up @@ -433,7 +433,7 @@ def edge_type_for(type_name)
type_ref = @state.type_ref(type_name)
new_object_type type_ref.as_edge.name do |t|
t.relay_pagination_type = true
t.runtime_metadata_overrides = {elasticgraph_category: :relay_edge}
t.override_runtime_metadata(elasticgraph_category: :relay_edge)

t.documentation <<~EOS
Represents a specific `#{type_name}` in the context of a `#{type_ref.as_connection.name}`,
Expand All @@ -460,7 +460,7 @@ def connection_type_for(type_name, include_total_edge_count, derived_indexed_typ
type_ref = @state.type_ref(type_name)
new_object_type type_ref.as_connection.name do |t|
t.relay_pagination_type = true
t.runtime_metadata_overrides = {elasticgraph_category: :relay_connection}
t.override_runtime_metadata(elasticgraph_category: :relay_connection)

if support_pagination
t.documentation <<~EOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ module Mixins
module HasIndices
# @dynamic runtime_metadata_overrides
# @private
attr_accessor :runtime_metadata_overrides
attr_reader :runtime_metadata_overrides

# @private
def initialize(*args, **options)
super(*args, **options)
self.runtime_metadata_overrides = {}
@runtime_metadata_overrides = {}
yield self

# Freeze `indices` so that the indexable status of a type does not change after instantiation.
Expand Down Expand Up @@ -150,6 +150,15 @@ def derived_indexed_types
@derived_indexed_types ||= []
end

# Configures overrides for runtime metadata. The provided runtime metadata values will be persisted in the
# `runtime_metadata.yaml` schema artifact and made available at runtime to `elasticgraph-graphql` and
# `elasticgraph-indexer`.
#
# @return [void]
def override_runtime_metadata(**overrides)
@runtime_metadata_overrides.merge!(overrides)
end

# @private
def runtime_metadata(extra_update_targets)
SchemaArtifacts::RuntimeMetadata::ObjectType.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def derived_graphql_types
[type] + schema_def_state.factory.build_relay_pagination_types(type.name, support_pagination: false) do |t|
# Record metadata that is necessary for elasticgraph-graphql to correctly recognize and handle
# this sub-aggregation correctly.
t.runtime_metadata_overrides = {elasticgraph_category: :nested_sub_aggregation_connection}
t.override_runtime_metadata(elasticgraph_category: :nested_sub_aggregation_connection)
end
end

Expand Down Expand Up @@ -225,7 +225,7 @@ def to_indexed_aggregation_type

# Record metadata that is necessary for elasticgraph-graphql to correctly recognize and handle
# this indexed aggregation type correctly.
t.runtime_metadata_overrides = {source_type: name, elasticgraph_category: :indexed_aggregation}
t.override_runtime_metadata(source_type: name, elasticgraph_category: :indexed_aggregation)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ def register_date_and_time_grouped_by_types
date = schema_def_state.type_ref("Date")
register_framework_object_type date.as_grouped_by.name do |t|
t.documentation "Allows for grouping `Date` values based on the desired return type."
t.runtime_metadata_overrides = {elasticgraph_category: :date_grouped_by_object}
t.override_runtime_metadata(elasticgraph_category: :date_grouped_by_object)

t.field names.as_date, "Date", graphql_only: true do |f|
f.documentation "Used when grouping on the full `Date` value."
Expand All @@ -1307,7 +1307,7 @@ def register_date_and_time_grouped_by_types
date_time = schema_def_state.type_ref("DateTime")
register_framework_object_type date_time.as_grouped_by.name do |t|
t.documentation "Allows for grouping `DateTime` values based on the desired return type."
t.runtime_metadata_overrides = {elasticgraph_category: :date_grouped_by_object}
t.override_runtime_metadata(elasticgraph_category: :date_grouped_by_object)

t.field names.as_date_time, "DateTime", graphql_only: true do |f|
f.documentation "Used when grouping on the full `DateTime` value."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module ElasticGraph
module Mixins
module HasIndices
attr_reader indices: ::Array[Indexing::Index]
attr_accessor runtime_metadata_overrides: ::Hash[::Symbol, untyped]
attr_reader runtime_metadata_overrides: ::Hash[::Symbol, untyped]
def index: (::String, ::Hash[::Symbol, ::String | ::Integer]) ?{ (Indexing::Index) -> void } -> ::Array[Indexing::Index]
def indexed?: () -> bool
def override_runtime_metadata: (**untyped) -> void
def runtime_metadata: (::Array[SchemaArtifacts::RuntimeMetadata::UpdateTarget]) -> SchemaArtifacts::RuntimeMetadata::ObjectType
def derived_indexed_types: () -> ::Array[Indexing::DerivedIndexedType]
def derive_indexed_type_fields: (
Expand Down

0 comments on commit b999574

Please sign in to comment.