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 9, 2025
1 parent d0dde6d commit 9d8e58d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ 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 = {
type.override_runtime_metadata(
elasticgraph_category: :scalar_aggregated_values,
default_graphql_resolver: :object
}
)

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 @@ -436,10 +436,10 @@ 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 = {
t.override_runtime_metadata(
elasticgraph_category: :relay_edge,
default_graphql_resolver: :object
}
)

t.documentation <<~EOS
Represents a specific `#{type_name}` in the context of a `#{type_ref.as_connection.name}`,
Expand All @@ -466,10 +466,10 @@ 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 = {
t.override_runtime_metadata(
elasticgraph_category: :relay_connection,
default_graphql_resolver: :object
}
)

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,10 @@ def derived_indexed_types
@derived_indexed_types ||= []
end

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,10 +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,
default_graphql_resolver: :object
}
t.override_runtime_metadata elasticgraph_category: :nested_sub_aggregation_connection
end
end

Expand Down Expand Up @@ -119,7 +116,7 @@ def sub_aggregation_types_for_nested_field_references
schema_def_state.sub_aggregation_paths_for(nested_field_ref.parent_type).map do |path|
schema_def_state.factory.new_object_type type_ref.as_sub_aggregation(parent_doc_types: path.parent_doc_types).name do |t|
t.documentation "Return type representing a bucket of `#{name}` objects for a sub-aggregation within each `#{type_ref.as_parent_aggregation(parent_doc_types: path.parent_doc_types).name}`."
t.runtime_metadata_overrides = {default_graphql_resolver: :object}
t.override_runtime_metadata default_graphql_resolver: :object

t.field schema_def_state.schema_elements.count_detail, "AggregationCountDetail", graphql_only: true do |f|
f.documentation "Details of the count of `#{name}` documents in a sub-aggregation bucket."
Expand Down Expand Up @@ -167,7 +164,7 @@ def build_aggregation_sub_aggregations_types
schema_def_state.factory.new_object_type agg_sub_aggs_type_ref.name do |t|
under_field_description = "under `#{path.field_path_string}` " unless path.field_path.empty?
t.documentation "Provides access to the `#{schema_def_state.schema_elements.sub_aggregations}` #{under_field_description}within each `#{type_ref.as_parent_aggregation(parent_doc_types: path.parent_doc_types).name}`."
t.runtime_metadata_overrides = {default_graphql_resolver: :object}
t.override_runtime_metadata default_graphql_resolver: :object

sub_aggregatable_fields.each do |field|
if field.nested?
Expand Down Expand Up @@ -230,11 +227,11 @@ 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 = {
t.override_runtime_metadata(
source_type: name,
elasticgraph_category: :indexed_aggregation,
default_graphql_resolver: :object
}
)
end
end

Expand All @@ -245,7 +242,7 @@ def to_grouped_by_type

new_non_empty_object_type type_ref.as_grouped_by.name do |t|
t.documentation "Type used to specify the `#{name}` fields to group by for aggregations."
t.runtime_metadata_overrides = {default_graphql_resolver: :object}
t.override_runtime_metadata default_graphql_resolver: :object

graphql_fields_by_name.values.each do |field|
field.define_grouped_by_field(t)
Expand All @@ -260,7 +257,7 @@ def to_aggregated_values_type

new_non_empty_object_type type_ref.as_aggregated_values.name do |t|
t.documentation "Type used to perform aggregation computations on `#{name}` fields."
t.runtime_metadata_overrides = {default_graphql_resolver: :object}
t.override_runtime_metadata default_graphql_resolver: :object

graphql_fields_by_name.values.each do |field|
field.define_aggregated_values_field(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def register_standard_elastic_graph_types
# to go from non-null to null, but is not a breaking change to make it non-null
# in the future.
register_framework_object_type "PageInfo" do |t|
t.runtime_metadata_overrides = {default_graphql_resolver: :object}
t.override_runtime_metadata default_graphql_resolver: :object

t.documentation <<~EOS
Provides information about the specific fetched page. This implements the `PageInfo`
Expand Down Expand Up @@ -511,7 +511,7 @@ def register_standard_elastic_graph_types

register_framework_object_type "AggregationCountDetail" do |t|
t.documentation "Provides detail about an aggregation `#{names.count}`."
t.runtime_metadata_overrides = {default_graphql_resolver: :object}
t.override_runtime_metadata default_graphql_resolver: :object

t.field names.approximate_value, "JsonSafeLong!", graphql_only: true do |f|
f.documentation <<~EOS
Expand Down Expand Up @@ -1293,10 +1293,10 @@ 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 = {
t.override_runtime_metadata(
elasticgraph_category: :date_grouped_by_object,
default_graphql_resolver: :object
}
)

t.field names.as_date, "Date", graphql_only: true do |f|
f.documentation "Used when grouping on the full `Date` value."
Expand All @@ -1313,10 +1313,10 @@ 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 = {
t.override_runtime_metadata(
elasticgraph_category: :date_grouped_by_object,
default_graphql_resolver: :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 9d8e58d

Please sign in to comment.