@powerhousedao/reactor-api 6.2.3-dev.92 → 6.2.3-dev.94
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/dist/{base-document-models-CNHrVpOU.mjs → base-document-models-577xnrmL.mjs} +510 -311
- package/dist/base-document-models-577xnrmL.mjs.map +1 -0
- package/dist/{create-schema-ClPWjRPY.d.mts → create-schema-BO0wYVr2.d.mts} +15 -11
- package/dist/create-schema-BO0wYVr2.d.mts.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +73 -33
- package/dist/index.mjs.map +1 -1
- package/dist/{package-subgraph-check-DdZLhOHW.mjs → package-subgraph-check-zi0YDhBj.mjs} +4 -4
- package/dist/{package-subgraph-check-DdZLhOHW.mjs.map → package-subgraph-check-zi0YDhBj.mjs.map} +1 -1
- package/dist/src/graphql/subgraph-types.d.mts +1 -1
- package/dist/src/graphql/subgraph-types.mjs +1 -1
- package/package.json +13 -13
- package/dist/base-document-models-CNHrVpOU.mjs.map +0 -1
- package/dist/create-schema-ClPWjRPY.d.mts.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
|
-
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="
|
|
3
|
-
import { childLogger, documentModelDocumentModelModule } from "document-model";
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="5196867a-cd79-5e29-9a40-a98ed396d70d")}catch(e){}}();
|
|
3
|
+
import { childLogger, documentModelDocumentModelModule, inspectDocumentModelDefinition, projectDocumentModelGraphQL } from "document-model";
|
|
4
4
|
import { gql } from "graphql-tag";
|
|
5
5
|
import { GraphQLScalarType, Kind, Lexer, Source, TokenKind, isTypeDefinitionNode, isTypeExtensionNode, parse, print, valueFromASTUntyped, visit } from "graphql";
|
|
6
6
|
import { createHash } from "node:crypto";
|
|
@@ -83,6 +83,217 @@ function registeredScalarType(definition) {
|
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
85
|
//#endregion
|
|
86
|
+
//#region src/graphql/document-model-schema/schema-ast.ts
|
|
87
|
+
const EMPTY_DOCUMENT = {
|
|
88
|
+
kind: Kind.DOCUMENT,
|
|
89
|
+
definitions: []
|
|
90
|
+
};
|
|
91
|
+
const OUTPUT_TYPE_KINDS = [
|
|
92
|
+
Kind.OBJECT_TYPE_DEFINITION,
|
|
93
|
+
Kind.OBJECT_TYPE_EXTENSION,
|
|
94
|
+
Kind.ENUM_TYPE_DEFINITION,
|
|
95
|
+
Kind.ENUM_TYPE_EXTENSION,
|
|
96
|
+
Kind.UNION_TYPE_DEFINITION,
|
|
97
|
+
Kind.UNION_TYPE_EXTENSION,
|
|
98
|
+
Kind.INTERFACE_TYPE_DEFINITION,
|
|
99
|
+
Kind.INTERFACE_TYPE_EXTENSION
|
|
100
|
+
];
|
|
101
|
+
const STATE_PREFIX_KINDS = new Set(OUTPUT_TYPE_KINDS);
|
|
102
|
+
const ALL_PREFIX_KINDS = /* @__PURE__ */ new Set([
|
|
103
|
+
...OUTPUT_TYPE_KINDS,
|
|
104
|
+
Kind.INPUT_OBJECT_TYPE_DEFINITION,
|
|
105
|
+
Kind.INPUT_OBJECT_TYPE_EXTENSION
|
|
106
|
+
]);
|
|
107
|
+
const TYPE_NAME_KINDS = /* @__PURE__ */ new Set([
|
|
108
|
+
...ALL_PREFIX_KINDS,
|
|
109
|
+
Kind.SCALAR_TYPE_DEFINITION,
|
|
110
|
+
Kind.SCALAR_TYPE_EXTENSION,
|
|
111
|
+
Kind.NAMED_TYPE
|
|
112
|
+
]);
|
|
113
|
+
const LOCAL_STATE_STUBS = /* @__PURE__ */ new Set([
|
|
114
|
+
"AccountSnapshotLocalState",
|
|
115
|
+
"BudgetStatementLocalState",
|
|
116
|
+
"ScopeFrameworkLocalState"
|
|
117
|
+
]);
|
|
118
|
+
/** Parse SDL; blank or comment-only input yields an empty document. */
|
|
119
|
+
function parseSdl(sdl) {
|
|
120
|
+
const source = new Source(sdl);
|
|
121
|
+
if (new Lexer(source).advance().kind === TokenKind.EOF) return EMPTY_DOCUMENT;
|
|
122
|
+
try {
|
|
123
|
+
return parse(source, { noLocation: true });
|
|
124
|
+
} catch (error) {
|
|
125
|
+
const withoutSemicolons = sdl.replaceAll(";", "");
|
|
126
|
+
if (withoutSemicolons === sdl) throw error;
|
|
127
|
+
return parse(withoutSemicolons, { noLocation: true });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function definedTypeNames(doc, kinds) {
|
|
131
|
+
return doc.definitions.filter((def) => kinds.has(def.kind)).map((def) => def.name.value);
|
|
132
|
+
}
|
|
133
|
+
/** Prefix type definition names and type references found in `names`. */
|
|
134
|
+
function prefixTypeNames(doc, prefix, names) {
|
|
135
|
+
return visit(doc, { enter(node) {
|
|
136
|
+
if (!TYPE_NAME_KINDS.has(node.kind)) return void 0;
|
|
137
|
+
const { name } = node;
|
|
138
|
+
if (!names.has(name.value)) return void 0;
|
|
139
|
+
return {
|
|
140
|
+
...node,
|
|
141
|
+
name: {
|
|
142
|
+
...name,
|
|
143
|
+
value: `${prefix}_${name.value}`
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
} });
|
|
147
|
+
}
|
|
148
|
+
/** State schema definitions without inputs, `DateTime` or the given object types. */
|
|
149
|
+
function stateDefinitions(doc, droppedObjectTypes = /* @__PURE__ */ new Set()) {
|
|
150
|
+
return doc.definitions.filter((def) => def.kind !== Kind.INPUT_OBJECT_TYPE_DEFINITION && def.kind !== Kind.INPUT_OBJECT_TYPE_EXTENSION && !(def.kind === Kind.SCALAR_TYPE_DEFINITION && def.name.value === "DateTime") && !(def.kind === Kind.OBJECT_TYPE_DEFINITION && droppedObjectTypes.has(def.name.value)));
|
|
151
|
+
}
|
|
152
|
+
/** The state types and the document type a model adds to the generated schema. */
|
|
153
|
+
function generatedModelDefinitions(dmSchemaName, global, local) {
|
|
154
|
+
const stateDoc = {
|
|
155
|
+
kind: Kind.DOCUMENT,
|
|
156
|
+
definitions: [...stateDefinitions(global), ...stateDefinitions(local, LOCAL_STATE_STUBS)]
|
|
157
|
+
};
|
|
158
|
+
const prefixed = prefixTypeNames(stateDoc, dmSchemaName, new Set(definedTypeNames(stateDoc, STATE_PREFIX_KINDS)));
|
|
159
|
+
const documentType = parseSdl(`
|
|
160
|
+
type ${dmSchemaName} implements IDocument {
|
|
161
|
+
id: String!
|
|
162
|
+
name: String!
|
|
163
|
+
documentType: String!
|
|
164
|
+
operations(skip: Int, first: Int): [Operation!]!
|
|
165
|
+
revision: Int!
|
|
166
|
+
createdAtUtcIso: DateTime!
|
|
167
|
+
lastModifiedAtUtcIso: DateTime!
|
|
168
|
+
${dmSchemaName !== "DocumentModel" ? `initialState: ${dmSchemaName}_${dmSchemaName}State!` : ""}
|
|
169
|
+
${dmSchemaName !== "DocumentModel" ? `state: ${dmSchemaName}_${dmSchemaName}State!` : ""}
|
|
170
|
+
stateJSON: JSONObject
|
|
171
|
+
}`);
|
|
172
|
+
return [...prefixed.definitions, ...documentType.definitions];
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Print the input type definitions of a schema document.
|
|
176
|
+
* @param excludeTypeNames - Input type names to leave out
|
|
177
|
+
*/
|
|
178
|
+
function printInputTypeDefinitions(doc, excludeTypeNames = /* @__PURE__ */ new Set()) {
|
|
179
|
+
const definitions = doc.definitions.filter((def) => def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && !excludeTypeNames.has(def.name.value));
|
|
180
|
+
if (definitions.length === 0) return "";
|
|
181
|
+
return print({
|
|
182
|
+
kind: Kind.DOCUMENT,
|
|
183
|
+
definitions
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Apply type prefixes to GraphQL schema to namespace types and avoid collisions.
|
|
188
|
+
* Inlined from @powerhousedao/common/utils to avoid ES module import issues.
|
|
189
|
+
* @param {string} schema - GraphQL schema string
|
|
190
|
+
* @param {string} prefix - Prefix to apply to type names
|
|
191
|
+
* @param {string[]} externalTypeNames - Type names from other schemas to also prefix
|
|
192
|
+
* @returns {string} Schema with prefixed types
|
|
193
|
+
*/
|
|
194
|
+
function applyGraphQLTypePrefixes(schema, prefix, externalTypeNames = []) {
|
|
195
|
+
if (!schema || !schema.trim()) return schema;
|
|
196
|
+
const doc = parseSdl(schema);
|
|
197
|
+
const names = /* @__PURE__ */ new Set([...definedTypeNames(doc, ALL_PREFIX_KINDS), ...externalTypeNames]);
|
|
198
|
+
if (names.size === 0) return schema;
|
|
199
|
+
return print(prefixTypeNames(doc, prefix, names));
|
|
200
|
+
}
|
|
201
|
+
function printModuleOperations(operations, documentName, names) {
|
|
202
|
+
const definitions = operations.flatMap((doc) => {
|
|
203
|
+
return prefixTypeNames(doc, documentName, /* @__PURE__ */ new Set([...names, ...definedTypeNames(doc, ALL_PREFIX_KINDS)])).definitions;
|
|
204
|
+
});
|
|
205
|
+
return definitions.length === 0 ? null : print({
|
|
206
|
+
kind: Kind.DOCUMENT,
|
|
207
|
+
definitions
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
function globalStateTypeName(documentName) {
|
|
211
|
+
return documentName === "DocumentModel" ? `${documentName}_${documentName}GlobalState` : `${documentName}_${documentName}State`;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Extract the root state type name from a scope's GraphQL schema.
|
|
215
|
+
* Uses the naming convention: {DocumentName}State for global,
|
|
216
|
+
* {DocumentName}{PascalScope}State for other scopes (e.g., DocumentDriveLocalState).
|
|
217
|
+
* Falls back to the last ObjectTypeDefinition (root types are conventionally last).
|
|
218
|
+
*/
|
|
219
|
+
function rootTypeName(ast, documentName, scopeName) {
|
|
220
|
+
const candidates = [`${documentName}${scopeName === "global" ? "" : pascalCase(scopeName)}State`, `${documentName}${pascalCase(scopeName)}State`];
|
|
221
|
+
let lastObjectType = null;
|
|
222
|
+
for (const def of ast.definitions) if (def.kind === Kind.OBJECT_TYPE_DEFINITION) {
|
|
223
|
+
if (candidates.includes(def.name.value)) return def.name.value;
|
|
224
|
+
lastObjectType = def.name.value;
|
|
225
|
+
}
|
|
226
|
+
return lastObjectType;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Convert a GraphQL output type AST node to an optional input type string.
|
|
230
|
+
* Strips non-null markers and converts object type references to Input suffix.
|
|
231
|
+
*/
|
|
232
|
+
function convertTypeNodeToInput(typeNode, objectNames, unionNames, interfaceNames) {
|
|
233
|
+
if (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type) return convertTypeNodeToInput(typeNode.type, objectNames, unionNames, interfaceNames);
|
|
234
|
+
if (typeNode.kind === Kind.LIST_TYPE && typeNode.type) return `[${convertTypeNodeToInput(typeNode.type, objectNames, unionNames, interfaceNames)}]`;
|
|
235
|
+
if (typeNode.kind === Kind.NAMED_TYPE && typeNode.name) {
|
|
236
|
+
const name = typeNode.name.value;
|
|
237
|
+
if (objectNames.has(name)) return `${name}Input`;
|
|
238
|
+
if (unionNames.has(name) || interfaceNames.has(name)) return "JSONObject";
|
|
239
|
+
return name;
|
|
240
|
+
}
|
|
241
|
+
return "JSONObject";
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Generate GraphQL input type definitions from a state schema document.
|
|
245
|
+
* Converts output type definitions to input types with all fields optional.
|
|
246
|
+
* - Object type references get Input suffix
|
|
247
|
+
* - Union/interface references become JSONObject
|
|
248
|
+
* - Enums and scalars remain unchanged
|
|
249
|
+
*/
|
|
250
|
+
function stateInputTypes(ast, excludeTypeNames) {
|
|
251
|
+
const enumNames = /* @__PURE__ */ new Set();
|
|
252
|
+
const unionNames = /* @__PURE__ */ new Set();
|
|
253
|
+
const interfaceNames = /* @__PURE__ */ new Set();
|
|
254
|
+
const objectNames = /* @__PURE__ */ new Set();
|
|
255
|
+
const existingInputNames = /* @__PURE__ */ new Set();
|
|
256
|
+
for (const def of ast.definitions) switch (def.kind) {
|
|
257
|
+
case Kind.ENUM_TYPE_DEFINITION:
|
|
258
|
+
enumNames.add(def.name.value);
|
|
259
|
+
break;
|
|
260
|
+
case Kind.UNION_TYPE_DEFINITION:
|
|
261
|
+
unionNames.add(def.name.value);
|
|
262
|
+
break;
|
|
263
|
+
case Kind.INTERFACE_TYPE_DEFINITION:
|
|
264
|
+
interfaceNames.add(def.name.value);
|
|
265
|
+
break;
|
|
266
|
+
case Kind.OBJECT_TYPE_DEFINITION:
|
|
267
|
+
objectNames.add(def.name.value);
|
|
268
|
+
break;
|
|
269
|
+
case Kind.INPUT_OBJECT_TYPE_DEFINITION: existingInputNames.add(def.name.value);
|
|
270
|
+
}
|
|
271
|
+
const inputTypes = [];
|
|
272
|
+
for (const def of ast.definitions) {
|
|
273
|
+
if (def.kind !== Kind.OBJECT_TYPE_DEFINITION) continue;
|
|
274
|
+
const inputName = `${def.name.value}Input`;
|
|
275
|
+
if (existingInputNames.has(inputName)) continue;
|
|
276
|
+
if (excludeTypeNames?.has(inputName)) continue;
|
|
277
|
+
const fields = def.fields ?? [];
|
|
278
|
+
if (fields.length === 0) continue;
|
|
279
|
+
const inputFields = fields.map((field) => {
|
|
280
|
+
return ` ${field.name.value}: ${convertTypeNodeToInput(field.type, objectNames, unionNames, interfaceNames)}`;
|
|
281
|
+
});
|
|
282
|
+
inputTypes.push(`input ${def.name.value}Input {\n${inputFields.join("\n")}\n}`);
|
|
283
|
+
}
|
|
284
|
+
return inputTypes.join("\n\n");
|
|
285
|
+
}
|
|
286
|
+
function scopeInitialState(ast, documentName, scopeName, allTypeNames) {
|
|
287
|
+
const rootName = rootTypeName(ast, documentName, scopeName);
|
|
288
|
+
if (!rootName) return { type: "JSONObject" };
|
|
289
|
+
const scopeInputTypes = stateInputTypes(ast, new Set(allTypeNames));
|
|
290
|
+
if (!scopeInputTypes) return { type: "JSONObject" };
|
|
291
|
+
return {
|
|
292
|
+
type: `${documentName}_${rootName}Input`,
|
|
293
|
+
inputTypes: applyGraphQLTypePrefixes(scopeInputTypes, documentName, allTypeNames)
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
//#endregion
|
|
86
297
|
//#region src/graphql/model-schema-templates.ts
|
|
87
298
|
/**
|
|
88
299
|
* Revision type - matches the definition in reactor/schema.graphql.
|
|
@@ -320,6 +531,281 @@ function generateNewApiSchema(projection) {
|
|
|
320
531
|
${printModuleSchemas(projection.modules)}`;
|
|
321
532
|
}
|
|
322
533
|
//#endregion
|
|
534
|
+
//#region src/graphql/document-model-schema/model-projection.ts
|
|
535
|
+
function buildModelProjection(documentName, documents) {
|
|
536
|
+
const allTypeNames = [.../* @__PURE__ */ new Set([...documents.stateTypeNames, ...documents.operationTypeNames])];
|
|
537
|
+
const names = new Set(allTypeNames);
|
|
538
|
+
const modules = documents.modules.flatMap(({ name, operations }) => {
|
|
539
|
+
const sdl = printModuleOperations(operations, documentName, names);
|
|
540
|
+
return sdl === null ? [] : [{
|
|
541
|
+
name,
|
|
542
|
+
sdl
|
|
543
|
+
}];
|
|
544
|
+
});
|
|
545
|
+
const scopes = documents.scopes.map(({ name, document }) => document === null ? {
|
|
546
|
+
name,
|
|
547
|
+
type: "JSONObject"
|
|
548
|
+
} : {
|
|
549
|
+
name,
|
|
550
|
+
...scopeInitialState(document, documentName, name, allTypeNames)
|
|
551
|
+
});
|
|
552
|
+
return {
|
|
553
|
+
documentName,
|
|
554
|
+
operations: documents.operations,
|
|
555
|
+
modules,
|
|
556
|
+
stateInputTypes: applyGraphQLTypePrefixes(printInputTypeDefinitions(documents.global, new Set(documents.operationTypeNames)), documentName, allTypeNames),
|
|
557
|
+
globalStateTypeName: globalStateTypeName(documentName),
|
|
558
|
+
localStateTypeName: documents.hasLocalRoot ? `${documentName}_${documentName}LocalState` : null,
|
|
559
|
+
initialState: {
|
|
560
|
+
inputTypes: scopes.flatMap(({ inputTypes }) => inputTypes ?? []).join("\n\n"),
|
|
561
|
+
scopes: scopes.map(({ name, type }) => ({
|
|
562
|
+
name,
|
|
563
|
+
type
|
|
564
|
+
}))
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region src/graphql/document-model-schema/stored-projection.ts
|
|
570
|
+
const logger$2 = childLogger(["reactor-api", "create-schema"]);
|
|
571
|
+
/**
|
|
572
|
+
* Reads a model's schema out of its latest stored specification's SDL, on
|
|
573
|
+
* every call. Malformed SDL fails, or falls back, where it always has.
|
|
574
|
+
*/
|
|
575
|
+
function storedSchemaSource(global) {
|
|
576
|
+
return {
|
|
577
|
+
packageScalars: [],
|
|
578
|
+
hasOperationSchemas: () => {
|
|
579
|
+
const specification = global.specifications.at(-1);
|
|
580
|
+
if (!specification) return false;
|
|
581
|
+
return specification.modules.some((module) => module.operations.some((op) => hasValidSchema(op.schema)));
|
|
582
|
+
},
|
|
583
|
+
modelDefinitions: () => {
|
|
584
|
+
const dmSchemaName = getDocumentModelSchemaName(global);
|
|
585
|
+
const latestSpec = global.specifications.at(-1);
|
|
586
|
+
const globalSchema = latestSpec?.state.global.schema ?? "";
|
|
587
|
+
const localSchema = latestSpec?.state.local.schema ?? "";
|
|
588
|
+
return generatedModelDefinitions(dmSchemaName, parseSdl(globalSchema), parseSdl(localSchema));
|
|
589
|
+
},
|
|
590
|
+
modelProjection: () => {
|
|
591
|
+
const documentName = getDocumentModelSchemaName(global);
|
|
592
|
+
return buildModelProjection(documentName, storedDocuments(global.specifications.at(-1), documentName));
|
|
593
|
+
},
|
|
594
|
+
stateDefinitions: () => {
|
|
595
|
+
const specification = global.specifications.at(-1);
|
|
596
|
+
if (!specification) return [];
|
|
597
|
+
const fullSchema = `${specification.state.global.schema ?? ""}\n${specification.state.local.schema ?? ""}`;
|
|
598
|
+
if (!fullSchema.trim()) return [];
|
|
599
|
+
try {
|
|
600
|
+
return parse(fullSchema).definitions;
|
|
601
|
+
} catch {
|
|
602
|
+
return [];
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
mutationOperations: () => global.specifications.at(-1)?.modules.flatMap((module) => module.operations.filter((op) => op.name)).map((op) => ({
|
|
606
|
+
camelName: camelCase(op.name),
|
|
607
|
+
storedName: op.name,
|
|
608
|
+
creatorKey: camelCase(op.name)
|
|
609
|
+
})) ?? []
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Type names a schema defines. Unparseable schemas yield no names.
|
|
614
|
+
*/
|
|
615
|
+
function extractTypeNames(schema) {
|
|
616
|
+
try {
|
|
617
|
+
return definedTypeNames(parseSdl(schema), ALL_PREFIX_KINDS);
|
|
618
|
+
} catch (error) {
|
|
619
|
+
logger$2.debug(`Skipping unparseable schema: ${String(error)}`);
|
|
620
|
+
return [];
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* The template documents of a stored specification. Type names skip
|
|
625
|
+
* unparseable schemas; the global state and served operation schemas must
|
|
626
|
+
* parse; a scope schema that does not parse strictly is served as `JSONObject`.
|
|
627
|
+
*/
|
|
628
|
+
function storedDocuments(specification, documentName) {
|
|
629
|
+
const globalStateSchema = specification?.state.global.schema;
|
|
630
|
+
const localStateSchema = specification?.state.local.schema;
|
|
631
|
+
const stateTypeNames = [...extractTypeNames(globalStateSchema ?? ""), ...extractTypeNames(localStateSchema ?? "")];
|
|
632
|
+
const operationTypeNames = specification?.modules.flatMap((module) => module.operations.flatMap((op) => extractTypeNames(op.schema ?? ""))) ?? [];
|
|
633
|
+
const global = parseSdl(globalStateSchema ?? "");
|
|
634
|
+
const operations = specification?.modules.flatMap((module) => module.operations.filter((op) => op.name && hasValidSchema(op.schema)).map((op) => ({
|
|
635
|
+
camelName: camelCase(op.name),
|
|
636
|
+
inputTypeName: `${documentName}_${pascalCase(op.name)}Input`
|
|
637
|
+
}))) ?? [];
|
|
638
|
+
return {
|
|
639
|
+
global,
|
|
640
|
+
stateTypeNames,
|
|
641
|
+
operationTypeNames,
|
|
642
|
+
modules: specification?.modules.map((module) => ({
|
|
643
|
+
name: module.name,
|
|
644
|
+
operations: module.operations.filter((op) => hasValidSchema(op.schema)).map((op) => parseSdl(op.schema ?? ""))
|
|
645
|
+
})) ?? [],
|
|
646
|
+
operations,
|
|
647
|
+
hasLocalRoot: (localStateSchema ?? "").includes(`type ${documentName}LocalState`),
|
|
648
|
+
scopes: storedScopes(specification)
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
function storedScopes(specification) {
|
|
652
|
+
if (!specification) return [];
|
|
653
|
+
return Object.entries(specification.state).map(([name, scopeState]) => {
|
|
654
|
+
const schema = scopeState.schema ?? "";
|
|
655
|
+
if (!hasValidSchema(schema)) return {
|
|
656
|
+
name,
|
|
657
|
+
document: null
|
|
658
|
+
};
|
|
659
|
+
try {
|
|
660
|
+
return {
|
|
661
|
+
name,
|
|
662
|
+
document: parse(schema)
|
|
663
|
+
};
|
|
664
|
+
} catch {
|
|
665
|
+
return {
|
|
666
|
+
name,
|
|
667
|
+
document: null
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
//#endregion
|
|
673
|
+
//#region src/graphql/document-model-schema/structured-projection.ts
|
|
674
|
+
const documentOf = (segment) => segment === null ? EMPTY_DOCUMENT : segment.document;
|
|
675
|
+
const inputOf = (operation) => operation.input;
|
|
676
|
+
function once(compute) {
|
|
677
|
+
let cached;
|
|
678
|
+
return () => (cached ??= { value: compute() }).value;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Adapts a definition's GraphQL projection. Each value is derived on first use
|
|
682
|
+
* and kept, since the projection is an immutable snapshot.
|
|
683
|
+
*/
|
|
684
|
+
function structuredSchemaSource(projection, global) {
|
|
685
|
+
const name = getDocumentModelSchemaName(global);
|
|
686
|
+
const documentName = () => name;
|
|
687
|
+
const globalDocument = documentOf(projection.global);
|
|
688
|
+
const localDocument = documentOf(projection.local);
|
|
689
|
+
const mutationOperations = once(() => projection.modules.flatMap((module) => module.operations.flatMap((operation) => {
|
|
690
|
+
const input = inputOf(operation)?.definitions[0];
|
|
691
|
+
if (input?.kind !== Kind.INPUT_OBJECT_TYPE_DEFINITION) return [];
|
|
692
|
+
return [{
|
|
693
|
+
camelName: camelCase(operation.name),
|
|
694
|
+
inputTypeName: `${documentName()}_${input.name.value}`,
|
|
695
|
+
storedName: operation.name,
|
|
696
|
+
creatorKey: operation.creatorKey
|
|
697
|
+
}];
|
|
698
|
+
})));
|
|
699
|
+
return {
|
|
700
|
+
packageScalars: projection.packageScalars,
|
|
701
|
+
mutationOperations,
|
|
702
|
+
hasOperationSchemas: once(() => projection.modules.some((module) => module.operations.some((op) => op.input !== null))),
|
|
703
|
+
modelDefinitions: once(() => generatedModelDefinitions(documentName(), globalDocument, localDocument)),
|
|
704
|
+
stateDefinitions: once(() => [...globalDocument.definitions, ...localDocument.definitions]),
|
|
705
|
+
modelProjection: once(() => {
|
|
706
|
+
const name = documentName();
|
|
707
|
+
const operationInputs = projection.modules.map((module) => module.operations.flatMap((operation) => inputOf(operation) ?? []));
|
|
708
|
+
const localRoot = `${name}LocalState`;
|
|
709
|
+
return buildModelProjection(name, {
|
|
710
|
+
global: globalDocument,
|
|
711
|
+
stateTypeNames: [...definedTypeNames(globalDocument, ALL_PREFIX_KINDS), ...definedTypeNames(localDocument, ALL_PREFIX_KINDS)],
|
|
712
|
+
operationTypeNames: operationInputs.flat().flatMap((input) => definedTypeNames(input, ALL_PREFIX_KINDS)),
|
|
713
|
+
modules: projection.modules.map((module, index) => ({
|
|
714
|
+
name: module.name,
|
|
715
|
+
operations: operationInputs[index] ?? []
|
|
716
|
+
})),
|
|
717
|
+
operations: mutationOperations(),
|
|
718
|
+
hasLocalRoot: localDocument.definitions.some((def) => def.kind === Kind.OBJECT_TYPE_DEFINITION && def.name.value === localRoot),
|
|
719
|
+
scopes: [{
|
|
720
|
+
name: "global",
|
|
721
|
+
document: globalDocument
|
|
722
|
+
}, {
|
|
723
|
+
name: "local",
|
|
724
|
+
document: projection.local === null ? null : localDocument
|
|
725
|
+
}]
|
|
726
|
+
});
|
|
727
|
+
})
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
//#endregion
|
|
731
|
+
//#region src/graphql/document-model-schema/source.ts
|
|
732
|
+
const logger$1 = childLogger(["reactor-api", "document-model-schema"]);
|
|
733
|
+
const logged = /* @__PURE__ */ new Set();
|
|
734
|
+
function warnOnce(documentType, reason, detail) {
|
|
735
|
+
const key = `${documentType}\n${reason}`;
|
|
736
|
+
if (logged.has(key)) return;
|
|
737
|
+
logged.add(key);
|
|
738
|
+
logger$1.warn("Serving document model @documentType from its stored model: its definition is @reason: @detail", documentType, reason, detail);
|
|
739
|
+
}
|
|
740
|
+
const projected = /* @__PURE__ */ new WeakMap();
|
|
741
|
+
function project(inspection) {
|
|
742
|
+
let entry = projected.get(inspection.specification);
|
|
743
|
+
if (entry === void 0) {
|
|
744
|
+
try {
|
|
745
|
+
entry = {
|
|
746
|
+
projection: projectDocumentModelGraphQL(inspection.definition, inspection.specification),
|
|
747
|
+
sources: /* @__PURE__ */ new Map()
|
|
748
|
+
};
|
|
749
|
+
} catch (error) {
|
|
750
|
+
entry = { error };
|
|
751
|
+
}
|
|
752
|
+
projected.set(inspection.specification, entry);
|
|
753
|
+
}
|
|
754
|
+
return entry;
|
|
755
|
+
}
|
|
756
|
+
const snapshotDigests = /* @__PURE__ */ new WeakMap();
|
|
757
|
+
/**
|
|
758
|
+
* The cache-key part a model's definition adds: a digest of the inspected
|
|
759
|
+
* snapshot when the schema is built from it, else `null`, so a model served
|
|
760
|
+
* from its stored SDL keeps main's key. Inspection is memoized per definition
|
|
761
|
+
* object, so this runs once per loaded definition.
|
|
762
|
+
*/
|
|
763
|
+
function structuredDefinitionDigest(model) {
|
|
764
|
+
let inspection;
|
|
765
|
+
try {
|
|
766
|
+
inspection = inspectDocumentModelDefinition(model);
|
|
767
|
+
} catch {
|
|
768
|
+
return null;
|
|
769
|
+
}
|
|
770
|
+
if (inspection.kind !== "structured") return null;
|
|
771
|
+
let digest = snapshotDigests.get(inspection.definition);
|
|
772
|
+
if (digest === void 0) {
|
|
773
|
+
digest = createHash("sha256").update(JSON.stringify(inspection.definition)).digest("base64");
|
|
774
|
+
snapshotDigests.set(inspection.definition, digest);
|
|
775
|
+
}
|
|
776
|
+
return digest;
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* The one place a host selects how a document model's schema is built: from
|
|
780
|
+
* its structured definition when it carries a valid, matching one, else from
|
|
781
|
+
* its stored SDL.
|
|
782
|
+
*/
|
|
783
|
+
function documentModelSchemaSource(model) {
|
|
784
|
+
const { global } = model.documentModel;
|
|
785
|
+
let inspection;
|
|
786
|
+
try {
|
|
787
|
+
inspection = inspectDocumentModelDefinition(model);
|
|
788
|
+
} catch (error) {
|
|
789
|
+
warnOnce(global.id, "unreadable", error);
|
|
790
|
+
return storedSchemaSource(global);
|
|
791
|
+
}
|
|
792
|
+
if (inspection.kind === "stored") {
|
|
793
|
+
if (inspection.reason === "invalid" || inspection.reason === "mismatch") warnOnce(global.id, inspection.reason, inspection.diagnostics);
|
|
794
|
+
return storedSchemaSource(global);
|
|
795
|
+
}
|
|
796
|
+
const entry = project(inspection);
|
|
797
|
+
if ("error" in entry) {
|
|
798
|
+
warnOnce(global.id, "unreadable", entry.error);
|
|
799
|
+
return storedSchemaSource(global);
|
|
800
|
+
}
|
|
801
|
+
let source = entry.sources.get(global.name);
|
|
802
|
+
if (source === void 0) {
|
|
803
|
+
source = structuredSchemaSource(entry.projection, global);
|
|
804
|
+
entry.sources.set(global.name, source);
|
|
805
|
+
}
|
|
806
|
+
return source;
|
|
807
|
+
}
|
|
808
|
+
//#endregion
|
|
323
809
|
//#region src/utils/create-schema.ts
|
|
324
810
|
const logger = childLogger(["reactor-api", "create-schema"]);
|
|
325
811
|
/**
|
|
@@ -373,71 +859,6 @@ function typeDefinitionName(def) {
|
|
|
373
859
|
if (!TYPE_DEFINITION_KINDS.has(def.kind)) return void 0;
|
|
374
860
|
return def.name?.value;
|
|
375
861
|
}
|
|
376
|
-
const EMPTY_DOCUMENT = {
|
|
377
|
-
kind: Kind.DOCUMENT,
|
|
378
|
-
definitions: []
|
|
379
|
-
};
|
|
380
|
-
const OUTPUT_TYPE_KINDS = [
|
|
381
|
-
Kind.OBJECT_TYPE_DEFINITION,
|
|
382
|
-
Kind.OBJECT_TYPE_EXTENSION,
|
|
383
|
-
Kind.ENUM_TYPE_DEFINITION,
|
|
384
|
-
Kind.ENUM_TYPE_EXTENSION,
|
|
385
|
-
Kind.UNION_TYPE_DEFINITION,
|
|
386
|
-
Kind.UNION_TYPE_EXTENSION,
|
|
387
|
-
Kind.INTERFACE_TYPE_DEFINITION,
|
|
388
|
-
Kind.INTERFACE_TYPE_EXTENSION
|
|
389
|
-
];
|
|
390
|
-
const STATE_PREFIX_KINDS = new Set(OUTPUT_TYPE_KINDS);
|
|
391
|
-
const ALL_PREFIX_KINDS = /* @__PURE__ */ new Set([
|
|
392
|
-
...OUTPUT_TYPE_KINDS,
|
|
393
|
-
Kind.INPUT_OBJECT_TYPE_DEFINITION,
|
|
394
|
-
Kind.INPUT_OBJECT_TYPE_EXTENSION
|
|
395
|
-
]);
|
|
396
|
-
const TYPE_NAME_KINDS = /* @__PURE__ */ new Set([
|
|
397
|
-
...ALL_PREFIX_KINDS,
|
|
398
|
-
Kind.SCALAR_TYPE_DEFINITION,
|
|
399
|
-
Kind.SCALAR_TYPE_EXTENSION,
|
|
400
|
-
Kind.NAMED_TYPE
|
|
401
|
-
]);
|
|
402
|
-
const LOCAL_STATE_STUBS = /* @__PURE__ */ new Set([
|
|
403
|
-
"AccountSnapshotLocalState",
|
|
404
|
-
"BudgetStatementLocalState",
|
|
405
|
-
"ScopeFrameworkLocalState"
|
|
406
|
-
]);
|
|
407
|
-
/** Parse SDL; blank or comment-only input yields an empty document. */
|
|
408
|
-
function parseSdl(sdl) {
|
|
409
|
-
const source = new Source(sdl);
|
|
410
|
-
if (new Lexer(source).advance().kind === TokenKind.EOF) return EMPTY_DOCUMENT;
|
|
411
|
-
try {
|
|
412
|
-
return parse(source, { noLocation: true });
|
|
413
|
-
} catch (error) {
|
|
414
|
-
const withoutSemicolons = sdl.replaceAll(";", "");
|
|
415
|
-
if (withoutSemicolons === sdl) throw error;
|
|
416
|
-
return parse(withoutSemicolons, { noLocation: true });
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
function definedTypeNames(doc, kinds) {
|
|
420
|
-
return doc.definitions.filter((def) => kinds.has(def.kind)).map((def) => def.name.value);
|
|
421
|
-
}
|
|
422
|
-
/** Prefix type definition names and type references found in `names`. */
|
|
423
|
-
function prefixTypeNames(doc, prefix, names) {
|
|
424
|
-
return visit(doc, { enter(node) {
|
|
425
|
-
if (!TYPE_NAME_KINDS.has(node.kind)) return void 0;
|
|
426
|
-
const { name } = node;
|
|
427
|
-
if (!names.has(name.value)) return void 0;
|
|
428
|
-
return {
|
|
429
|
-
...node,
|
|
430
|
-
name: {
|
|
431
|
-
...name,
|
|
432
|
-
value: `${prefix}_${name.value}`
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
} });
|
|
436
|
-
}
|
|
437
|
-
/** State schema definitions without inputs, `DateTime` or the given object types. */
|
|
438
|
-
function stateTypeDefinitions(sdl, droppedObjectTypes = /* @__PURE__ */ new Set()) {
|
|
439
|
-
return parseSdl(sdl).definitions.filter((def) => def.kind !== Kind.INPUT_OBJECT_TYPE_DEFINITION && def.kind !== Kind.INPUT_OBJECT_TYPE_EXTENSION && !(def.kind === Kind.SCALAR_TYPE_DEFINITION && def.name.value === "DateTime") && !(def.kind === Kind.OBJECT_TYPE_DEFINITION && droppedObjectTypes.has(def.name.value)));
|
|
440
|
-
}
|
|
441
862
|
const buildSubgraphSchemaModule = (documentModels, resolvers, typeDefs, options = {}) => {
|
|
442
863
|
const generated = options.generated ?? getGeneratedDocumentModelTypes(documentModels, void 0, options.scalars);
|
|
443
864
|
const newResolvers = {
|
|
@@ -479,30 +900,10 @@ function typeDefinitionNames(doc) {
|
|
|
479
900
|
}
|
|
480
901
|
/** The definitions `documentModel` adds to the generated schema. */
|
|
481
902
|
function documentModelDefinitions(documentModel) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
const definitions = [...stateTypeDefinitions(globalSchema), ...stateTypeDefinitions(localSchema, LOCAL_STATE_STUBS)];
|
|
487
|
-
const stateDoc = {
|
|
488
|
-
kind: Kind.DOCUMENT,
|
|
489
|
-
definitions
|
|
490
|
-
};
|
|
491
|
-
const prefixed = prefixTypeNames(stateDoc, dmSchemaName, new Set(definedTypeNames(stateDoc, STATE_PREFIX_KINDS)));
|
|
492
|
-
const documentType = parseSdl(`
|
|
493
|
-
type ${dmSchemaName} implements IDocument {
|
|
494
|
-
id: String!
|
|
495
|
-
name: String!
|
|
496
|
-
documentType: String!
|
|
497
|
-
operations(skip: Int, first: Int): [Operation!]!
|
|
498
|
-
revision: Int!
|
|
499
|
-
createdAtUtcIso: DateTime!
|
|
500
|
-
lastModifiedAtUtcIso: DateTime!
|
|
501
|
-
${dmSchemaName !== "DocumentModel" ? `initialState: ${dmSchemaName}_${dmSchemaName}State!` : ""}
|
|
502
|
-
${dmSchemaName !== "DocumentModel" ? `state: ${dmSchemaName}_${dmSchemaName}State!` : ""}
|
|
503
|
-
stateJSON: JSONObject
|
|
504
|
-
}`);
|
|
505
|
-
return [...prefixed.definitions, ...documentType.definitions];
|
|
903
|
+
return documentModelSchemaSource(documentModel).modelDefinitions();
|
|
904
|
+
}
|
|
905
|
+
function sourceOf(documentModel) {
|
|
906
|
+
return "documentModel" in documentModel ? documentModel : { documentModel: { global: documentModel } };
|
|
506
907
|
}
|
|
507
908
|
function addScalarFlags(definitions, into) {
|
|
508
909
|
for (const def of definitions) {
|
|
@@ -533,7 +934,7 @@ function reservedTypeNames(coreTypeDefs) {
|
|
|
533
934
|
* The reserved names `documentModel`'s generated types take. A scalar may
|
|
534
935
|
* repeat a reserved scalar: the duplicate is dropped on assembly.
|
|
535
936
|
*/
|
|
536
|
-
function reservedTypeNamesTaken(documentModel, reserved, definitions = documentModelDefinitions(documentModel
|
|
937
|
+
function reservedTypeNamesTaken(documentModel, reserved, definitions = documentModelDefinitions(documentModel)) {
|
|
537
938
|
const generated = addScalarFlags(definitions, /* @__PURE__ */ new Map());
|
|
538
939
|
return Array.from(generated).filter(([name, scalar]) => reserved.has(name) && !(scalar && reserved.get(name))).map(([name]) => name);
|
|
539
940
|
}
|
|
@@ -636,25 +1037,25 @@ const GENERATED_TAIL = gql`
|
|
|
636
1037
|
*/
|
|
637
1038
|
function getGeneratedDocumentModelTypes(documentModels, schemas, scalars = EMPTY_SCALAR_REGISTRY) {
|
|
638
1039
|
return generatedTypesFrom(documentModels.map((model) => ({
|
|
639
|
-
|
|
1040
|
+
model,
|
|
640
1041
|
generated: schemas?.get(model)?.generated
|
|
641
1042
|
})), scalars);
|
|
642
1043
|
}
|
|
643
1044
|
/** {@link getGeneratedDocumentModelTypes} from the models' global states. */
|
|
644
1045
|
function generatedTypesOf(documentModels) {
|
|
645
|
-
return generatedTypesFrom(documentModels.map((global) => ({ global })), EMPTY_SCALAR_REGISTRY);
|
|
1046
|
+
return generatedTypesFrom(documentModels.map((global) => ({ model: sourceOf(global) })), EMPTY_SCALAR_REGISTRY);
|
|
646
1047
|
}
|
|
647
1048
|
function generatedTypesFrom(documentModels, scalars) {
|
|
648
1049
|
const modelDefinitions = [];
|
|
649
1050
|
const addedDocumentModels = /* @__PURE__ */ new Set();
|
|
650
|
-
documentModels.forEach(({
|
|
651
|
-
const dmSchemaName = getDocumentModelSchemaName(global);
|
|
1051
|
+
documentModels.forEach(({ model, generated }) => {
|
|
1052
|
+
const dmSchemaName = getDocumentModelSchemaName(model.documentModel.global);
|
|
652
1053
|
if (addedDocumentModels.has(dmSchemaName)) {
|
|
653
1054
|
logger.debug(`Skipping document model with duplicate name: ${dmSchemaName}`);
|
|
654
1055
|
return;
|
|
655
1056
|
}
|
|
656
1057
|
addedDocumentModels.add(dmSchemaName);
|
|
657
|
-
modelDefinitions.push(...generated ?? documentModelDefinitions(
|
|
1058
|
+
modelDefinitions.push(...generated ?? documentModelDefinitions(model));
|
|
658
1059
|
});
|
|
659
1060
|
const definitions = dedupeTypeDefinitions([
|
|
660
1061
|
...generatedHead(scalars),
|
|
@@ -669,9 +1070,7 @@ function generatedTypesFrom(documentModels, scalars) {
|
|
|
669
1070
|
}
|
|
670
1071
|
/** Whether any operation of the latest specification has a schema. */
|
|
671
1072
|
function hasOperationSchemas(documentModel) {
|
|
672
|
-
|
|
673
|
-
if (!specification) return false;
|
|
674
|
-
return specification.modules.some((module) => module.operations.some((op) => hasValidSchema(op.schema)));
|
|
1073
|
+
return documentModelSchemaSource(sourceOf(documentModel)).hasOperationSchemas();
|
|
675
1074
|
}
|
|
676
1075
|
/** The types of a document model's own subgraph. */
|
|
677
1076
|
function documentModelSubgraphTypes(documentModel) {
|
|
@@ -688,13 +1087,13 @@ function checkBuilds(documentModel, generated, subgraph) {
|
|
|
688
1087
|
}
|
|
689
1088
|
/** Throws when the generated types every subgraph carries do not build. */
|
|
690
1089
|
function buildDocumentModelSchema(documentModel, options = {}) {
|
|
691
|
-
const generated = documentModelDefinitions(documentModel
|
|
692
|
-
if (options.subgraph === false || !hasOperationSchemas(documentModel
|
|
1090
|
+
const generated = documentModelDefinitions(documentModel);
|
|
1091
|
+
if (options.subgraph === false || !hasOperationSchemas(documentModel)) {
|
|
693
1092
|
checkBuilds(documentModel, generated);
|
|
694
1093
|
return { generated };
|
|
695
1094
|
}
|
|
696
1095
|
try {
|
|
697
|
-
const subgraph = documentModelSubgraphTypes(documentModel
|
|
1096
|
+
const subgraph = documentModelSubgraphTypes(documentModel);
|
|
698
1097
|
checkBuilds(documentModel, generated, subgraph);
|
|
699
1098
|
return {
|
|
700
1099
|
generated,
|
|
@@ -711,7 +1110,10 @@ function buildDocumentModelSchema(documentModel, options = {}) {
|
|
|
711
1110
|
/** Hash of everything {@link buildDocumentModelSchema} reads. */
|
|
712
1111
|
function schemaSourceKey(documentModel) {
|
|
713
1112
|
const { name, specifications } = documentModel.documentModel.global;
|
|
714
|
-
|
|
1113
|
+
const hash = createHash("sha256").update(JSON.stringify([name, specifications.at(-1)]));
|
|
1114
|
+
const definition = structuredDefinitionDigest(documentModel);
|
|
1115
|
+
if (definition !== null) hash.update(definition);
|
|
1116
|
+
return hash.digest("base64");
|
|
715
1117
|
}
|
|
716
1118
|
/** Schemas and failures by source hash, for the last resolve's models only. */
|
|
717
1119
|
var DocumentModelSchemaCache = class {
|
|
@@ -779,129 +1181,6 @@ function assembleSubgraphTypeDefs(generated, typeDefs, ownTypeNames) {
|
|
|
779
1181
|
};
|
|
780
1182
|
}
|
|
781
1183
|
/**
|
|
782
|
-
* Extract the names of types defined in a GraphQL schema.
|
|
783
|
-
* Unparseable schemas yield no names.
|
|
784
|
-
*/
|
|
785
|
-
function extractTypeNames(schema) {
|
|
786
|
-
try {
|
|
787
|
-
return definedTypeNames(parseSdl(schema), ALL_PREFIX_KINDS);
|
|
788
|
-
} catch (error) {
|
|
789
|
-
logger.debug(`Skipping unparseable schema: ${String(error)}`);
|
|
790
|
-
return [];
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
/**
|
|
794
|
-
* Print the input type definitions of a GraphQL schema.
|
|
795
|
-
* @param excludeTypeNames - Input type names to leave out
|
|
796
|
-
*/
|
|
797
|
-
function extractInputTypeDefinitions(schema, excludeTypeNames = /* @__PURE__ */ new Set()) {
|
|
798
|
-
const definitions = parseSdl(schema).definitions.filter((def) => def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && !excludeTypeNames.has(def.name.value));
|
|
799
|
-
if (definitions.length === 0) return "";
|
|
800
|
-
return print({
|
|
801
|
-
kind: Kind.DOCUMENT,
|
|
802
|
-
definitions
|
|
803
|
-
});
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
* Extract the root state type name from a scope's GraphQL schema.
|
|
807
|
-
* Uses the naming convention: {DocumentName}State for global,
|
|
808
|
-
* {DocumentName}{PascalScope}State for other scopes (e.g., DocumentDriveLocalState).
|
|
809
|
-
* Falls back to the last ObjectTypeDefinition (root types are conventionally last).
|
|
810
|
-
*/
|
|
811
|
-
function extractRootTypeName(schema, documentName, scopeName) {
|
|
812
|
-
try {
|
|
813
|
-
const ast = parse(schema);
|
|
814
|
-
const candidates = [`${documentName}${scopeName === "global" ? "" : pascalCase(scopeName)}State`, `${documentName}${pascalCase(scopeName)}State`];
|
|
815
|
-
let lastObjectType = null;
|
|
816
|
-
for (const def of ast.definitions) if (def.kind === Kind.OBJECT_TYPE_DEFINITION) {
|
|
817
|
-
if (candidates.includes(def.name.value)) return def.name.value;
|
|
818
|
-
lastObjectType = def.name.value;
|
|
819
|
-
}
|
|
820
|
-
return lastObjectType;
|
|
821
|
-
} catch {}
|
|
822
|
-
return null;
|
|
823
|
-
}
|
|
824
|
-
/**
|
|
825
|
-
* Convert a GraphQL output type AST node to an optional input type string.
|
|
826
|
-
* Strips non-null markers and converts object type references to Input suffix.
|
|
827
|
-
*/
|
|
828
|
-
function convertTypeNodeToInput(typeNode, objectNames, unionNames, interfaceNames) {
|
|
829
|
-
if (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type) return convertTypeNodeToInput(typeNode.type, objectNames, unionNames, interfaceNames);
|
|
830
|
-
if (typeNode.kind === Kind.LIST_TYPE && typeNode.type) return `[${convertTypeNodeToInput(typeNode.type, objectNames, unionNames, interfaceNames)}]`;
|
|
831
|
-
if (typeNode.kind === Kind.NAMED_TYPE && typeNode.name) {
|
|
832
|
-
const name = typeNode.name.value;
|
|
833
|
-
if (objectNames.has(name)) return `${name}Input`;
|
|
834
|
-
if (unionNames.has(name) || interfaceNames.has(name)) return "JSONObject";
|
|
835
|
-
return name;
|
|
836
|
-
}
|
|
837
|
-
return "JSONObject";
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* Generate GraphQL input type definitions from a state schema string.
|
|
841
|
-
* Converts output type definitions to input types with all fields optional.
|
|
842
|
-
* - Object type references get Input suffix
|
|
843
|
-
* - Union/interface references become JSONObject
|
|
844
|
-
* - Enums and scalars remain unchanged
|
|
845
|
-
*/
|
|
846
|
-
function generateStateInputTypes(stateSchema, excludeTypeNames) {
|
|
847
|
-
if (!stateSchema || !stateSchema.trim()) return "";
|
|
848
|
-
let ast;
|
|
849
|
-
try {
|
|
850
|
-
ast = parse(stateSchema);
|
|
851
|
-
} catch {
|
|
852
|
-
return "";
|
|
853
|
-
}
|
|
854
|
-
const enumNames = /* @__PURE__ */ new Set();
|
|
855
|
-
const unionNames = /* @__PURE__ */ new Set();
|
|
856
|
-
const interfaceNames = /* @__PURE__ */ new Set();
|
|
857
|
-
const objectNames = /* @__PURE__ */ new Set();
|
|
858
|
-
const existingInputNames = /* @__PURE__ */ new Set();
|
|
859
|
-
for (const def of ast.definitions) switch (def.kind) {
|
|
860
|
-
case Kind.ENUM_TYPE_DEFINITION:
|
|
861
|
-
enumNames.add(def.name.value);
|
|
862
|
-
break;
|
|
863
|
-
case Kind.UNION_TYPE_DEFINITION:
|
|
864
|
-
unionNames.add(def.name.value);
|
|
865
|
-
break;
|
|
866
|
-
case Kind.INTERFACE_TYPE_DEFINITION:
|
|
867
|
-
interfaceNames.add(def.name.value);
|
|
868
|
-
break;
|
|
869
|
-
case Kind.OBJECT_TYPE_DEFINITION:
|
|
870
|
-
objectNames.add(def.name.value);
|
|
871
|
-
break;
|
|
872
|
-
case Kind.INPUT_OBJECT_TYPE_DEFINITION: existingInputNames.add(def.name.value);
|
|
873
|
-
}
|
|
874
|
-
const inputTypes = [];
|
|
875
|
-
for (const def of ast.definitions) {
|
|
876
|
-
if (def.kind !== Kind.OBJECT_TYPE_DEFINITION) continue;
|
|
877
|
-
const inputName = `${def.name.value}Input`;
|
|
878
|
-
if (existingInputNames.has(inputName)) continue;
|
|
879
|
-
if (excludeTypeNames?.has(inputName)) continue;
|
|
880
|
-
const fields = def.fields ?? [];
|
|
881
|
-
if (fields.length === 0) continue;
|
|
882
|
-
const inputFields = fields.map((field) => {
|
|
883
|
-
return ` ${field.name.value}: ${convertTypeNodeToInput(field.type, objectNames, unionNames, interfaceNames)}`;
|
|
884
|
-
});
|
|
885
|
-
inputTypes.push(`input ${def.name.value}Input {\n${inputFields.join("\n")}\n}`);
|
|
886
|
-
}
|
|
887
|
-
return inputTypes.join("\n\n");
|
|
888
|
-
}
|
|
889
|
-
/**
|
|
890
|
-
* Apply type prefixes to GraphQL schema to namespace types and avoid collisions.
|
|
891
|
-
* Inlined from @powerhousedao/common/utils to avoid ES module import issues.
|
|
892
|
-
* @param {string} schema - GraphQL schema string
|
|
893
|
-
* @param {string} prefix - Prefix to apply to type names
|
|
894
|
-
* @param {string[]} externalTypeNames - Type names from other schemas to also prefix
|
|
895
|
-
* @returns {string} Schema with prefixed types
|
|
896
|
-
*/
|
|
897
|
-
function applyGraphQLTypePrefixes(schema, prefix, externalTypeNames = []) {
|
|
898
|
-
if (!schema || !schema.trim()) return schema;
|
|
899
|
-
const doc = parseSdl(schema);
|
|
900
|
-
const names = /* @__PURE__ */ new Set([...definedTypeNames(doc, ALL_PREFIX_KINDS), ...externalTypeNames]);
|
|
901
|
-
if (names.size === 0) return schema;
|
|
902
|
-
return print(prefixTypeNames(doc, prefix, names));
|
|
903
|
-
}
|
|
904
|
-
/**
|
|
905
1184
|
* Generate a GraphQL schema for a document model.
|
|
906
1185
|
*
|
|
907
1186
|
* @param documentModel - The document model global state
|
|
@@ -909,87 +1188,7 @@ function applyGraphQLTypePrefixes(schema, prefix, externalTypeNames = []) {
|
|
|
909
1188
|
* @returns GraphQL DocumentNode
|
|
910
1189
|
*/
|
|
911
1190
|
function generateDocumentModelSchema(documentModel, options = {}) {
|
|
912
|
-
return generateModelSchema(
|
|
913
|
-
}
|
|
914
|
-
/** Reads the template inputs out of a model's stored SDL. */
|
|
915
|
-
function storedModelProjection(documentModel, documentName) {
|
|
916
|
-
const specification = documentModel.specifications.at(-1);
|
|
917
|
-
const globalStateSchema = specification?.state.global.schema;
|
|
918
|
-
const localStateSchema = specification?.state.local.schema;
|
|
919
|
-
const stateTypeNames = [...extractTypeNames(globalStateSchema ?? ""), ...extractTypeNames(localStateSchema ?? "")];
|
|
920
|
-
const allOperationTypeNames = specification?.modules.flatMap((module) => module.operations.flatMap((op) => extractTypeNames(op.schema ?? ""))) ?? [];
|
|
921
|
-
const allTypeNames = [.../* @__PURE__ */ new Set([...stateTypeNames, ...allOperationTypeNames])];
|
|
922
|
-
const prefixedStateInputTypes = applyGraphQLTypePrefixes(extractInputTypeDefinitions(globalStateSchema ?? "", new Set(allOperationTypeNames)), documentName, allTypeNames);
|
|
923
|
-
const operations = specification?.modules.flatMap((module) => module.operations.filter((op) => op.name && hasValidSchema(op.schema)).map((op) => ({
|
|
924
|
-
camelName: camelCase(op.name),
|
|
925
|
-
inputTypeName: `${documentName}_${pascalCase(op.name)}Input`
|
|
926
|
-
}))) ?? [];
|
|
927
|
-
const names = new Set(allTypeNames);
|
|
928
|
-
return {
|
|
929
|
-
documentName,
|
|
930
|
-
operations,
|
|
931
|
-
modules: specification?.modules.flatMap((module) => {
|
|
932
|
-
const definitions = module.operations.filter((op) => hasValidSchema(op.schema)).flatMap((op) => {
|
|
933
|
-
const doc = parseSdl(op.schema ?? "");
|
|
934
|
-
return prefixTypeNames(doc, documentName, /* @__PURE__ */ new Set([...names, ...definedTypeNames(doc, ALL_PREFIX_KINDS)])).definitions;
|
|
935
|
-
});
|
|
936
|
-
return definitions.length === 0 ? [] : [{
|
|
937
|
-
name: module.name,
|
|
938
|
-
sdl: print({
|
|
939
|
-
kind: Kind.DOCUMENT,
|
|
940
|
-
definitions
|
|
941
|
-
})
|
|
942
|
-
}];
|
|
943
|
-
}) ?? [],
|
|
944
|
-
stateInputTypes: prefixedStateInputTypes,
|
|
945
|
-
globalStateTypeName: documentName === "DocumentModel" ? `${documentName}_${documentName}GlobalState` : `${documentName}_${documentName}State`,
|
|
946
|
-
localStateTypeName: (localStateSchema ?? "").includes(`type ${documentName}LocalState`) ? `${documentName}_${documentName}LocalState` : null,
|
|
947
|
-
initialState: storedInitialState(specification, documentName, allTypeNames)
|
|
948
|
-
};
|
|
949
|
-
}
|
|
950
|
-
/** The new API's initial-state argument, read out of the stored scope SDL. */
|
|
951
|
-
function storedInitialState(specification, documentName, allTypeNames) {
|
|
952
|
-
const scopes = [];
|
|
953
|
-
const generatedInputTypeParts = [];
|
|
954
|
-
if (!specification) return {
|
|
955
|
-
inputTypes: "",
|
|
956
|
-
scopes
|
|
957
|
-
};
|
|
958
|
-
for (const [scopeName, scopeState] of Object.entries(specification.state)) {
|
|
959
|
-
const schema = scopeState.schema ?? "";
|
|
960
|
-
if (!hasValidSchema(schema)) {
|
|
961
|
-
scopes.push({
|
|
962
|
-
name: scopeName,
|
|
963
|
-
type: "JSONObject"
|
|
964
|
-
});
|
|
965
|
-
continue;
|
|
966
|
-
}
|
|
967
|
-
const rootTypeName = extractRootTypeName(schema, documentName, scopeName);
|
|
968
|
-
if (!rootTypeName) {
|
|
969
|
-
scopes.push({
|
|
970
|
-
name: scopeName,
|
|
971
|
-
type: "JSONObject"
|
|
972
|
-
});
|
|
973
|
-
continue;
|
|
974
|
-
}
|
|
975
|
-
const scopeInputTypes = generateStateInputTypes(schema, new Set(allTypeNames));
|
|
976
|
-
if (!scopeInputTypes) {
|
|
977
|
-
scopes.push({
|
|
978
|
-
name: scopeName,
|
|
979
|
-
type: "JSONObject"
|
|
980
|
-
});
|
|
981
|
-
continue;
|
|
982
|
-
}
|
|
983
|
-
scopes.push({
|
|
984
|
-
name: scopeName,
|
|
985
|
-
type: `${documentName}_${rootTypeName}Input`
|
|
986
|
-
});
|
|
987
|
-
generatedInputTypeParts.push(applyGraphQLTypePrefixes(scopeInputTypes, documentName, allTypeNames));
|
|
988
|
-
}
|
|
989
|
-
return {
|
|
990
|
-
inputTypes: generatedInputTypeParts.join("\n\n"),
|
|
991
|
-
scopes
|
|
992
|
-
};
|
|
1191
|
+
return generateModelSchema(documentModelSchemaSource(sourceOf(documentModel)).modelProjection(), options);
|
|
993
1192
|
}
|
|
994
1193
|
//#endregion
|
|
995
1194
|
//#region src/graphql/subgraph-type-ownership.ts
|
|
@@ -1855,7 +2054,7 @@ const baseDocumentModels = [
|
|
|
1855
2054
|
["reactor-group", ReactorGroupV1]
|
|
1856
2055
|
];
|
|
1857
2056
|
//#endregion
|
|
1858
|
-
export { getDocumentModelTypeDefs as A, createMergedSchema as C, generateDocumentModelSchema as D, findSubgraphTypeCollisions as E, typeDefinitionNames as F,
|
|
2057
|
+
export { getDocumentModelTypeDefs as A, schema_default$1 as B, createMergedSchema as C, generateDocumentModelSchema as D, findSubgraphTypeCollisions as E, typeDefinitionNames as F, documentModelSchemaSource as I, getDocumentModelSchemaName as L, hasOperationSchemas as M, reservedTypeNames as N, generatedTypesOf as O, reservedTypeNamesTaken as P, EMPTY_SCALAR_REGISTRY as R, buildSubgraphSchemaModule as S, documentModelSubgraphTypes as T, describeOwnershipConflicts as _, inspectionAdminTypeDefs as a, assembleSubgraphTypeDefs as b, HOST_ORIGIN as c, byClaimPriority as d, decideAdmission as f, collectSubgraphClaims as g, supergraphClaimLosers as h, inspectionTypeDefs as i, getGeneratedDocumentModelTypes as j, getDocumentModelSubgraphName as k, RegistrationOrder as l, documentModelSubgraphSources as m, systemTypeDefs as n, CORE_ORIGIN as o, describeOrigin as p, schema_default as r, DOCUMENT_MODEL_ORIGIN as s, baseDocumentModels as t, admitDocumentModels as u, DocumentModelSchemaCache as v, createSchema as w, buildDocumentModelSchema as x, SubgraphTypeCollisionError as y, sameScalarRegistry as z };
|
|
1859
2058
|
|
|
1860
|
-
//# sourceMappingURL=base-document-models-
|
|
1861
|
-
//# debugId=
|
|
2059
|
+
//# sourceMappingURL=base-document-models-577xnrmL.mjs.map
|
|
2060
|
+
//# debugId=5196867a-cd79-5e29-9a40-a98ed396d70d
|