|
| 1 | +# ElasticGraph::Protobuf |
| 2 | + |
| 3 | +An ElasticGraph extension that generates Protocol Buffers (`proto3`) schema artifacts from ElasticGraph schemas. |
| 4 | + |
| 5 | +## Dependency Diagram |
| 6 | + |
| 7 | +```mermaid |
| 8 | +graph LR; |
| 9 | + classDef targetGemStyle fill:#FADBD8,stroke:#EC7063,color:#000,stroke-width:2px; |
| 10 | + classDef otherEgGemStyle fill:#A9DFBF,stroke:#2ECC71,color:#000; |
| 11 | + classDef externalGemStyle fill:#E0EFFF,stroke:#70A1D7,color:#2980B9; |
| 12 | + elasticgraph-protobuf["elasticgraph-protobuf"]; |
| 13 | + class elasticgraph-protobuf targetGemStyle; |
| 14 | + elasticgraph-support["elasticgraph-support"]; |
| 15 | + elasticgraph-protobuf --> elasticgraph-support; |
| 16 | + class elasticgraph-support otherEgGemStyle; |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +First, add `elasticgraph-protobuf` to your `Gemfile`, alongside the other ElasticGraph gems: |
| 22 | + |
| 23 | +```diff |
| 24 | +diff --git a/Gemfile b/Gemfile |
| 25 | +index 4a5ef1e..5c16c2b 100644 |
| 26 | +--- a/Gemfile |
| 27 | ++++ b/Gemfile |
| 28 | +@@ -8,6 +8,7 @@ gem "elasticgraph-query_registry", *elasticgraph_details |
| 29 | + |
| 30 | + # Can be elasticgraph-elasticsearch or elasticgraph-opensearch based on the datastore you want to use. |
| 31 | + gem "elasticgraph-opensearch", *elasticgraph_details |
| 32 | ++gem "elasticgraph-protobuf", *elasticgraph_details |
| 33 | + |
| 34 | + gem "httpx", "~> 1.3" |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +Next, update your `Rakefile` so that `ElasticGraph::Protobuf::SchemaDefinition::APIExtension` is |
| 39 | +included in the schema-definition extension modules: |
| 40 | + |
| 41 | +```diff |
| 42 | +diff --git a/Rakefile b/Rakefile |
| 43 | +index 2943335..26633c3 100644 |
| 44 | +--- a/Rakefile |
| 45 | ++++ b/Rakefile |
| 46 | +@@ -1,5 +1,6 @@ |
| 47 | + project_root = File.expand_path(__dir__) |
| 48 | + |
| 49 | ++require "elastic_graph/protobuf/schema_definition/api_extension" |
| 50 | + require "elastic_graph/local/rake_tasks" |
| 51 | + require "elastic_graph/query_registry/rake_tasks" |
| 52 | + require "rspec/core/rake_task" |
| 53 | +@@ -12,6 +13,8 @@ ElasticGraph::Local::RakeTasks.new( |
| 54 | + local_config_yaml: settings_file, |
| 55 | + path_to_schema: "#{project_root}/config/schema.rb" |
| 56 | + ) do |tasks| |
| 57 | ++ tasks.schema_definition_extension_modules << ElasticGraph::Protobuf::SchemaDefinition::APIExtension |
| 58 | ++ |
| 59 | + # Set this to true once you're beyond the prototyping stage. |
| 60 | + tasks.enforce_json_schema_version = false |
| 61 | + |
| 62 | +``` |
| 63 | + |
| 64 | +Then opt into proto generation from your schema definition: |
| 65 | + |
| 66 | +```ruby |
| 67 | +# in config/schema/protobuf.rb |
| 68 | + |
| 69 | +ElasticGraph.define_schema do |schema| |
| 70 | + schema.proto_schema_artifacts package_name: "myapp.events.v1" |
| 71 | +end |
| 72 | +``` |
| 73 | + |
| 74 | +After running `bundle exec rake schema_artifacts:dump`, ElasticGraph will generate: |
| 75 | + |
| 76 | +- `schema.proto` |
| 77 | +- `proto_field_numbers.yaml` |
| 78 | + |
| 79 | +## Schema Definition Options |
| 80 | + |
| 81 | +### Custom Scalar Types |
| 82 | + |
| 83 | +Built-in ElasticGraph scalar types are automatically mapped to proto scalar types. |
| 84 | +For custom scalar types, the generator infers proto scalar types from `json_schema type:` when it is one |
| 85 | +of `string`, `boolean`, `number`, or `integer`. You can override inference with `proto_field`: |
| 86 | + |
| 87 | +```ruby |
| 88 | +# in config/schema/money.rb |
| 89 | + |
| 90 | +ElasticGraph.define_schema do |schema| |
| 91 | + schema.scalar_type "Money" do |t| |
| 92 | + t.mapping type: "long" |
| 93 | + t.json_schema type: "integer" |
| 94 | + t.proto_field type: "int64" |
| 95 | + end |
| 96 | +end |
| 97 | +``` |
| 98 | + |
| 99 | +### Sourcing Enum Values From Existing Protobuf Mappings |
| 100 | + |
| 101 | +If your project already maintains GraphQL-to-proto enum mappings (for example in tests), |
| 102 | +you can reuse them for proto schema generation: |
| 103 | + |
| 104 | +```ruby |
| 105 | +# in config/schema/proto_enum_mappings.rb |
| 106 | + |
| 107 | +ElasticGraph.define_schema do |schema| |
| 108 | + schema.proto_enum_mappings( |
| 109 | + SalesEg::ProtoEnumMappings::PROTO_ENUMS_BY_GRAPHQL_ENUM |
| 110 | + ) if defined?(SalesEg::ProtoEnumMappings) |
| 111 | +end |
| 112 | +``` |
| 113 | + |
| 114 | +When a mapping exists for an enum, `elasticgraph-protobuf` uses the mapped proto enum(s) |
| 115 | +as the source of enum values (respecting `exclusions`, `expected_extras`, and `name_transform`). |
| 116 | + |
| 117 | +### Stable Field Numbers |
| 118 | + |
| 119 | +`schema_artifacts:dump` automatically reads and writes `proto_field_numbers.yaml` |
| 120 | +in the schema artifacts directory. Existing numbers stay fixed even if field order |
| 121 | +changes, and new fields get the next available numbers. |
| 122 | + |
| 123 | +`schema.proto` always uses the public GraphQL field names. When a field uses a |
| 124 | +different `name_in_index`, the sidecar YAML stores that override privately: |
| 125 | + |
| 126 | +```yaml |
| 127 | +messages: |
| 128 | + Widget: |
| 129 | + fields: |
| 130 | + id: 1 |
| 131 | + display_name: |
| 132 | + field_number: 2 |
| 133 | + name_in_index: displayName |
| 134 | +``` |
| 135 | +
|
| 136 | +If a field is renamed with `field.renamed_from`, `elasticgraph-protobuf` reuses the |
| 137 | +existing field number under the new public field name. |
| 138 | + |
| 139 | +## Type Mappings |
| 140 | + |
| 141 | +The generated `schema.proto` uses these built-in scalar mappings: |
| 142 | + |
| 143 | +| ElasticGraph Type | Protobuf Type | |
| 144 | +|-------------------|------------| |
| 145 | +| `Boolean` | `bool` | |
| 146 | +| `Cursor` | `string` | |
| 147 | +| `Date` | `string` | |
| 148 | +| `DateTime` | `string` | |
| 149 | +| `Float` | `double` | |
| 150 | +| `ID` | `string` | |
| 151 | +| `Int` | `int32` | |
| 152 | +| `JsonSafeLong` | `int64` | |
| 153 | +| `LocalTime` | `string` | |
| 154 | +| `LongString` | `int64` | |
| 155 | +| `String` | `string` | |
| 156 | +| `TimeZone` | `string` | |
| 157 | +| `Untyped` | `string` | |
| 158 | + |
| 159 | +Additionally: |
| 160 | +- List types become `repeated` fields. |
| 161 | +- Nested list types generate wrapper messages so the output remains valid `proto3`. |
| 162 | +- Enum types generate `enum` definitions whose values are prefixed with the enum type name in `UPPER_SNAKE_CASE`, including a zero-valued `*_UNSPECIFIED` entry. |
0 commit comments