Class: ElasticGraph::SchemaDefinition::State

Inherits:
Struct
  • Object
show all
Defined in:
lib/elastic_graph/schema_definition/state.rb

Overview

Encapsulates all state that needs to be managed while a schema is defined. This is separated from ‘API` to make it easy to expose some state management helper methods to our internal code without needing to expose it as part of the public API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allow_extra_json_schema_fieldsObject

Returns the value of attribute allow_extra_json_schema_fields

Returns:

  • (Object)

    the current value of allow_extra_json_schema_fields



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def allow_extra_json_schema_fields
  @allow_extra_json_schema_fields
end

#allow_omitted_json_schema_fieldsObject

Returns the value of attribute allow_omitted_json_schema_fields

Returns:

  • (Object)

    the current value of allow_omitted_json_schema_fields



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def allow_omitted_json_schema_fields
  @allow_omitted_json_schema_fields
end

#apiObject

Returns the value of attribute api

Returns:

  • (Object)

    the current value of api



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def api
  @api
end

#built_in_graphql_resolversObject

Returns the value of attribute built_in_graphql_resolvers

Returns:

  • (Object)

    the current value of built_in_graphql_resolvers



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def built_in_graphql_resolvers
  @built_in_graphql_resolvers
end

#built_in_types_customization_blocksObject

Returns the value of attribute built_in_types_customization_blocks

Returns:

  • (Object)

    the current value of built_in_types_customization_blocks



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def built_in_types_customization_blocks
  @built_in_types_customization_blocks
end

#deleted_fields_by_type_name_and_old_field_nameObject

Returns the value of attribute deleted_fields_by_type_name_and_old_field_name

Returns:

  • (Object)

    the current value of deleted_fields_by_type_name_and_old_field_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def deleted_fields_by_type_name_and_old_field_name
  @deleted_fields_by_type_name_and_old_field_name
end

#deleted_types_by_old_nameObject

Returns the value of attribute deleted_types_by_old_name

Returns:

  • (Object)

    the current value of deleted_types_by_old_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def deleted_types_by_old_name
  @deleted_types_by_old_name
end

#enum_types_by_nameObject

Returns the value of attribute enum_types_by_name

Returns:

  • (Object)

    the current value of enum_types_by_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def enum_types_by_name
  @enum_types_by_name
end

#enum_value_namerObject

Returns the value of attribute enum_value_namer

Returns:

  • (Object)

    the current value of enum_value_namer



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def enum_value_namer
  @enum_value_namer
end

#graphql_extension_modulesObject

Returns the value of attribute graphql_extension_modules

Returns:

  • (Object)

    the current value of graphql_extension_modules



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def graphql_extension_modules
  @graphql_extension_modules
end

#graphql_resolvers_by_nameObject

Returns the value of attribute graphql_resolvers_by_name

Returns:

  • (Object)

    the current value of graphql_resolvers_by_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def graphql_resolvers_by_name
  @graphql_resolvers_by_name
end

#implementations_by_interface_refObject

Returns the value of attribute implementations_by_interface_ref

Returns:

  • (Object)

    the current value of implementations_by_interface_ref



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def implementations_by_interface_ref
  @implementations_by_interface_ref
end

#index_document_sizesObject Also known as: index_document_sizes?

Returns the value of attribute index_document_sizes

Returns:

  • (Object)

    the current value of index_document_sizes



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def index_document_sizes
  @index_document_sizes
end

#indexed_types_by_index_nameObject

Returns the value of attribute indexed_types_by_index_name

Returns:

  • (Object)

    the current value of indexed_types_by_index_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def indexed_types_by_index_name
  @indexed_types_by_index_name
end

#initially_registered_built_in_typesObject

Returns the value of attribute initially_registered_built_in_types

Returns:

  • (Object)

    the current value of initially_registered_built_in_types



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def initially_registered_built_in_types
  @initially_registered_built_in_types
end

#json_schema_versionObject

Returns the value of attribute json_schema_version

Returns:

  • (Object)

    the current value of json_schema_version



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def json_schema_version
  @json_schema_version
end

#json_schema_version_setter_locationObject

Returns the value of attribute json_schema_version_setter_location

Returns:

  • (Object)

    the current value of json_schema_version_setter_location



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def json_schema_version_setter_location
  @json_schema_version_setter_location
end

#object_types_by_nameObject

Returns the value of attribute object_types_by_name

Returns:

  • (Object)

    the current value of object_types_by_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def object_types_by_name
  @object_types_by_name
end

#outputObject

Returns the value of attribute output

Returns:

  • (Object)

    the current value of output



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def output
  @output
end

#paginated_collection_element_typesObject

Returns the value of attribute paginated_collection_element_types

Returns:

  • (Object)

    the current value of paginated_collection_element_types



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def paginated_collection_element_types
  @paginated_collection_element_types
end

#renamed_fields_by_type_name_and_old_field_nameObject

Returns the value of attribute renamed_fields_by_type_name_and_old_field_name

Returns:

  • (Object)

    the current value of renamed_fields_by_type_name_and_old_field_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def renamed_fields_by_type_name_and_old_field_name
  @renamed_fields_by_type_name_and_old_field_name
end

#renamed_types_by_old_nameObject

Returns the value of attribute renamed_types_by_old_name

Returns:

  • (Object)

    the current value of renamed_types_by_old_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def renamed_types_by_old_name
  @renamed_types_by_old_name
end

#scalar_types_by_nameObject

Returns the value of attribute scalar_types_by_name

Returns:

  • (Object)

    the current value of scalar_types_by_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def scalar_types_by_name
  @scalar_types_by_name
end

#schema_elementsObject

Returns the value of attribute schema_elements

Returns:

  • (Object)

    the current value of schema_elements



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def schema_elements
  @schema_elements
end

#sdl_partsObject

Returns the value of attribute sdl_parts

Returns:

  • (Object)

    the current value of sdl_parts



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def sdl_parts
  @sdl_parts
end

#sub_aggregation_paths_by_typeObject

Returns the value of attribute sub_aggregation_paths_by_type

Returns:

  • (Object)

    the current value of sub_aggregation_paths_by_type



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def sub_aggregation_paths_by_type
  @sub_aggregation_paths_by_type
end

#type_namerObject

Returns the value of attribute type_namer

Returns:

  • (Object)

    the current value of type_namer



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def type_namer
  @type_namer
end

#type_refs_by_nameObject

Returns the value of attribute type_refs_by_name

Returns:

  • (Object)

    the current value of type_refs_by_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def type_refs_by_name
  @type_refs_by_name
end

#types_by_nameObject

Returns the value of attribute types_by_name

Returns:

  • (Object)

    the current value of types_by_name



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def types_by_name
  @types_by_name
end

#union_types_by_member_refObject

Returns the value of attribute union_types_by_member_ref

Returns:

  • (Object)

    the current value of union_types_by_member_ref



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def union_types_by_member_ref
  @union_types_by_member_ref
end

#user_defined_fieldsObject

Returns the value of attribute user_defined_fields

Returns:

  • (Object)

    the current value of user_defined_fields



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def user_defined_fields
  @user_defined_fields
end

#user_definition_completeObject

Returns the value of attribute user_definition_complete

Returns:

  • (Object)

    the current value of user_definition_complete



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def user_definition_complete
  @user_definition_complete
end

#user_definition_complete_callbacksObject

Returns the value of attribute user_definition_complete_callbacks

Returns:

  • (Object)

    the current value of user_definition_complete_callbacks



26
27
28
# File 'lib/elastic_graph/schema_definition/state.rb', line 26

def user_definition_complete_callbacks
  @user_definition_complete_callbacks
end

Class Method Details

.with(api:, schema_elements:, index_document_sizes:, derived_type_name_formats:, type_name_overrides:, enum_value_overrides_by_type:, output: $stdout) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/elastic_graph/schema_definition/state.rb', line 63

def self.with(
  api:,
  schema_elements:,
  index_document_sizes:,
  derived_type_name_formats:,
  type_name_overrides:,
  enum_value_overrides_by_type:,
  output: $stdout
)
  # @type var types_by_name: SchemaElements::typesByNameHash
  types_by_name = {}

  new(
    api: api,
    schema_elements: schema_elements,
    index_document_sizes: index_document_sizes,
    types_by_name: types_by_name,
    object_types_by_name: {},
    scalar_types_by_name: {},
    enum_types_by_name: {},
    implementations_by_interface_ref: ::Hash.new { |h, k| h[k] = ::Set.new },
    union_types_by_member_ref: ::Hash.new { |h, k| h[k] = ::Set.new },
    sdl_parts: [],
    paginated_collection_element_types: ::Set.new,
    user_defined_fields: ::Set.new,
    renamed_types_by_old_name: {},
    deleted_types_by_old_name: {},
    renamed_fields_by_type_name_and_old_field_name: ::Hash.new { |h, k| h[k] = {} },
    deleted_fields_by_type_name_and_old_field_name: ::Hash.new { |h, k| h[k] = {} },
    json_schema_version_setter_location: nil,
    json_schema_version: nil,
    graphql_extension_modules: [],
    graphql_resolvers_by_name: {},
    built_in_graphql_resolvers: ::Set.new,
    initially_registered_built_in_types: ::Set.new,
    built_in_types_customization_blocks: [],
    user_definition_complete: false,
    user_definition_complete_callbacks: [],
    sub_aggregation_paths_by_type: {},
    type_refs_by_name: {},
    type_namer: SchemaElements::TypeNamer.new(
      format_overrides: derived_type_name_formats,
      name_overrides: type_name_overrides
    ),
    enum_value_namer: SchemaElements::EnumValueNamer.new(enum_value_overrides_by_type),
    output: output,
    allow_omitted_json_schema_fields: false,
    allow_extra_json_schema_fields: true,
    indexed_types_by_index_name: {}
  )
end

Instance Method Details

#after_user_definition_complete(&block) ⇒ Object



215
216
217
# File 'lib/elastic_graph/schema_definition/state.rb', line 215

def after_user_definition_complete(&block)
  user_definition_complete_callbacks << block
end

#enums_for_directly_queryable_typesObject



200
201
202
# File 'lib/elastic_graph/schema_definition/state.rb', line 200

def enums_for_directly_queryable_types
  @enums_for_directly_queryable_types ||= factory.new_enums_for_directly_queryable_types
end

#factoryObject



196
197
198
# File 'lib/elastic_graph/schema_definition/state.rb', line 196

def factory
  @factory ||= Factory.new(self)
end

#field_path_resolverObject



219
220
221
# File 'lib/elastic_graph/schema_definition/state.rb', line 219

def field_path_resolver
  @field_path_resolver ||= SchemaElements::FieldPath::Resolver.new(self)
end

#register_deleted_field(type_name, field_name, defined_at:, defined_via:) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/elastic_graph/schema_definition/state.rb', line 172

def register_deleted_field(type_name, field_name, defined_at:, defined_via:)
  deleted_fields_by_old_field_name = deleted_fields_by_type_name_and_old_field_name[type_name] # : ::Hash[::String, SchemaElements::DeprecatedElement]
  deleted_fields_by_old_field_name[field_name] = factory.new_deprecated_element(
    field_name,
    defined_at: defined_at,
    defined_via: defined_via
  )
end

#register_deleted_type(type_name, defined_at:, defined_via:) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/elastic_graph/schema_definition/state.rb', line 155

def register_deleted_type(type_name, defined_at:, defined_via:)
  deleted_types_by_old_name[type_name] = factory.new_deprecated_element(
    type_name,
    defined_at: defined_at,
    defined_via: defined_via
  )
end

#register_enum_type(type) ⇒ Object



128
129
130
# File 'lib/elastic_graph/schema_definition/state.rb', line 128

def register_enum_type(type)
  register_type(type, enum_types_by_name)
end

#register_index(name, type) ⇒ Object



140
141
142
143
144
145
# File 'lib/elastic_graph/schema_definition/state.rb', line 140

def register_index(name, type)
  if (existing_type = indexed_types_by_index_name[name])
    raise Errors::SchemaError, "Duplicate index name `#{name}` defined on `#{type.name}` and `#{existing_type.name}`. Each index can only be defined once."
  end
  indexed_types_by_index_name[name] = type
end

#register_input_type(type) ⇒ Object



136
137
138
# File 'lib/elastic_graph/schema_definition/state.rb', line 136

def register_input_type(type)
  register_type(type)
end

#register_object_interface_or_union_type(type) ⇒ Object



124
125
126
# File 'lib/elastic_graph/schema_definition/state.rb', line 124

def register_object_interface_or_union_type(type)
  register_type(type, object_types_by_name)
end

#register_renamed_field(type_name, from:, to:, defined_at:, defined_via:) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/elastic_graph/schema_definition/state.rb', line 163

def register_renamed_field(type_name, from:, to:, defined_at:, defined_via:)
  renamed_fields_by_old_field_name = renamed_fields_by_type_name_and_old_field_name[type_name] # : ::Hash[::String, SchemaElements::DeprecatedElement]
  renamed_fields_by_old_field_name[from] = factory.new_deprecated_element(
    to,
    defined_at: defined_at,
    defined_via: defined_via
  )
end

#register_renamed_type(type_name, from:, defined_at:, defined_via:) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/elastic_graph/schema_definition/state.rb', line 147

def register_renamed_type(type_name, from:, defined_at:, defined_via:)
  renamed_types_by_old_name[from] = factory.new_deprecated_element(
    type_name,
    defined_at: defined_at,
    defined_via: defined_via
  )
end

#register_scalar_type(type) ⇒ Object



132
133
134
# File 'lib/elastic_graph/schema_definition/state.rb', line 132

def register_scalar_type(type)
  register_type(type, scalar_types_by_name)
end

#register_user_defined_field(field) ⇒ Object

Registers the given ‘field` as a user-defined field, unless the user definitions are complete.



182
183
184
# File 'lib/elastic_graph/schema_definition/state.rb', line 182

def register_user_defined_field(field)
  user_defined_fields << field
end

#sub_aggregation_paths_for(type) ⇒ Object



204
205
206
207
208
209
210
211
212
213
# File 'lib/elastic_graph/schema_definition/state.rb', line 204

def sub_aggregation_paths_for(type)
  sub_aggregation_paths_by_type.fetch(type) do
    SchemaElements::SubAggregationPath.paths_for(type, schema_def_state: self).uniq.tap do |paths|
      # Cache our results if the user has finished their schema definition. Otherwise, it's not safe to cache.
      # :nocov: -- we never execute this with `user_definition_complete == false`
      sub_aggregation_paths_by_type[type] = paths if user_definition_complete
      # :nocov:
    end
  end
end

#type_ref(name) ⇒ Object



118
119
120
121
122
# File 'lib/elastic_graph/schema_definition/state.rb', line 118

def type_ref(name)
  # Type references are immutable and can be safely cached. Here we cache them because we've observed
  # it having a noticeable impact on our test suite runtime.
  type_refs_by_name[name] ||= factory.new_type_reference(name)
end

#user_defined_field_references_by_type_nameObject



186
187
188
189
190
191
192
193
194
# File 'lib/elastic_graph/schema_definition/state.rb', line 186

def user_defined_field_references_by_type_name
  @user_defined_field_references_by_type_name ||= begin
    unless user_definition_complete
      raise Errors::SchemaError, "Cannot access `user_defined_field_references_by_type_name` until the schema definition is complete."
    end

    user_defined_fields.group_by { |f| f.type.fully_unwrapped.name }
  end
end