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

#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_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

#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

#resolvers_by_nameObject

Returns the value of attribute resolvers_by_name

Returns:

  • (Object)

    the current value of resolvers_by_name



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

def resolvers_by_name
  @resolvers_by_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

#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



59
60
61
62
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
# File 'lib/elastic_graph/schema_definition/state.rb', line 59

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 },
    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: {},
    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
  )
end

Instance Method Details

#after_user_definition_complete(&block) ⇒ Object



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

def after_user_definition_complete(&block)
  user_definition_complete_callbacks << block
end

#enums_for_indexed_typesObject



184
185
186
# File 'lib/elastic_graph/schema_definition/state.rb', line 184

def enums_for_indexed_types
  @enums_for_indexed_types ||= factory.new_enums_for_indexed_types
end

#factoryObject



180
181
182
# File 'lib/elastic_graph/schema_definition/state.rb', line 180

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

#field_path_resolverObject



203
204
205
# File 'lib/elastic_graph/schema_definition/state.rb', line 203

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



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

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



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

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



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

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

#register_input_type(type) ⇒ Object



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

def register_input_type(type)
  register_type(type)
end

#register_object_interface_or_union_type(type) ⇒ Object



115
116
117
# File 'lib/elastic_graph/schema_definition/state.rb', line 115

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



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

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



131
132
133
134
135
136
137
# File 'lib/elastic_graph/schema_definition/state.rb', line 131

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



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

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.



166
167
168
# File 'lib/elastic_graph/schema_definition/state.rb', line 166

def register_user_defined_field(field)
  user_defined_fields << field
end

#sub_aggregation_paths_for(type) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/elastic_graph/schema_definition/state.rb', line 188

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



109
110
111
112
113
# File 'lib/elastic_graph/schema_definition/state.rb', line 109

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



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

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