Skip to content

Commit

Permalink
Avoid "Object types must have fields" warnings.
Browse files Browse the repository at this point in the history
The GraphQL gem, starting in v2.4.5, is issuing warnigs about object types with no fields:

```
Object types must have fields, but Color doesn't have any. Define a field for this type, remove it from your schema, or add `has_no_fields(true)` to its definition.

This will raise an error in a future GraphQL-Ruby version.
```

This fixes spec that accidentally trigger this warning.
  • Loading branch information
myronmarston committed Jan 1, 2025
1 parent 83be56f commit 7f1390a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ def self.with_both_casing_forms(&block)
t.field "id", "ID" do |f|
expect(f).not_to respond_to(*field_extensions)
end
t.index "t1"
end

schema.interface_type "T2" do |t|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ class Schema
RSpec.describe Type, :ensure_no_orphaned_types do
it "exposes the name as a capitalized symbol" do
type = define_schema do |schema|
schema.object_type "Color"
schema.object_type "Color" do |t|
t.field "name", "String"
end
end.type_named("Color")

expect(type.name).to eq :Color
end

it "inspects well" do
type = define_schema do |schema|
schema.object_type "Color"
schema.object_type "Color" do |t|
t.field "name", "String"
end
end.type_named("Color")

expect(type.inspect).to eq "#<ElasticGraph::GraphQL::Schema::Type Color>"
Expand Down Expand Up @@ -608,7 +612,7 @@ def type_for(field_name)
describe "#search_index_definitions" do
it "returns an empty array for a non-union type that is not indexed" do
search_index_definitions = search_index_definitions_from do |schema, type|
schema.object_type(type) {}
schema.object_type(type) { |t| t.field "name", "String" }
end

expect(search_index_definitions).to eq []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class GraphQL
describe "#type_named" do
let(:schema) do
define_schema do |s|
s.object_type "Color"
s.object_type "Color" do |f|
f.field "name", "String"
end
end
end

Expand Down Expand Up @@ -77,7 +79,9 @@ class GraphQL
s.enum_type "Options" do |t|
t.value "firstOption"
end
s.object_type "Color"
s.object_type "Color" do |t|
t.field "name", "String"
end
end

expect(schema.defined_types).to all be_a Schema::Type
Expand Down
4 changes: 4 additions & 0 deletions elasticgraph-health_check/spec/unit/envoy_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def build_graphql_for_path(http_path_segment)
config = {http_path_segment: http_path_segment}.compact
schema_artifacts = generate_schema_artifacts do |schema|
schema.register_graphql_extension(EnvoyExtension, defined_at: "elastic_graph/health_check/envoy_extension", **config)
schema.object_type "Widget" do |t|
t.field "id", "ID"
t.index "widgets"
end
end

build_graphql(schema_artifacts: schema_artifacts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ def expect_configured_interceptors(graphql)
expect(query_adapter.interceptors).to match_array(yield) # we yield to make it lazy since the interceptors are loaded lazily
expect(query_adapter.interceptors.map(&:elasticgraph_graphql)).to all be graphql
end

def generate_schema_artifacts
super do |schema|
yield schema

# Ensure there's at least one indexed type defined to avoid GraphQL validation errors.
schema.object_type "Widget" do |t|
t.field "id", "ID"
t.index "widgets"
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@ module SchemaDefinition
f.renamed_from "old_description"
end
t.renamed_from "Widget3"
t.field "id", "ID"
t.index "widgets"
end
end
EOS
Expand Down

0 comments on commit 7f1390a

Please sign in to comment.