@powerhousedao/switchboard 6.2.3-dev.101 → 6.2.3-dev.103

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## 6.2.3-dev.103 (2026-11-12)
2
+
3
+ This was a version bump only for @powerhousedao/switchboard to align it with other projects, there were no code changes.
4
+
5
+ ## 6.2.3-dev.102 (2026-11-11)
6
+
7
+ ### 🚀 Features
8
+
9
+ - **reactor-api:** serve code-first Subgraph classes beside BaseSubgraph ([a46e86b443](https://github.com/powerhouse-inc/powerhouse/commit/a46e86b443))
10
+ - **document-model:** add the code-first subgraph definition layer and checks ([ed3a0ddb00](https://github.com/powerhouse-inc/powerhouse/commit/ed3a0ddb00))
11
+
12
+ ### ❤️ Thank You
13
+
14
+ - Yasiel Cabrera @YasielCabrera
15
+
1
16
  ## 6.2.3-dev.101 (2026-11-10)
2
17
 
3
18
  ### 🩹 Fixes
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@powerhousedao/switchboard",
3
3
  "type": "module",
4
- "version": "6.2.3-dev.101",
4
+ "version": "6.2.3-dev.103",
5
5
  "main": "dist/index.mjs",
6
6
  "exports": {
7
7
  ".": {
@@ -94,23 +94,23 @@
94
94
  "@opentelemetry/sdk-node": "^0.218.0",
95
95
  "@opentelemetry/sdk-trace-base": "^2.7.1",
96
96
  "@opentelemetry/semantic-conventions": "^1.41.1",
97
- "@powerhousedao/config": "6.2.3-dev.101",
98
- "@powerhousedao/host-core": "6.2.3-dev.101",
99
- "@powerhousedao/opentelemetry-instrumentation-reactor": "6.2.3-dev.101",
100
- "@powerhousedao/reactor": "6.2.3-dev.101",
101
- "@powerhousedao/reactor-api": "6.2.3-dev.101",
102
- "@powerhousedao/reactor-attachments": "6.2.3-dev.101",
103
- "@powerhousedao/reactor-drive": "6.2.3-dev.101",
104
- "@powerhousedao/reactor-group": "6.2.3-dev.101",
105
- "@powerhousedao/reactor-privacy": "6.2.3-dev.101",
106
- "@powerhousedao/reactor-workflow": "6.2.3-dev.101",
107
- "@powerhousedao/shared": "6.2.3-dev.101",
108
- "@powerhousedao/workflow": "6.2.3-dev.101",
97
+ "@powerhousedao/config": "6.2.3-dev.103",
98
+ "@powerhousedao/host-core": "6.2.3-dev.103",
99
+ "@powerhousedao/opentelemetry-instrumentation-reactor": "6.2.3-dev.103",
100
+ "@powerhousedao/reactor": "6.2.3-dev.103",
101
+ "@powerhousedao/reactor-api": "6.2.3-dev.103",
102
+ "@powerhousedao/reactor-attachments": "6.2.3-dev.103",
103
+ "@powerhousedao/reactor-drive": "6.2.3-dev.103",
104
+ "@powerhousedao/reactor-group": "6.2.3-dev.103",
105
+ "@powerhousedao/reactor-privacy": "6.2.3-dev.103",
106
+ "@powerhousedao/reactor-workflow": "6.2.3-dev.103",
107
+ "@powerhousedao/shared": "6.2.3-dev.103",
108
+ "@powerhousedao/workflow": "6.2.3-dev.103",
109
109
  "@pyroscope/nodejs": "^0.4.5",
110
- "@renown/sdk": "6.2.3-dev.101",
110
+ "@renown/sdk": "6.2.3-dev.103",
111
111
  "@sentry/node": "^10.53.1",
112
112
  "@sentry/opentelemetry": "^10.53.1",
113
- "document-model": "6.2.3-dev.101",
113
+ "document-model": "6.2.3-dev.103",
114
114
  "dotenv": "^16.4.7",
115
115
  "express": "^4.22.1",
116
116
  "graphql": "^16",
@@ -126,7 +126,7 @@
126
126
  "@openfeature/core": "1.9.1",
127
127
  "@openfeature/env-var-provider": "0.3.1",
128
128
  "@openfeature/server-sdk": "1.19.0",
129
- "@powerhousedao/vetra": "6.2.3-dev.101",
129
+ "@powerhousedao/vetra": "6.2.3-dev.103",
130
130
  "@types/express": "^4.17.25",
131
131
  "@types/node": "25.2.3",
132
132
  "@types/pg": "8.16.0",
@@ -138,7 +138,7 @@
138
138
  "vitest": "5.0.3"
139
139
  },
140
140
  "peerDependencies": {
141
- "@powerhousedao/vetra": "6.2.3-dev.101",
141
+ "@powerhousedao/vetra": "6.2.3-dev.103",
142
142
  "react": ">=19.0.0",
143
143
  "vite": "^8.0.0"
144
144
  },
@@ -0,0 +1,16 @@
1
+ import { BaseSubgraph } from "@powerhousedao/reactor-api";
2
+ import { gql } from "graphql-tag";
3
+
4
+ export class GreetingsSubgraph extends BaseSubgraph {
5
+ override name = "greetings";
6
+ override typeDefs = gql`
7
+ type Query {
8
+ greeting(to: String!): String!
9
+ }
10
+ `;
11
+ override resolvers = {
12
+ Query: {
13
+ greeting: (_: unknown, args: { to: string }) => `Hello, ${args.to}`,
14
+ },
15
+ };
16
+ }
@@ -0,0 +1,3 @@
1
+ // The shape codegen's subgraphs index emits, one export per style.
2
+ export * as GreetingsSubgraph from "./greetings/index.js";
3
+ export * as TallySubgraph from "./tally/index.js";
@@ -0,0 +1,73 @@
1
+ import { type SchemaBuilder, Subgraph } from "@powerhousedao/reactor-api";
2
+ import { ph } from "document-model";
3
+
4
+ const Tally = ph.object("Tally", {
5
+ fields: {
6
+ id: ph.ID({ required: true }),
7
+ count: ph.Int({ required: true }),
8
+ },
9
+ });
10
+
11
+ type TallyRecord = { id: string; count: number };
12
+
13
+ export class TallySubgraph extends Subgraph {
14
+ static override readonly subgraphName = "tally";
15
+
16
+ readonly #tallies = new Map<string, number>();
17
+ readonly #listeners = new Set<() => void>();
18
+
19
+ static override getSchema(schema: SchemaBuilder<TallySubgraph>) {
20
+ return [
21
+ schema.query("tally", {
22
+ args: { id: ph.ID({ required: true }) },
23
+ type: ph.ref(Tally, { required: true }),
24
+ resolve: ({ args, subgraph }) => subgraph.#read(args.id),
25
+ }),
26
+ schema.mutation("incrementTally", {
27
+ args: {
28
+ id: ph.ID({ required: true }),
29
+ by: ph.Int({ required: true }),
30
+ },
31
+ type: ph.ref(Tally, { required: true }),
32
+ resolve: ({ args, subgraph }) => subgraph.#increment(args.id, args.by),
33
+ }),
34
+ schema.field(Tally, "doubled", {
35
+ type: ph.Int({ required: true }),
36
+ resolve: ({ parent }) => parent.count * 2,
37
+ }),
38
+ schema.subscription("tallyChanged", {
39
+ args: { id: ph.ID({ required: true }) },
40
+ type: ph.ref(Tally, { required: true }),
41
+ subscribe: ({ args, subgraph }) => subgraph.#watch(args.id),
42
+ resolve: ({ parent }) => parent,
43
+ }),
44
+ ];
45
+ }
46
+
47
+ #read(id: string): TallyRecord {
48
+ return { id, count: this.#tallies.get(id) ?? 0 };
49
+ }
50
+
51
+ #increment(id: string, by: number): TallyRecord {
52
+ this.#tallies.set(id, this.#read(id).count + by);
53
+ for (const notify of this.#listeners) notify();
54
+ return this.#read(id);
55
+ }
56
+
57
+ /** The tally now, then again after each change. */
58
+ async *#watch(id: string): AsyncGenerator<TallyRecord> {
59
+ let changed = Promise.resolve();
60
+ let notify = () => {};
61
+ const listener = () => notify();
62
+ this.#listeners.add(listener);
63
+ try {
64
+ for (;;) {
65
+ await changed;
66
+ changed = new Promise<void>((resolve) => (notify = resolve));
67
+ yield this.#read(id);
68
+ }
69
+ } finally {
70
+ this.#listeners.delete(listener);
71
+ }
72
+ }
73
+ }
@@ -0,0 +1,251 @@
1
+ /**
2
+ * One package whose subgraphs index exports a BaseSubgraph subclass and a
3
+ * code-first Subgraph subclass, loaded from `packages.list` the way a local
4
+ * project is. Both serve through the switchboard supergraph: the schema-first
5
+ * query, the code-first query and mutation, the code-first subscription
6
+ * beside the built-in `r` subgraph's on the supergraph's SSE stream, and
7
+ * `r`'s over the WebSocket endpoint.
8
+ */
9
+ import { documentModelDocumentModelModule, type ILogger } from "document-model";
10
+ import { mkdtemp, rm, writeFile } from "node:fs/promises";
11
+ import { tmpdir } from "node:os";
12
+ import { join } from "node:path";
13
+ import { fileURLToPath } from "node:url";
14
+ import {
15
+ afterAll,
16
+ afterEach,
17
+ beforeAll,
18
+ describe,
19
+ expect,
20
+ it,
21
+ vi,
22
+ } from "vitest";
23
+ import type { startSwitchboard } from "../src/server.mjs";
24
+ import { bootSwitchboard } from "./boot-helpers.js";
25
+
26
+ const FIXTURE = fileURLToPath(
27
+ new URL("./fixtures/mixed-subgraphs", import.meta.url),
28
+ );
29
+
30
+ function stubLogger(): ILogger {
31
+ const logger = {
32
+ verbose: vi.fn(),
33
+ debug: vi.fn(),
34
+ info: vi.fn(),
35
+ warn: vi.fn(),
36
+ error: vi.fn(),
37
+ child: vi.fn(),
38
+ };
39
+ logger.child.mockReturnValue(logger);
40
+ return logger as unknown as ILogger;
41
+ }
42
+
43
+ let tempRoot: string;
44
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
45
+ let origin: string;
46
+ const cleanups: (() => void)[] = [];
47
+
48
+ beforeAll(async () => {
49
+ tempRoot = await mkdtemp(join(tmpdir(), "switchboard-mixed-package-"));
50
+ const configFile = join(tempRoot, "powerhouse.config.json");
51
+ await writeFile(configFile, "{}");
52
+ switchboard = await bootSwitchboard(
53
+ {
54
+ server: { port: 0 },
55
+ storage: {
56
+ reactor: { url: join(tempRoot, "reactor") },
57
+ readModel: { url: join(tempRoot, "read-model") },
58
+ userDataDir: tempRoot,
59
+ },
60
+ features: { workflows: false, mcp: false },
61
+ packages: { disableLocal: true, list: [FIXTURE] },
62
+ identity: { keypairPath: join(tempRoot, "identity.json") },
63
+ configFile,
64
+ },
65
+ { logger: stubLogger() },
66
+ {},
67
+ );
68
+ origin = `http://127.0.0.1:${switchboard.port}`;
69
+ }, 120_000);
70
+
71
+ afterEach(() => {
72
+ for (const cleanup of cleanups.splice(0)) cleanup();
73
+ });
74
+
75
+ afterAll(async () => {
76
+ try {
77
+ await switchboard?.shutdown();
78
+ } finally {
79
+ await rm(tempRoot, { recursive: true, force: true });
80
+ }
81
+ });
82
+
83
+ async function post(query: string): Promise<unknown> {
84
+ const response = await fetch(`${origin}/graphql`, {
85
+ method: "POST",
86
+ headers: { "content-type": "application/json" },
87
+ body: JSON.stringify({ query }),
88
+ });
89
+ return response.json();
90
+ }
91
+
92
+ /** Every `next` payload of a subscription over the graphql-transport-ws endpoint. */
93
+ function overWebSocket(query: string): unknown[] {
94
+ const events: unknown[] = [];
95
+ const socket = new WebSocket(
96
+ `ws://127.0.0.1:${switchboard!.port}/graphql/subscriptions`,
97
+ "graphql-transport-ws",
98
+ );
99
+ socket.onopen = () =>
100
+ socket.send(JSON.stringify({ type: "connection_init", payload: {} }));
101
+ socket.onmessage = ({ data }) => {
102
+ const message = JSON.parse(String(data)) as {
103
+ type: string;
104
+ payload?: unknown;
105
+ };
106
+ if (message.type === "connection_ack") {
107
+ socket.send(
108
+ JSON.stringify({ id: "1", type: "subscribe", payload: { query } }),
109
+ );
110
+ } else if (message.type === "next") {
111
+ events.push(message.payload);
112
+ } else if (message.type === "error") {
113
+ events.push({ error: message.payload });
114
+ }
115
+ };
116
+ cleanups.push(() => socket.close());
117
+ return events;
118
+ }
119
+
120
+ /** Every `next` payload of a subscription over the supergraph's SSE stream. */
121
+ function overSse(query: string): unknown[] {
122
+ const events: unknown[] = [];
123
+ const controller = new AbortController();
124
+ cleanups.push(() => controller.abort());
125
+ void (async () => {
126
+ const response = await fetch(`${origin}/graphql/stream`, {
127
+ method: "POST",
128
+ headers: {
129
+ "content-type": "application/json",
130
+ accept: "text/event-stream",
131
+ },
132
+ body: JSON.stringify({ query }),
133
+ signal: controller.signal,
134
+ });
135
+ const decoder = new TextDecoder();
136
+ let text = "";
137
+ for await (const chunk of response.body!) {
138
+ text += decoder.decode(chunk as Uint8Array, { stream: true });
139
+ const frames = text.split("\n\n");
140
+ text = frames.pop() ?? "";
141
+ for (const frame of frames) {
142
+ const data = /^data: (.*)$/m.exec(frame)?.[1];
143
+ if (frame.startsWith("event: next") && data !== undefined) {
144
+ events.push(JSON.parse(data));
145
+ }
146
+ }
147
+ }
148
+ })().catch(() => undefined);
149
+ return events;
150
+ }
151
+
152
+ const until = (events: unknown[], count: number) =>
153
+ vi.waitFor(() => expect(events.length).toBeGreaterThanOrEqual(count), {
154
+ timeout: 15_000,
155
+ interval: 50,
156
+ });
157
+
158
+ const DOCUMENT_CHANGES = `subscription {
159
+ documentChanges(search: { type: "powerhouse/document-model" }) {
160
+ type
161
+ documents { id name documentType }
162
+ }
163
+ }`;
164
+
165
+ /** Creates documents named `<prefix>-<n>` until `events` holds one, and checks it is that creation. */
166
+ async function expectDocumentCreation(events: unknown[], prefix: string) {
167
+ const created = new Map<string, string>();
168
+ // A subscription is live some time after it is sent, and nothing tells
169
+ // the client when, so create a document until one arrives.
170
+ let n = 0;
171
+ await vi.waitFor(
172
+ async () => {
173
+ if (events.length === 0) {
174
+ const document =
175
+ documentModelDocumentModelModule.utils.createDocument();
176
+ document.header.name = `${prefix}-${n++}`;
177
+ const stored = await switchboard!.reactor.create(document);
178
+ created.set(document.header.name, stored.header.id);
179
+ }
180
+ expect(events.length).toBeGreaterThan(0);
181
+ },
182
+ { timeout: 15_000, interval: 200 },
183
+ );
184
+ const event = events[0] as {
185
+ data: { documentChanges: { documents: { name: string }[] } };
186
+ };
187
+ const name = event.data.documentChanges.documents[0]?.name ?? "";
188
+ expect(name).toMatch(new RegExp(`^${prefix}-\\d+$`));
189
+ expect(event).toEqual({
190
+ data: {
191
+ documentChanges: {
192
+ type: "CREATED",
193
+ documents: [
194
+ {
195
+ id: created.get(name),
196
+ name,
197
+ documentType: "powerhouse/document-model",
198
+ },
199
+ ],
200
+ },
201
+ },
202
+ });
203
+ }
204
+
205
+ describe("a package with a schema-first and a code-first subgraph", () => {
206
+ it("answers the schema-first query through the supergraph", async () => {
207
+ expect(await post('{ greeting(to: "Ada") }')).toEqual({
208
+ data: { greeting: "Hello, Ada" },
209
+ });
210
+ });
211
+
212
+ it("answers the code-first query and mutation through the supergraph", async () => {
213
+ expect(await post('{ tally(id: "q") { id count doubled } }')).toEqual({
214
+ data: { tally: { id: "q", count: 0, doubled: 0 } },
215
+ });
216
+ expect(
217
+ await post(
218
+ 'mutation { incrementTally(id: "q", by: 3) { id count doubled } }',
219
+ ),
220
+ ).toEqual({
221
+ data: { incrementTally: { id: "q", count: 3, doubled: 6 } },
222
+ });
223
+ expect(await post('{ tally(id: "q") { id count doubled } }')).toEqual({
224
+ data: { tally: { id: "q", count: 3, doubled: 6 } },
225
+ });
226
+ });
227
+ });
228
+
229
+ describe("subscriptions through the supergraph", () => {
230
+ it("delivers the tally, its change and an r document creation over SSE", async () => {
231
+ const id = "sub-SSE";
232
+ const tally = overSse(
233
+ `subscription { tallyChanged(id: "${id}") { id count doubled } }`,
234
+ );
235
+ const documents = overSse(DOCUMENT_CHANGES);
236
+ await until(tally, 1);
237
+ expect(
238
+ await post(`mutation { incrementTally(id: "${id}", by: 2) { count } }`),
239
+ ).toEqual({ data: { incrementTally: { count: 2 } } });
240
+ await until(tally, 2);
241
+ expect(tally).toEqual([
242
+ { data: { tallyChanged: { id, count: 0, doubled: 0 } } },
243
+ { data: { tallyChanged: { id, count: 2, doubled: 4 } } },
244
+ ]);
245
+ await expectDocumentCreation(documents, "SSE");
246
+ }, 30_000);
247
+
248
+ it("delivers an r document creation over WebSocket", async () => {
249
+ await expectDocumentCreation(overWebSocket(DOCUMENT_CHANGES), "WebSocket");
250
+ }, 30_000);
251
+ });