|
1 | 1 | # ElasticGraph::JSONIngestion |
2 | 2 |
|
3 | | -JSON Schema ingestion support for ElasticGraph. |
| 3 | +Default 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 | + |
| 15 | +```diff |
| 16 | +diff --git a/config/schema.rb b/config/schema.rb |
| 17 | +index 015c5fa..b8eeaef 100644 |
| 18 | +--- a/config/schema.rb |
| 19 | ++++ b/config/schema.rb |
| 20 | +@@ -1,5 +1,5 @@ |
| 21 | + ElasticGraph.define_schema do |schema| |
| 22 | + # ElasticGraph will tell you when you need to bump this. |
| 23 | +- schema.json_schema_version 1 |
| 24 | ++ schema.json_schema_version 2 |
| 25 | +- |
| 26 | ++ |
| 27 | + # Set this to true once you're beyond the prototyping stage. |
| 28 | +``` |
| 29 | + |
| 30 | +Use `schema.json_schema_strictness` to configure whether indexing events may omit nullable fields or include |
| 31 | +extra fields. We recommend enabling at most one of these options, because enabling both can hide misspelled |
| 32 | +event fields. |
| 33 | + |
| 34 | +```ruby |
| 35 | +# in config/schema/json_schema_strictness.rb |
| 36 | + |
| 37 | +ElasticGraph.define_schema do |schema| |
| 38 | + schema.json_schema_strictness allow_omitted_fields: true, allow_extra_fields: false |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +Custom scalar types must declare how they are represented in JSON Schema: |
| 43 | + |
| 44 | +```ruby |
| 45 | +# in config/schema/url.rb |
| 46 | + |
| 47 | +ElasticGraph.define_schema do |schema| |
| 48 | + schema.scalar_type "URL" do |t| |
| 49 | + t.mapping type: "keyword" |
| 50 | + t.json_schema type: "string", format: "uri" |
| 51 | + end |
| 52 | +end |
| 53 | +``` |
| 54 | + |
| 55 | +Fields and object/interface types can add JSON Schema validations. Use field-level validations sparingly: |
| 56 | +they run while indexing events, so violations can send otherwise valid source-system data to the dead letter |
| 57 | +queue. They are best reserved for constraints that ElasticGraph needs in order to index correctly. |
| 58 | + |
| 59 | +```ruby |
| 60 | +# in config/schema/card.rb |
| 61 | + |
| 62 | +ElasticGraph.define_schema do |schema| |
| 63 | + schema.object_type "Card" do |t| |
| 64 | + t.field "id", "ID!" |
| 65 | + |
| 66 | + t.field "expYear", "Int" do |f| |
| 67 | + f.json_schema minimum: 2000, maximum: 2099 |
| 68 | + end |
| 69 | + |
| 70 | + t.field "expMonth", "Int" do |f| |
| 71 | + f.json_schema minimum: 1, maximum: 12 |
| 72 | + end |
| 73 | + |
| 74 | + t.index "cards" |
| 75 | + end |
| 76 | +end |
| 77 | +``` |
| 78 | + |
| 79 | +On fields, `nullable: false` disallows `null` in indexing events while keeping the GraphQL field nullable: |
| 80 | + |
| 81 | +```ruby |
| 82 | +# in config/schema/widget.rb |
| 83 | + |
| 84 | +ElasticGraph.define_schema do |schema| |
| 85 | + schema.object_type "Widget" do |t| |
| 86 | + t.field "id", "ID!" |
| 87 | + |
| 88 | + t.field "name", "String" do |f| |
| 89 | + f.json_schema nullable: false |
| 90 | + end |
| 91 | + |
| 92 | + t.index "widgets" |
| 93 | + end |
| 94 | +end |
| 95 | +``` |
7 | 96 |
|
8 | 97 | ## Dependency Diagram |
9 | 98 |
|
|
0 commit comments