|
1 | 1 | # ElasticGraph::JSONIngestion |
2 | 2 |
|
3 | | -JSON Schema ingestion support for ElasticGraph. |
| 3 | +Provides JSON Schema ingestion support for ElasticGraph. |
4 | 4 |
|
5 | 5 | This gem provides the schema-definition extension that generates JSON Schema artifacts for indexing |
6 | | -events and validates JSON-ingestion-specific schema options. |
| 6 | +events and validates JSON-ingestion-specific schema options. Generated ElasticGraph projects install |
| 7 | +and enable it by default. Applications that wire schema-definition tasks manually enable it by adding |
| 8 | +`ElasticGraph::JSONIngestion::SchemaDefinition::APIExtension` to their schema-definition extension modules. |
| 9 | + |
| 10 | +## Schema Definition APIs |
| 11 | + |
| 12 | +Use `schema.json_schema_version` to identify the current JSON schema artifact. Every change that affects |
| 13 | +the JSON schema should increment this version so publishers and indexers can safely evolve independently. |
| 14 | +During early prototyping, `schema.enforce_json_schema_version false` can disable version-change enforcement. |
| 15 | +For production applications, leave enforcement enabled so schema artifact changes require an intentional |
| 16 | +version bump. |
| 17 | + |
| 18 | +```diff |
| 19 | +diff --git a/config/schema.rb b/config/schema.rb |
| 20 | +index 015c5fa..b8eeaef 100644 |
| 21 | +--- a/config/schema.rb |
| 22 | ++++ b/config/schema.rb |
| 23 | +@@ -2,3 +2,3 @@ ElasticGraph.define_schema do |schema| |
| 24 | + # ElasticGraph will tell you when you need to bump this. |
| 25 | +- schema.json_schema_version 1 |
| 26 | ++ schema.json_schema_version 2 |
| 27 | + |
| 28 | +``` |
| 29 | + |
| 30 | +```ruby |
| 31 | +# in config/schema/json_schema_enforcement.rb |
| 32 | + |
| 33 | +ElasticGraph.define_schema do |schema| |
| 34 | + schema.enforce_json_schema_version false |
| 35 | +end |
| 36 | +``` |
| 37 | + |
| 38 | +Use `schema.json_schema_strictness` to configure whether indexing events may omit nullable fields or include |
| 39 | +extra fields. We recommend enabling at most one of these options, because enabling both can hide misspelled |
| 40 | +event fields. |
| 41 | + |
| 42 | +```ruby |
| 43 | +# in config/schema/json_schema_strictness.rb |
| 44 | + |
| 45 | +ElasticGraph.define_schema do |schema| |
| 46 | + schema.json_schema_strictness allow_omitted_fields: true, allow_extra_fields: false |
| 47 | +end |
| 48 | +``` |
| 49 | + |
| 50 | +Custom scalar types must declare how they are represented in JSON Schema: |
| 51 | + |
| 52 | +```ruby |
| 53 | +# in config/schema/url.rb |
| 54 | + |
| 55 | +ElasticGraph.define_schema do |schema| |
| 56 | + schema.scalar_type "URL" do |t| |
| 57 | + t.mapping type: "keyword" |
| 58 | + t.json_schema type: "string", format: "uri" |
| 59 | + end |
| 60 | +end |
| 61 | +``` |
| 62 | + |
| 63 | +Fields and object/interface types can add JSON Schema validations. Use field-level validations sparingly: |
| 64 | +they run while indexing events, so violations can send otherwise valid source-system data to the dead letter |
| 65 | +queue. They are best reserved for constraints that ElasticGraph needs in order to index correctly. |
| 66 | + |
| 67 | +```ruby |
| 68 | +# in config/schema/card.rb |
| 69 | + |
| 70 | +ElasticGraph.define_schema do |schema| |
| 71 | + schema.object_type "Card" do |t| |
| 72 | + t.field "id", "ID!" |
| 73 | + |
| 74 | + t.field "expYear", "Int" do |f| |
| 75 | + f.json_schema minimum: 2000, maximum: 2099 |
| 76 | + end |
| 77 | + |
| 78 | + t.field "expMonth", "Int" do |f| |
| 79 | + f.json_schema minimum: 1, maximum: 12 |
| 80 | + end |
| 81 | + |
| 82 | + t.index "cards" |
| 83 | + end |
| 84 | +end |
| 85 | +``` |
| 86 | + |
| 87 | +On fields, `nullable: false` disallows `null` in indexing events while keeping the GraphQL field nullable: |
| 88 | + |
| 89 | +```ruby |
| 90 | +# in config/schema/widget.rb |
| 91 | + |
| 92 | +ElasticGraph.define_schema do |schema| |
| 93 | + schema.object_type "Widget" do |t| |
| 94 | + t.field "id", "ID!" |
| 95 | + |
| 96 | + t.field "name", "String" do |f| |
| 97 | + f.json_schema nullable: false |
| 98 | + end |
| 99 | + |
| 100 | + t.index "widgets" |
| 101 | + end |
| 102 | +end |
| 103 | +``` |
7 | 104 |
|
8 | 105 | ## Dependency Diagram |
9 | 106 |
|
|
0 commit comments