Skip to content

Commit 4545482

Browse files
jwilsmyronmarston
andauthored
Build namespace types as object types (#1264)
## Summary Namespace types are now built as regular `ObjectType` instances via `new_object_type`. `new_namespace_type` applies the namespace-specific settings (`graphql_only true` and `resolve_fields_with nil`) before yielding, so object-type factory extensions apply to namespace types without swapping constructors or maintaining a dedicated `NamespaceType` subclass. Because GraphQL-only types have no backing datastore representation, `HasIndices#index` now rejects index definitions on any GraphQL-only type. That keeps namespace types unindexable at the shared indexing boundary instead of via a namespace-only subclass override. The Apollo factory extension no longer needs a separate namespace hook because namespace creation now flows through its existing `new_object_type` extension. ## Testing Performed - `bundle exec rspec elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/factory_spec.rb` - `bundle exec rspec elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/graphql_schema/namespace_type_spec.rb elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/graphql_schema/object_type_spec.rb` - `bundle exec rspec elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/runtime_metadata/object_types_by_name/namespace_type_spec.rb elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/datastore_config/index_mappings/namespace_type_spec.rb` - `bundle exec rspec elasticgraph-apollo/spec/unit/elastic_graph/apollo/schema_definition_spec.rb` - `script/type_check` - `script/lint` - `git diff --check` --------- Co-authored-by: Myron Marston <myron.marston@toasttab.com>
1 parent db69965 commit 4545482

12 files changed

Lines changed: 62 additions & 93 deletions

File tree

‎elasticgraph-apollo/lib/elastic_graph/apollo/schema_definition/factory_extension.rb‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@ def new_object_type(name)
7575
end
7676
end
7777

78-
def new_namespace_type(name, &block)
79-
super(name) do |raw_type|
80-
raw_type.extend ObjectTypeExtension
81-
type = raw_type # : ElasticGraph::SchemaDefinition::SchemaElements::NamespaceType & ObjectTypeExtension
82-
83-
yield type if block_given?
84-
end
85-
end
86-
8778
def new_scalar_type(name)
8879
super(name) do |type|
8980
type.extend ScalarTypeExtension

‎elasticgraph-schema_definition/lib/elastic_graph/schema_definition/api.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def object_type(name, &block)
151151
# intermediate namespace fields.
152152
#
153153
# @param name [String] name of the namespace type
154-
# @yield [SchemaElements::NamespaceType] namespace type object, for further customization
154+
# @yield [SchemaElements::ObjectType] namespace type object, for further customization
155155
# @return [void]
156156
#
157157
# @example Group an indexed type's root query fields under an `OlapQuery` namespace

‎elasticgraph-schema_definition/lib/elastic_graph/schema_definition/factory.rb‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
require "elastic_graph/schema_definition/schema_elements/input_field"
2929
require "elastic_graph/schema_definition/schema_elements/input_type"
3030
require "elastic_graph/schema_definition/schema_elements/interface_type"
31-
require "elastic_graph/schema_definition/schema_elements/namespace_type"
3231
require "elastic_graph/schema_definition/schema_elements/object_type"
3332
require "elastic_graph/schema_definition/schema_elements/relationship"
3433
require "elastic_graph/schema_definition/schema_elements/scalar_type"
@@ -239,9 +238,12 @@ def new_interface_type(name)
239238
@@interface_type_new = prevent_non_factory_instantiation_of(SchemaElements::InterfaceType)
240239

241240
def new_namespace_type(name, &block)
242-
@@namespace_type_new.call(@state, name.to_s, &block)
241+
new_object_type(name) do |type|
242+
type.graphql_only true
243+
type.resolve_fields_with nil
244+
yield type if block_given?
245+
end
243246
end
244-
@@namespace_type_new = prevent_non_factory_instantiation_of(SchemaElements::NamespaceType)
245247

246248
def new_object_type(name)
247249
@@object_type_new.call(@state, name.to_s) do |object_type|

‎elasticgraph-schema_definition/lib/elastic_graph/schema_definition/mixins/has_indices.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def index(name, **settings, &block)
103103
"Indices must be configured during initial type definition."
104104
end
105105

106+
if graphql_only?
107+
raise Errors::SchemaError, "`#{self.name}` cannot be both an indexed type and a GraphQL-only type."
108+
end
109+
106110
if @own_index_def
107111
raise Errors::SchemaError, "Cannot define multiple indices on `#{self.name}`. " \
108112
"Only one index per type is supported. An index named `#{@own_index_def.name}` has already been defined."

‎elasticgraph-schema_definition/lib/elastic_graph/schema_definition/schema_elements/namespace_type.rb‎

Lines changed: 0 additions & 63 deletions
This file was deleted.

‎elasticgraph-schema_definition/sig/elastic_graph/schema_definition/api.rbs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module ElasticGraph
6767

6868
def raw_sdl: (::String) -> void
6969
def object_type: (::String) { (SchemaElements::ObjectType) -> void } -> void
70-
def namespace_type: (::String) ?{ (SchemaElements::NamespaceType) -> void } -> void
70+
def namespace_type: (::String) ?{ (SchemaElements::ObjectType) -> void } -> void
7171
def interface_type: (::String) { (SchemaElements::InterfaceType) -> void } -> void
7272
def enum_type: (::String) { (SchemaElements::EnumType) -> void } -> void
7373
def union_type: (::String) { (SchemaElements::UnionType) -> void } -> void

‎elasticgraph-schema_definition/sig/elastic_graph/schema_definition/factory.rbs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ module ElasticGraph
9292
) -> Indexing::FieldType::Object
9393
@@object_indexing_field_type_new: ::Method
9494

95-
def new_namespace_type: (::String) ?{ (SchemaElements::NamespaceType) -> void } -> SchemaElements::NamespaceType
96-
@@namespace_type_new: ::Method
95+
def new_namespace_type: (::String) ?{ (SchemaElements::ObjectType) -> void } -> SchemaElements::ObjectType
9796

9897
def new_scalar_type: (::String) { (SchemaElements::ScalarType) -> void } -> SchemaElements::ScalarType
9998
@@scalar_type_new: ::Method

‎elasticgraph-schema_definition/sig/elastic_graph/schema_definition/schema_elements/namespace_type.rbs‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎elasticgraph-schema_definition/sig/elastic_graph/schema_definition/state.rbs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module ElasticGraph
66
attr_reader index_document_sizes: bool
77
attr_reader types_by_name: SchemaElements::typesByNameHash
88
attr_reader object_types_by_name: ::Hash[::String, indexableType]
9-
attr_reader namespace_types_by_name: ::Hash[::String, SchemaElements::NamespaceType]
9+
attr_reader namespace_types_by_name: ::Hash[::String, SchemaElements::ObjectType]
1010
attr_reader scalar_types_by_name: ::Hash[::String, SchemaElements::ScalarType]
1111
attr_reader enum_types_by_name: ::Hash[::String, SchemaElements::EnumType]
1212
attr_reader implementations_by_interface_ref: ::Hash[SchemaElements::TypeReference, ::Set[SchemaElements::TypeWithSubfields]]
@@ -39,7 +39,7 @@ module ElasticGraph
3939
index_document_sizes: bool,
4040
types_by_name: SchemaElements::typesByNameHash,
4141
object_types_by_name: ::Hash[::String, indexableType],
42-
namespace_types_by_name: ::Hash[::String, SchemaElements::NamespaceType],
42+
namespace_types_by_name: ::Hash[::String, SchemaElements::ObjectType],
4343
scalar_types_by_name: ::Hash[::String, SchemaElements::ScalarType],
4444
enum_types_by_name: ::Hash[::String, SchemaElements::EnumType],
4545
implementations_by_interface_ref: ::Hash[SchemaElements::TypeReference, ::Set[SchemaElements::TypeWithSubfields]],

‎elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/factory_spec.rb‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ module SchemaDefinition
1818
expect(object_type.name).to eq("Widget")
1919
end
2020

21+
it "can create a namespace type without a customization block" do
22+
namespace_type = build_api.factory.new_namespace_type("WidgetNamespace")
23+
24+
expect(namespace_type.name).to eq("WidgetNamespace")
25+
expect(namespace_type).to be_graphql_only
26+
expect(namespace_type.default_graphql_resolver).to be nil
27+
end
28+
2129
it "can create an index without a customization block" do
2230
api = build_api
2331

@@ -30,6 +38,33 @@ module SchemaDefinition
3038
expect(index.name).to eq("widgets")
3139
end
3240

41+
it "builds namespace types through `new_object_type` so factory extensions that customize object types also apply to namespace types" do
42+
marker = Module.new
43+
customizer = Module.new do
44+
define_method(:new_object_type) do |name, &block|
45+
super(name) do |type|
46+
type.extend(marker)
47+
block&.call(type)
48+
end
49+
end
50+
end
51+
52+
factory = build_api.factory
53+
factory.extend(customizer)
54+
55+
object_type = factory.new_object_type("Widget")
56+
customized_namespace_type = nil
57+
namespace_type = factory.new_namespace_type("WidgetNamespace") do |type|
58+
customized_namespace_type = type
59+
end
60+
61+
expect(namespace_type).to be_a(SchemaElements::ObjectType)
62+
# It also went through the overridden `new_object_type`, like an ordinary object type.
63+
expect(object_type).to be_a(marker)
64+
expect(namespace_type).to be_a(marker)
65+
expect(customized_namespace_type).to be(namespace_type)
66+
end
67+
3368
def build_api
3469
API.new(
3570
SchemaArtifacts::RuntimeMetadata::SchemaElementNames.new(form: :snake_case, overrides: {}),

0 commit comments

Comments
 (0)