Skip to content

Commit bd68e85

Browse files
Fix/id field non nullable json schema (#1118)
* Enforce non-nullability in required ID field * Fix broken Goose documentation link The Goose docs moved from block.github.io/goose to goose-docs.ai, causing a 404 in the HTML-Proofer CI check. * remove unnecessary change * remove non-nullable from ID field to force test coverage * update schema rtifacts
1 parent 14cb7cf commit bd68e85

5 files changed

Lines changed: 29 additions & 4 deletions

File tree

config/schema/artifacts/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10318,7 +10318,7 @@ type Team {
1031810318
forbes_valuation_moneys_object: [Money!]!
1031910319
forbes_valuations: [JsonSafeLong!]!
1032010320
formed_on: Date
10321-
id: ID!
10321+
id: ID
1032210322
league: String
1032310323
nested_fields: TeamNestedFields
1032410324
nested_fields2: TeamNestedFields

config/schema/artifacts_with_apollo/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10641,7 +10641,7 @@ type Team @key(fields: "id") {
1064110641
forbes_valuation_moneys_object: [Money!]!
1064210642
forbes_valuations: [JsonSafeLong!]!
1064310643
formed_on: Date
10644-
id: ID!
10644+
id: ID
1064510645
league: String
1064610646
nested_fields: TeamNestedFields
1064710647
nested_fields2: TeamNestedFields

config/schema/teams.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
schema.object_type "Team" do |t|
4141
t.root_query_fields plural: "teams"
42-
t.field "id", "ID!"
42+
t.field "id", "ID"
4343
t.field "league", "String"
4444
t.field "country_code", "ID!"
4545
t.field "formed_on", "Date"

elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/index.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def initialize(name, settings, schema_def_state, indexed_type)
6363
# By using it here, it will cause queries to pass a `routing` parameter when
6464
# searching with id filtering on an index that does not use custom shard routing, giving
6565
# us a nice efficiency boost.
66-
self.routing_field_path = public_field_path("id", explanation: "indexed types must have an `id` field")
66+
id_field_path = public_field_path("id", explanation: "indexed types must have an `id` field")
67+
self.routing_field_path = id_field_path
68+
id_field_path.last_part.json_schema nullable: false
6769
end
6870

6971
yield self if block_given?

elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/json_schema_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,29 @@ def have_json_schema_like(type_name, *args, **kwargs)
11401140
})
11411141
end
11421142

1143+
it "makes the `id` field non-nullable in JSON schema even when declared as a nullable `ID` in the GraphQL schema" do
1144+
json_schema = dump_schema do |s|
1145+
s.object_type "Widget" do |t|
1146+
t.field "id", "ID"
1147+
t.field "name", "String"
1148+
t.index "widgets"
1149+
end
1150+
end
1151+
1152+
expect(json_schema).to have_json_schema_like("Widget", {
1153+
"type" => "object",
1154+
"properties" => {
1155+
"id" => json_schema_ref("ID!"),
1156+
"name" => json_schema_ref("String")
1157+
},
1158+
"required" => %w[id name]
1159+
}).which_matches(
1160+
{"id" => "abc", "name" => "thing"}
1161+
).and_fails_to_match(
1162+
{"id" => nil, "name" => "thing"}
1163+
)
1164+
end
1165+
11431166
context "on an indexed type with a rollover index" do
11441167
it "makes the JSON schema for a rollover index timestamp field on the indexed type non-nullable since a target index cannot be chosen without it" do
11451168
json_schema = dump_schema do |s|

0 commit comments

Comments
 (0)