|
| 1 | +# Copyright 2024 - 2026 Block, Inc. |
| 2 | +# |
| 3 | +# Use of this source code is governed by an MIT-style |
| 4 | +# license that can be found in the LICENSE file or at |
| 5 | +# https://opensource.org/licenses/MIT. |
| 6 | +# |
| 7 | +# frozen_string_literal: true |
| 8 | + |
| 9 | +require "elastic_graph/proto_ingestion/schema_definition/api_extension" |
| 10 | + |
| 11 | +module ElasticGraph |
| 12 | + module ProtoIngestion |
| 13 | + module SchemaDefinition |
| 14 | + # Covers which types get a message: the types a publisher can send events for, and every type |
| 15 | + # they reference. |
| 16 | + RSpec.describe Schema, "ingestible types" do |
| 17 | + it "generates a message for a `sourced_from` source type that has no index of its own" do |
| 18 | + proto = define_proto_schema do |s| |
| 19 | + define_component_type(s) |
| 20 | + |
| 21 | + s.object_type "ComponentDesign" do |t| |
| 22 | + t.field "id", "ID!" |
| 23 | + t.field "component_id", "ID" |
| 24 | + t.field "designer_name", "String" |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + expect(proto_type_def_from(proto, "ComponentDesign")).to eq(<<~PROTO.strip) |
| 29 | + message ComponentDesign { |
| 30 | + optional string id = 1; |
| 31 | + optional string component_id = 2; |
| 32 | + optional string designer_name = 3; |
| 33 | + // Next field number: 4 |
| 34 | + } |
| 35 | + PROTO |
| 36 | + end |
| 37 | + |
| 38 | + it "generates a message for a type referenced by a non-indexed source type" do |
| 39 | + proto = define_proto_schema do |s| |
| 40 | + define_component_type(s) |
| 41 | + |
| 42 | + s.object_type "ComponentDesign" do |t| |
| 43 | + t.field "id", "ID!" |
| 44 | + t.field "component_id", "ID" |
| 45 | + t.field "designer_name", "String" |
| 46 | + t.field "studio", "DesignStudio" |
| 47 | + end |
| 48 | + |
| 49 | + s.object_type "DesignStudio" do |t| |
| 50 | + t.field "name", "String" |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + expect(proto_type_def_from(proto, "DesignStudio")).to eq(<<~PROTO.strip) |
| 55 | + message DesignStudio { |
| 56 | + optional string name = 1; |
| 57 | + // Next field number: 2 |
| 58 | + } |
| 59 | + PROTO |
| 60 | + end |
| 61 | + |
| 62 | + it "generates a oneof wrapper and subtype messages for an abstract source type" do |
| 63 | + proto = define_proto_schema do |s| |
| 64 | + define_component_type(s) |
| 65 | + |
| 66 | + s.interface_type "ComponentDesign" do |t| |
| 67 | + t.field "id", "ID!" |
| 68 | + t.field "component_id", "ID" |
| 69 | + t.field "designer_name", "String" |
| 70 | + end |
| 71 | + |
| 72 | + s.object_type "InternalComponentDesign" do |t| |
| 73 | + t.implements "ComponentDesign" |
| 74 | + t.field "id", "ID!" |
| 75 | + t.field "component_id", "ID" |
| 76 | + t.field "designer_name", "String" |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + expect(proto_type_def_from(proto, "ComponentDesign")).to eq(<<~PROTO.strip) |
| 81 | + message ComponentDesign { |
| 82 | + oneof value { |
| 83 | + .elasticgraph.InternalComponentDesign internal_component_design = 1; |
| 84 | + } |
| 85 | + // Next field number: 2 |
| 86 | + } |
| 87 | + PROTO |
| 88 | + |
| 89 | + expect(proto_type_def_from(proto, "InternalComponentDesign")).to eq(<<~PROTO.strip) |
| 90 | + message InternalComponentDesign { |
| 91 | + optional string id = 1; |
| 92 | + optional string component_id = 2; |
| 93 | + optional string designer_name = 3; |
| 94 | + // Next field number: 4 |
| 95 | + } |
| 96 | + PROTO |
| 97 | + end |
| 98 | + |
| 99 | + it "generates no message for a derived indexing type, since no publisher sends its events" do |
| 100 | + proto = define_proto_schema do |s| |
| 101 | + s.object_type "Widget" do |t| |
| 102 | + t.field "id", "ID!" |
| 103 | + t.field "workspace_id", "ID" |
| 104 | + t.field "name", "String" |
| 105 | + t.index "widgets" |
| 106 | + |
| 107 | + t.derive_indexed_type_fields "WidgetWorkspace", from_id: "workspace_id" do |derive| |
| 108 | + derive.immutable_value "name", from: "name" |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + s.object_type "WidgetWorkspace" do |t| |
| 113 | + t.field "id", "ID!" |
| 114 | + t.field "name", "String" |
| 115 | + t.index "widget_workspaces" |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + expect(proto_types_defined_in(proto)).to contain_exactly("Widget") |
| 120 | + end |
| 121 | + |
| 122 | + it "generates no message for a type that no ingestible type references" do |
| 123 | + proto = define_proto_schema do |s| |
| 124 | + s.object_type "Widget" do |t| |
| 125 | + t.field "id", "ID!" |
| 126 | + t.field "name", "String" |
| 127 | + t.index "widgets" |
| 128 | + end |
| 129 | + |
| 130 | + s.object_type "Unreferenced" do |t| |
| 131 | + t.field "id", "ID!" |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + expect(proto_types_defined_in(proto)).to contain_exactly("Widget") |
| 136 | + end |
| 137 | + |
| 138 | + # Defines an indexed type with a top-level `sourced_from` field fed by `ComponentDesign`. |
| 139 | + def define_component_type(schema) |
| 140 | + schema.object_type "Component" do |t| |
| 141 | + t.field "id", "ID!" |
| 142 | + t.field "name", "String" |
| 143 | + |
| 144 | + t.field "designer_name", "String" do |f| |
| 145 | + f.sourced_from "design", "designer_name" |
| 146 | + end |
| 147 | + |
| 148 | + t.relates_to_one "design", "ComponentDesign", via: "component_id", dir: :in, indexing_only: true |
| 149 | + |
| 150 | + t.index "components" do |i| |
| 151 | + i.has_had_multiple_sources! |
| 152 | + end |
| 153 | + end |
| 154 | + end |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | +end |
0 commit comments