Class: ElasticGraph::SchemaDefinition::SchemaElements::Relationship
- Inherits:
-
Field
- Object
- Struct
- Field
- ElasticGraph::SchemaDefinition::SchemaElements::Relationship
- Defined in:
- lib/elastic_graph/schema_definition/schema_elements/relationship.rb
Overview
Wraps a Field to provide additional relationship-specific functionality when defining a field via TypeWithSubfields#relates_to_one or TypeWithSubfields#relates_to_many.
Constant Summary
Constants inherited from Field
Field::ACCURACY_SCORES, Field::HIGHLIGHTABLE_MAPPING_TYPES
Constants included from Mixins::HasTypeInfo
Mixins::HasTypeInfo::CUSTOMIZABLE_DATASTORE_PARAMS
Instance Attribute Summary collapse
- #hide_relationship_runtime_metadata ⇒ Object
-
#related_type ⇒ ObjectType, ...
readonly
The type this relationship relates to.
Attributes inherited from Field
#accuracy_confidence, #aggregatable, #aggregated_values_customizations, #args, #as_input, #computation_detail, #filter_customizations, #filterable, #graphql_only, #groupable, #grouped_by_customizations, #highlightable, #highlights_customizations, #name, #name_in_index, #non_nullable_in_json_schema, #original_type, #original_type_for_derived_types, #parent_type, #relationship, #resolver, #runtime_field_script, #schema_def_state, #singular_name, #sort_order_enum_value_customizations, #sortable, #source, #sub_aggregations_customizations
Attributes included from Mixins::HasDocumentation
Instance Method Summary collapse
-
#additional_filter(filter) ⇒ void
Adds additional filter conditions to a relationship beyond the foreign key.
-
#equivalent_field(path, locally_named: path) ⇒ void
Indicates that ‘path` (a field on the related type) is the equivalent of `locally_named` on this type.
-
#initialize(field, cardinality:, related_type:, foreign_key:, direction:) ⇒ Relationship
constructor
A new instance of Relationship.
- #many? ⇒ Boolean
-
#rollover_timestamp_value_source_for_index(index) ⇒ Object
Gets the ‘rollover_timestamp_value_source` from this relationship for the given `index`, based on the configured equivalent fields and the rollover configuration used by `index`.
-
#routing_value_source_for_index(index) ⇒ Object
Gets the ‘routing_value_source` from this relationship for the given `index`, based on the configured routing used by `index` and the configured equivalent fields.
- #runtime_metadata ⇒ Object
- #validate_equivalent_fields(field_path_resolver) ⇒ Object
Methods inherited from Field
#add_grouped_by_field_documentation, #aggregatable?, #argument, #backing_indexing_field, #customize_aggregated_values_field, #customize_filter_field, #customize_grouped_by_field, #customize_highlights_field, #customize_sort_order_enum_values, #customize_sub_aggregations_field, #define_aggregated_values_field, #define_grouped_by_field, #define_highlights_field, #define_relay_pagination_arguments!, #define_sub_aggregations_field, #filterable?, #groupable?, #grouped_by_field_name, #grouped_by_field_type_name, #highlightable?, #index_leaf?, #json_schema, #list_field_groupable_by_single_values?, #mapping, #mapping_type, #nested?, #on_each_generated_schema_element, #paths_to_lists_for_count_indexing, pick_most_accurate_from, #renamed_from, #resolve_mapping, #resolve_with, #runtime_metadata_computation_detail, #runtime_metadata_graphql_field, #runtime_script, #sortable?, #sourced_from, #sub_aggregatable?, #to_filter_field, #to_indexing_field, #to_indexing_field_reference, #to_qualified_sdl, #to_sdl, #type, #type_for_derived_types
Methods included from Mixins::HasTypeInfo
#json_schema, #json_schema_options, #mapping, #mapping_options
Methods included from Mixins::HasDirectives
#directive, #directives, #directives_sdl
Methods included from Mixins::HasDocumentation
#append_to_documentation, #derived_documentation, #documentation, #formatted_documentation
Methods included from Mixins::VerifiesGraphQLName
Constructor Details
#initialize(field, cardinality:, related_type:, foreign_key:, direction:) ⇒ Relationship
Returns a new instance of Relationship.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 49 def initialize(field, cardinality:, related_type:, foreign_key:, direction:) super(field) self. = false @cardinality = cardinality @related_type = @foreign_key = foreign_key @direction = direction @equivalent_field_paths_by_local_path = {} @additional_filter = {} end |
Instance Attribute Details
#hide_relationship_runtime_metadata ⇒ Object
46 47 48 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 46 def @hide_relationship_runtime_metadata end |
#related_type ⇒ ObjectType, ... (readonly)
Returns the type this relationship relates to.
43 44 45 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 43 def @related_type end |
Instance Method Details
#additional_filter(filter) ⇒ void
This method returns an undefined value.
Adds additional filter conditions to a relationship beyond the foreign key.
85 86 87 88 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 85 def additional_filter(filter) stringified_filter = Support::HashUtil.stringify_keys(filter) @additional_filter = Support::HashUtil.deep_merge(@additional_filter, stringified_filter) end |
#equivalent_field(path, locally_named: path) ⇒ void
This method returns an undefined value.
Indicates that ‘path` (a field on the related type) is the equivalent of `locally_named` on this type.
Use this API to specify a local field’s equivalent path on the related type. This must be used on relationships used by Field#sourced_from when the local type uses Indexing::Index#route_with or Indexing::Index#rollover so that ElasticGraph can determine what field from the related type to use to route the update requests to the correct index and shard.
129 130 131 132 133 134 135 136 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 129 def equivalent_field(path, locally_named: path) if @equivalent_field_paths_by_local_path.key?(locally_named) raise Errors::SchemaError, "`equivalent_field` has been called multiple times on `#{parent_type.name}.#{name}` with the same " \ "`locally_named` value (#{locally_named.inspect}), but each local field can have only one `equivalent_field`." else @equivalent_field_paths_by_local_path[locally_named] = path end end |
#many? ⇒ Boolean
193 194 195 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 193 def many? @cardinality == :many end |
#rollover_timestamp_value_source_for_index(index) ⇒ Object
Gets the ‘rollover_timestamp_value_source` from this relationship for the given `index`, based on the configured equivalent fields and the rollover configuration used by `index`.
Returns the GraphQL field name (not the ‘name_in_index`).
159 160 161 162 163 164 165 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 159 def (index) return nil unless (rollover_config = index.rollover_config) @equivalent_field_paths_by_local_path.fetch(rollover_config..path) do |local_need| yield local_need end end |
#routing_value_source_for_index(index) ⇒ Object
Gets the ‘routing_value_source` from this relationship for the given `index`, based on the configured routing used by `index` and the configured equivalent fields.
Returns the GraphQL field name (not the ‘name_in_index`).
144 145 146 147 148 149 150 151 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 144 def routing_value_source_for_index(index) return nil unless index.uses_custom_routing? index_routing_field_path = index.routing_field_path # : FieldPath @equivalent_field_paths_by_local_path.fetch(index_routing_field_path.path) do |local_need| yield local_need end end |
#runtime_metadata ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 198 def return nil if = (_ = .unwrap_list.as_object_type) # : indexableType foreign_key_nested_paths = schema_def_state.field_path_resolver.determine_nested_paths(, @foreign_key) foreign_key_nested_paths ||= [] # : ::Array[::String] SchemaArtifacts::RuntimeMetadata::Relation.new( foreign_key: @foreign_key, direction: @direction, additional_filter: @additional_filter, foreign_key_nested_paths: foreign_key_nested_paths ) end |
#validate_equivalent_fields(field_path_resolver) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/elastic_graph/schema_definition/schema_elements/relationship.rb', line 168 def validate_equivalent_fields(field_path_resolver) = (_ = .as_object_type) # : indexableType @equivalent_field_paths_by_local_path.flat_map do |local_path_string, | errors = [] # : ::Array[::String] local_path = resolve_and_validate_field_path(parent_type, local_path_string, field_path_resolver) do |error| errors << error end = resolve_and_validate_field_path(, , field_path_resolver) do |error| errors << error end if local_path && && local_path.type.unwrap_non_null != .type.unwrap_non_null errors << "Field `#{.full_description}` is defined as an equivalent of " \ "`#{local_path.full_description}` via an `equivalent_field` definition on `#{parent_type.name}.#{name}`, " \ "but their types do not agree. To continue, change one or the other so that they agree." end errors end end |