-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgenerate-schema-docs.ts
More file actions
322 lines (296 loc) · 9.37 KB
/
Copy pathgenerate-schema-docs.ts
File metadata and controls
322 lines (296 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* Generates docs/schema.md — a human-readable reference for every entity and
* field defined in schema.graphql.
*
* The GraphQL schema is the source of truth and is already richly commented,
* so this script parses those comments rather than duplicating them by hand.
* Re-run it whenever schema.graphql changes so the reference stays in lockstep.
*
* Usage:
* pnpm tsx scripts/generate-schema-docs.ts
* # (or, on Node 22.6+: node scripts/generate-schema-docs.ts)
*/
import { readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(__dirname, "..");
const schemaPath = path.join(repoRoot, "schema.graphql");
const outPath = path.join(repoRoot, "docs", "schema.md");
interface Field {
name: string;
type: string;
indexed: boolean;
derived: boolean;
description: string;
}
interface Entity {
name: string;
description: string;
fields: Field[];
}
/** Strips a leading `#` and one optional space from a comment line. */
function stripHash(line: string): string {
return line.replace(/^\s*#\s?/, "").trimEnd();
}
/**
* Joins a run of preceding comment lines into a single description, dropping a
* leading section-header line (e.g. "CL Pool specific fields", "Swap metrics").
*/
function joinComment(lines: string[]): string {
const out = [...lines];
while (
out.length > 0 &&
/(?:fields|metrics)$/i.test(out[0]) &&
out[0].split(/\s+/).length <= 5
) {
out.shift();
}
return out.join(" ").replace(/\s+/g, " ").trim();
}
/** Parses schema.graphql into a list of entities with their fields. */
function parseSchema(src: string): Entity[] {
const lines = src.split("\n");
const entities: Entity[] = [];
let current: Entity | null = null;
let pending: string[] = []; // contiguous comment lines awaiting a field/type
for (const raw of lines) {
const line = raw.trim();
if (line === "") {
pending = [];
continue;
}
const typeMatch = line.match(/^type\s+(\w+)\s*\{/);
if (typeMatch) {
current = {
name: typeMatch[1],
description: joinComment(pending),
fields: [],
};
entities.push(current);
pending = [];
continue;
}
if (line.startsWith("}")) {
current = null;
pending = [];
continue;
}
if (line.startsWith("#")) {
pending.push(stripHash(line));
continue;
}
if (!current) continue;
// A directive continuation on its own line (e.g. @derivedFrom on the next line).
if (line.startsWith("@")) {
const last = current.fields[current.fields.length - 1];
if (last && /@derivedFrom/.test(line)) last.derived = true;
if (last && /@index\b/.test(line)) last.indexed = true;
continue;
}
const fieldMatch = line.match(/^(\w+):\s*(.+)$/);
if (fieldMatch) {
const name = fieldMatch[1];
const rest = fieldMatch[2];
const hashIdx = rest.indexOf("#");
const inline = hashIdx >= 0 ? rest.slice(hashIdx + 1).trim() : "";
const decl = (hashIdx >= 0 ? rest.slice(0, hashIdx) : rest).trim();
const type = (decl.match(/^([^\s@]+)/)?.[1] ?? decl).trim();
current.fields.push({
name,
type,
indexed: /@index\b/.test(decl),
derived: /@derivedFrom/.test(decl),
description: inline || joinComment(pending),
});
pending = [];
}
}
return entities;
}
// Category → ordered list of entity names. Entities not listed fall into "Other".
const CATEGORIES: { title: string; blurb: string; names: string[] }[] = [
{
title: "Core aggregates",
blurb:
"Latest-state entities that hold the headline metrics most consumers query.",
names: [
"Pool",
"Token",
"UserStatsPerPool",
"NonFungiblePosition",
"VeNFTState",
"VeNFTPoolVote",
"ALM_LP_Wrapper",
],
},
{
title: "Snapshots",
blurb:
"Hourly, epoch-aligned copies of the core aggregates for historical/time-series queries.",
names: [
"PoolSnapshot",
"TokenPriceSnapshot",
"UserStatsPerPoolSnapshot",
"NonFungiblePositionSnapshot",
"VeNFTStateSnapshot",
"VeNFTPoolVoteSnapshot",
"ALM_LP_WrapperSnapshot",
],
},
{
title: "Config & registry",
blurb: "Chain-wide configuration and cross-chain mapping tables.",
names: [
"FactoryRegistryConfig",
"DynamicFeeGlobalConfig",
"CLGaugeConfig",
"FeeToTickSpacingMapping",
"RedistributorConfig",
"RootPool_LeafPool",
"RootGauge_RootPool",
],
},
{
title: "Pool launcher",
blurb: "Emerging-token pools created or migrated via the Pool Launcher.",
names: ["PoolLauncherPool", "PoolLauncherConfig"],
},
{
title: "Cross-chain superswaps (Hyperlane)",
blurb:
"oUSDT-bridged swaps and the Hyperlane dispatch/process events that correlate them.",
names: [
"SuperSwap",
"OUSDTBridgedTransaction",
"OUSDTSwaps",
"DispatchId_event",
"ProcessId_event",
],
},
{
title: "Internal buffers & deferred state",
blurb:
"Short-lived bookkeeping entities used to correlate events across log indices/blocks. Most are deleted once consumed; not intended for end-user queries.",
names: [
"PendingVote",
"CLPoolPendingInitialize",
"PendingRootPoolMapping",
"PendingDistribution",
"CLPoolMintEvent",
"CLPositionPendingPrincipal",
"TxCLPoolMintRegistry",
"TxPoolTransferRegistry",
"PoolTransferInTx",
"ALMLPWrapperTransferInTx",
"ALM_TotalSupplyLimitUpdated_event",
],
},
];
/** GitHub-style heading anchor slug. */
function slug(s: string): string {
return s
.toLowerCase()
.replace(/[^\w\- ]/g, "")
.replace(/ /g, "-");
}
/** Escapes a value for use inside a markdown table cell. */
function cell(s: string): string {
return s.replace(/\|/g, "\\|").replace(/\s+/g, " ").trim();
}
function renderEntity(e: Entity): string {
const out: string[] = [];
out.push(`### ${e.name}`);
out.push("");
if (e.description) {
out.push(e.description);
out.push("");
}
out.push("| Field | Type | Description |");
out.push("| ----- | ---- | ----------- |");
for (const f of e.fields) {
const tags: string[] = [];
if (f.indexed) tags.push("indexed");
if (f.derived) tags.push("derived");
const tag = tags.length ? ` _(${tags.join(", ")})_` : "";
out.push(
`| \`${f.name}\` | \`${f.type}\` | ${cell(f.description)}${tag} |`,
);
}
out.push("");
return out.join("\n");
}
function main(): void {
const src = readFileSync(schemaPath, "utf8");
const entities = parseSchema(src);
const byName = new Map(entities.map((e) => [e.name, e]));
// Bucket entities into categories, collecting any leftovers into "Other".
const seen = new Set<string>();
const buckets = CATEGORIES.map((c) => ({
...c,
entities: c.names
.map((n) => byName.get(n))
.filter((e): e is Entity => Boolean(e)),
}));
for (const b of buckets) for (const e of b.entities) seen.add(e.name);
const leftovers = entities.filter((e) => !seen.has(e.name));
if (leftovers.length > 0) {
buckets.push({
title: "Other",
blurb: "Entities not yet categorized in scripts/generate-schema-docs.ts.",
names: leftovers.map((e) => e.name),
entities: leftovers,
});
}
const out: string[] = [];
out.push("<!--");
out.push(" AUTO-GENERATED — do not edit by hand.");
out.push(" Source: schema.graphql");
out.push(" Regenerate: pnpm tsx scripts/generate-schema-docs.ts");
out.push("-->");
out.push("");
out.push("# Entity & field reference");
out.push("");
out.push(
`Generated from [\`schema.graphql\`](../schema.graphql). The schema defines **${entities.length} entity types**; each becomes a queryable table in the indexer's database.`,
);
out.push("");
out.push("## Reading this reference");
out.push("");
out.push(
"- **`Type`** is the GraphQL type. A trailing `!` means non-nullable.",
);
out.push(
"- **`BigInt`** fields are fixed-point integers (no JS number precision loss); divide by the relevant token's `decimals` (or `10^18` for USD/`WAD`-scaled values) to get a human number.",
);
out.push(
"- **`Timestamp`** is an ISO-8601 / epoch timestamp; **`Bytes`**/**`String`** hold addresses and hashes.",
);
out.push(
"- **_(indexed)_** marks fields with a secondary index (`@index`) — efficient to filter on. **_(derived)_** marks reverse-relation fields (`@derivedFrom`) that are computed, not stored.",
);
out.push(
"- Entity `id` formats and other conventions are documented inline in each `id` row. See the [Data model](../README.md#data-model) section of the README for the high-level map.",
);
out.push("");
out.push("## Contents");
out.push("");
for (const b of buckets) {
out.push(
`- **${b.title}** — ${b.entities.map((e) => `[${e.name}](#${slug(e.name)})`).join(", ")}`,
);
}
out.push("");
for (const b of buckets) {
out.push(`## ${b.title}`);
out.push("");
out.push(b.blurb);
out.push("");
for (const e of b.entities) out.push(renderEntity(e));
}
writeFileSync(outPath, `${out.join("\n").trimEnd()}\n`);
process.stdout.write(
`Wrote ${outPath} (${entities.length} entities, ${entities.reduce((n, e) => n + e.fields.length, 0)} fields)\n`,
);
}
main();