@soda-gql/codegen 0.0.1
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/LICENSE +21 -0
- package/dist/generator-BgvqYNQA.js +511 -0
- package/dist/generator-BgvqYNQA.js.map +1 -0
- package/dist/generator-DLeYPsgP.js +3 -0
- package/dist/generator-DudCa3Gz.cjs +4 -0
- package/dist/generator-t_NByvnv.cjs +551 -0
- package/dist/index.cjs +270 -0
- package/dist/index.d.cts +91 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +262 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let graphql = require("graphql");
|
|
25
|
+
graphql = __toESM(graphql);
|
|
26
|
+
|
|
27
|
+
//#region packages/codegen/src/generator.ts
|
|
28
|
+
const builtinScalarTypes = new Map([
|
|
29
|
+
["ID", {
|
|
30
|
+
input: "string",
|
|
31
|
+
output: "string"
|
|
32
|
+
}],
|
|
33
|
+
["String", {
|
|
34
|
+
input: "string",
|
|
35
|
+
output: "string"
|
|
36
|
+
}],
|
|
37
|
+
["Int", {
|
|
38
|
+
input: "number",
|
|
39
|
+
output: "number"
|
|
40
|
+
}],
|
|
41
|
+
["Float", {
|
|
42
|
+
input: "number",
|
|
43
|
+
output: "number"
|
|
44
|
+
}],
|
|
45
|
+
["Boolean", {
|
|
46
|
+
input: "boolean",
|
|
47
|
+
output: "boolean"
|
|
48
|
+
}]
|
|
49
|
+
]);
|
|
50
|
+
const ensureRecord = (collection, key, factory) => {
|
|
51
|
+
const existing = collection.get(key);
|
|
52
|
+
if (existing) {
|
|
53
|
+
return existing;
|
|
54
|
+
}
|
|
55
|
+
const created = factory(key);
|
|
56
|
+
collection.set(key, created);
|
|
57
|
+
return created;
|
|
58
|
+
};
|
|
59
|
+
const addObjectFields = (target, fields) => {
|
|
60
|
+
if (!fields) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (const field of fields) {
|
|
64
|
+
target.set(field.name.value, field);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const addInputFields = (target, fields) => {
|
|
68
|
+
if (!fields) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
for (const field of fields) {
|
|
72
|
+
target.set(field.name.value, field);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const addEnumValues = (target, values) => {
|
|
76
|
+
if (!values) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
for (const value of values) {
|
|
80
|
+
target.set(value.name.value, value);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const addUnionMembers = (target, members) => {
|
|
84
|
+
if (!members) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
for (const member of members) {
|
|
88
|
+
target.set(member.name.value, member);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const mergeDirectives = (existing, incoming, precedence) => {
|
|
92
|
+
const current = existing ?? [];
|
|
93
|
+
const next = incoming ? Array.from(incoming) : [];
|
|
94
|
+
return precedence === "definition" ? [...next, ...current] : [...current, ...next];
|
|
95
|
+
};
|
|
96
|
+
const updateOperationTypes = (operationTypes, definition) => {
|
|
97
|
+
for (const operation of definition.operationTypes ?? []) {
|
|
98
|
+
const typeName = operation.type.name.value;
|
|
99
|
+
switch (operation.operation) {
|
|
100
|
+
case "query":
|
|
101
|
+
operationTypes.query = typeName;
|
|
102
|
+
break;
|
|
103
|
+
case "mutation":
|
|
104
|
+
operationTypes.mutation = typeName;
|
|
105
|
+
break;
|
|
106
|
+
case "subscription":
|
|
107
|
+
operationTypes.subscription = typeName;
|
|
108
|
+
break;
|
|
109
|
+
default: break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
const createSchemaIndex = (document) => {
|
|
114
|
+
const objects = new Map();
|
|
115
|
+
const inputs = new Map();
|
|
116
|
+
const enums = new Map();
|
|
117
|
+
const unions = new Map();
|
|
118
|
+
const scalars = new Map();
|
|
119
|
+
const operationTypes = {};
|
|
120
|
+
for (const definition of document.definitions) {
|
|
121
|
+
switch (definition.kind) {
|
|
122
|
+
case graphql.Kind.OBJECT_TYPE_DEFINITION:
|
|
123
|
+
case graphql.Kind.OBJECT_TYPE_EXTENSION: {
|
|
124
|
+
const precedence = definition.kind === graphql.Kind.OBJECT_TYPE_DEFINITION ? "definition" : "extension";
|
|
125
|
+
const record = ensureRecord(objects, definition.name.value, (name) => ({
|
|
126
|
+
name,
|
|
127
|
+
fields: new Map(),
|
|
128
|
+
directives: []
|
|
129
|
+
}));
|
|
130
|
+
addObjectFields(record.fields, definition.fields);
|
|
131
|
+
record.directives = mergeDirectives(record.directives, definition.directives, precedence);
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION:
|
|
135
|
+
case graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION: {
|
|
136
|
+
const precedence = definition.kind === graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION ? "definition" : "extension";
|
|
137
|
+
const record = ensureRecord(inputs, definition.name.value, (name) => ({
|
|
138
|
+
name,
|
|
139
|
+
fields: new Map(),
|
|
140
|
+
directives: []
|
|
141
|
+
}));
|
|
142
|
+
addInputFields(record.fields, definition.fields);
|
|
143
|
+
record.directives = mergeDirectives(record.directives, definition.directives, precedence);
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case graphql.Kind.ENUM_TYPE_DEFINITION:
|
|
147
|
+
case graphql.Kind.ENUM_TYPE_EXTENSION: {
|
|
148
|
+
const precedence = definition.kind === graphql.Kind.ENUM_TYPE_DEFINITION ? "definition" : "extension";
|
|
149
|
+
const record = ensureRecord(enums, definition.name.value, (name) => ({
|
|
150
|
+
name,
|
|
151
|
+
values: new Map(),
|
|
152
|
+
directives: []
|
|
153
|
+
}));
|
|
154
|
+
addEnumValues(record.values, definition.values);
|
|
155
|
+
record.directives = mergeDirectives(record.directives, definition.directives, precedence);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case graphql.Kind.UNION_TYPE_DEFINITION:
|
|
159
|
+
case graphql.Kind.UNION_TYPE_EXTENSION: {
|
|
160
|
+
const precedence = definition.kind === graphql.Kind.UNION_TYPE_DEFINITION ? "definition" : "extension";
|
|
161
|
+
const record = ensureRecord(unions, definition.name.value, (name) => ({
|
|
162
|
+
name,
|
|
163
|
+
members: new Map(),
|
|
164
|
+
directives: []
|
|
165
|
+
}));
|
|
166
|
+
addUnionMembers(record.members, definition.types);
|
|
167
|
+
record.directives = mergeDirectives(record.directives, definition.directives, precedence);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case graphql.Kind.SCALAR_TYPE_DEFINITION:
|
|
171
|
+
case graphql.Kind.SCALAR_TYPE_EXTENSION: {
|
|
172
|
+
const precedence = definition.kind === graphql.Kind.SCALAR_TYPE_DEFINITION ? "definition" : "extension";
|
|
173
|
+
const record = ensureRecord(scalars, definition.name.value, (name) => ({
|
|
174
|
+
name,
|
|
175
|
+
directives: []
|
|
176
|
+
}));
|
|
177
|
+
record.directives = mergeDirectives(record.directives, definition.directives, precedence);
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case graphql.Kind.SCHEMA_DEFINITION:
|
|
181
|
+
case graphql.Kind.SCHEMA_EXTENSION:
|
|
182
|
+
updateOperationTypes(operationTypes, definition);
|
|
183
|
+
break;
|
|
184
|
+
default: break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (!operationTypes.query && objects.has("Query")) {
|
|
188
|
+
operationTypes.query = "Query";
|
|
189
|
+
}
|
|
190
|
+
if (!operationTypes.mutation && objects.has("Mutation")) {
|
|
191
|
+
operationTypes.mutation = "Mutation";
|
|
192
|
+
}
|
|
193
|
+
if (!operationTypes.subscription && objects.has("Subscription")) {
|
|
194
|
+
operationTypes.subscription = "Subscription";
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
objects,
|
|
198
|
+
inputs,
|
|
199
|
+
enums,
|
|
200
|
+
unions,
|
|
201
|
+
scalars,
|
|
202
|
+
operationTypes
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
const collectTypeLevels = (type, nonNull = false, levels = []) => {
|
|
206
|
+
if (type.kind === graphql.Kind.NON_NULL_TYPE) {
|
|
207
|
+
return collectTypeLevels(type.type, true, levels);
|
|
208
|
+
}
|
|
209
|
+
if (type.kind === graphql.Kind.LIST_TYPE) {
|
|
210
|
+
levels.push({
|
|
211
|
+
kind: "list",
|
|
212
|
+
nonNull
|
|
213
|
+
});
|
|
214
|
+
return collectTypeLevels(type.type, false, levels);
|
|
215
|
+
}
|
|
216
|
+
levels.push({
|
|
217
|
+
kind: "named",
|
|
218
|
+
nonNull
|
|
219
|
+
});
|
|
220
|
+
return {
|
|
221
|
+
name: type.name.value,
|
|
222
|
+
levels
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
const buildTypeModifier = (levels) => {
|
|
226
|
+
let modifier = "";
|
|
227
|
+
for (const level of levels.slice().reverse()) {
|
|
228
|
+
if (level.kind === "named") {
|
|
229
|
+
modifier = level.nonNull ? "!" : "";
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const rest = modifier;
|
|
233
|
+
const base = rest.startsWith("!") ? `![]${rest.slice(1)}` : `[]${rest}`;
|
|
234
|
+
modifier = level.nonNull ? `${base}!` : base;
|
|
235
|
+
}
|
|
236
|
+
return modifier || "?";
|
|
237
|
+
};
|
|
238
|
+
const parseTypeReference = (type) => {
|
|
239
|
+
const { name, levels } = collectTypeLevels(type);
|
|
240
|
+
return {
|
|
241
|
+
name,
|
|
242
|
+
modifier: buildTypeModifier(levels)
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
const renderType = (name, modifier) => JSON.stringify(`${name}:${modifier}`);
|
|
246
|
+
const isScalarName = (schema, name) => builtinScalarTypes.has(name) || schema.scalars.has(name);
|
|
247
|
+
const isEnumName = (schema, name) => schema.enums.has(name);
|
|
248
|
+
const _isInputName = (schema, name) => schema.inputs.has(name);
|
|
249
|
+
const isUnionName = (schema, name) => schema.unions.has(name);
|
|
250
|
+
const isObjectName = (schema, name) => schema.objects.has(name);
|
|
251
|
+
const renderConstValue = (value) => {
|
|
252
|
+
switch (value.kind) {
|
|
253
|
+
case graphql.Kind.NULL: return "null";
|
|
254
|
+
case graphql.Kind.INT:
|
|
255
|
+
case graphql.Kind.FLOAT: return value.value;
|
|
256
|
+
case graphql.Kind.STRING:
|
|
257
|
+
case graphql.Kind.ENUM: return JSON.stringify(value.value);
|
|
258
|
+
case graphql.Kind.BOOLEAN: return value.value ? "true" : "false";
|
|
259
|
+
case graphql.Kind.LIST: return `[${value.values.map((item) => renderConstValue(item)).join(", ")}]`;
|
|
260
|
+
case graphql.Kind.OBJECT: {
|
|
261
|
+
if (value.fields.length === 0) {
|
|
262
|
+
return "{}";
|
|
263
|
+
}
|
|
264
|
+
const entries = value.fields.map((field) => `${field.name.value}: ${renderConstValue(field.value)}`);
|
|
265
|
+
return `{ ${entries.join(", ")} }`;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
const renderConstArgumentMap = (args) => {
|
|
270
|
+
const entries = (args ?? []).map((arg) => `${arg.name.value}: ${renderConstValue(arg.value)}`);
|
|
271
|
+
return renderPropertyLines({
|
|
272
|
+
entries,
|
|
273
|
+
indentSize: 8
|
|
274
|
+
});
|
|
275
|
+
};
|
|
276
|
+
const renderDirectives = (directives) => {
|
|
277
|
+
const entries = (directives ?? []).map((directive) => `${directive.name.value}: ${renderConstArgumentMap(directive.arguments)}`);
|
|
278
|
+
return renderPropertyLines({
|
|
279
|
+
entries,
|
|
280
|
+
indentSize: 8
|
|
281
|
+
});
|
|
282
|
+
};
|
|
283
|
+
const renderDefaultValue = (value) => value ? `() => (${renderConstValue(value)})` : "null";
|
|
284
|
+
const renderInputRef = (schema, definition) => {
|
|
285
|
+
const { name, modifier } = parseTypeReference(definition.type);
|
|
286
|
+
const tuple = renderType(name, modifier);
|
|
287
|
+
const defaultValue = renderDefaultValue(definition.defaultValue ?? null);
|
|
288
|
+
const directives = renderDirectives(definition.directives);
|
|
289
|
+
if (isScalarName(schema, name)) {
|
|
290
|
+
return `unsafeInputType.scalar(${tuple}, { default: ${defaultValue}, directives: ${directives} })`;
|
|
291
|
+
}
|
|
292
|
+
if (isEnumName(schema, name)) {
|
|
293
|
+
return `unsafeInputType.enum(${tuple}, { default: ${defaultValue}, directives: ${directives} })`;
|
|
294
|
+
}
|
|
295
|
+
return `unsafeInputType.input(${tuple}, { default: ${defaultValue}, directives: ${directives} })`;
|
|
296
|
+
};
|
|
297
|
+
const renderArgumentMap = (schema, args) => {
|
|
298
|
+
const entries = [...args ?? []].sort((left, right) => left.name.value.localeCompare(right.name.value)).map((arg) => `${arg.name.value}: ${renderInputRef(schema, arg)}`);
|
|
299
|
+
return renderPropertyLines({
|
|
300
|
+
entries,
|
|
301
|
+
indentSize: 8
|
|
302
|
+
});
|
|
303
|
+
};
|
|
304
|
+
const renderOutputRef = (schema, type, args, directives) => {
|
|
305
|
+
const { name, modifier } = parseTypeReference(type);
|
|
306
|
+
const modifiedType = renderType(name, modifier);
|
|
307
|
+
const argumentMap = renderArgumentMap(schema, args);
|
|
308
|
+
const directiveMap = renderDirectives(directives);
|
|
309
|
+
if (isScalarName(schema, name)) {
|
|
310
|
+
return `unsafeOutputType.scalar(${modifiedType}, { arguments: ${argumentMap}, directives: ${directiveMap} })`;
|
|
311
|
+
}
|
|
312
|
+
if (isEnumName(schema, name)) {
|
|
313
|
+
return `unsafeOutputType.enum(${modifiedType}, { arguments: ${argumentMap}, directives: ${directiveMap} })`;
|
|
314
|
+
}
|
|
315
|
+
if (isUnionName(schema, name)) {
|
|
316
|
+
return `unsafeOutputType.union(${modifiedType}, { arguments: ${argumentMap}, directives: ${directiveMap} })`;
|
|
317
|
+
}
|
|
318
|
+
if (isObjectName(schema, name)) {
|
|
319
|
+
return `unsafeOutputType.object(${modifiedType}, { arguments: ${argumentMap}, directives: ${directiveMap} })`;
|
|
320
|
+
}
|
|
321
|
+
return `unsafeOutputType.scalar(${modifiedType}, { arguments: ${argumentMap}, directives: ${directiveMap} })`;
|
|
322
|
+
};
|
|
323
|
+
const renderPropertyLines = ({ entries, indentSize }) => {
|
|
324
|
+
if (entries.length === 0) {
|
|
325
|
+
return "{}";
|
|
326
|
+
}
|
|
327
|
+
const indent = " ".repeat(indentSize);
|
|
328
|
+
const lastIndent = " ".repeat(indentSize - 2);
|
|
329
|
+
return [
|
|
330
|
+
"{",
|
|
331
|
+
`${indent}${entries.join(`,\n${indent}`)},`,
|
|
332
|
+
`${lastIndent}}`
|
|
333
|
+
].join(`\n`);
|
|
334
|
+
};
|
|
335
|
+
const renderObjectFields = (schema, fields) => {
|
|
336
|
+
const entries = Array.from(fields.values()).sort((left, right) => left.name.value.localeCompare(right.name.value)).map((field) => `${field.name.value}: ${renderOutputRef(schema, field.type, field.arguments, field.directives)}`);
|
|
337
|
+
return renderPropertyLines({
|
|
338
|
+
entries,
|
|
339
|
+
indentSize: 6
|
|
340
|
+
});
|
|
341
|
+
};
|
|
342
|
+
const renderInputFields = (schema, fields) => {
|
|
343
|
+
const entries = Array.from(fields.values()).sort((left, right) => left.name.value.localeCompare(right.name.value)).map((field) => `${field.name.value}: ${renderInputRef(schema, field)}`);
|
|
344
|
+
return renderPropertyLines({
|
|
345
|
+
entries,
|
|
346
|
+
indentSize: 6
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
const renderScalarDefinition = (record) => {
|
|
350
|
+
const typeInfo = builtinScalarTypes.get(record.name) ?? {
|
|
351
|
+
input: "string",
|
|
352
|
+
output: "string"
|
|
353
|
+
};
|
|
354
|
+
const scalarType = `type<{ input: ${typeInfo.input}; output: ${typeInfo.output} }>()`;
|
|
355
|
+
return `${record.name}: define("${record.name}").scalar(${scalarType}, ${renderDirectives(record.directives)})`;
|
|
356
|
+
};
|
|
357
|
+
const renderObjectDefinition = (schema, typeName) => {
|
|
358
|
+
const record = schema.objects.get(typeName);
|
|
359
|
+
if (!record) {
|
|
360
|
+
return "";
|
|
361
|
+
}
|
|
362
|
+
const fields = renderObjectFields(schema, record.fields);
|
|
363
|
+
return `${record.name}: define("${record.name}").object(${fields}, ${renderDirectives(record.directives)})`;
|
|
364
|
+
};
|
|
365
|
+
const renderInputDefinition = (schema, typeName) => {
|
|
366
|
+
const record = schema.inputs.get(typeName);
|
|
367
|
+
if (!record) {
|
|
368
|
+
return "";
|
|
369
|
+
}
|
|
370
|
+
const fields = renderInputFields(schema, record.fields);
|
|
371
|
+
return `${record.name}: define("${record.name}").input(${fields}, ${renderDirectives(record.directives)})`;
|
|
372
|
+
};
|
|
373
|
+
const renderEnumDefinition = (schema, typeName) => {
|
|
374
|
+
const record = schema.enums.get(typeName);
|
|
375
|
+
if (!record) {
|
|
376
|
+
return "";
|
|
377
|
+
}
|
|
378
|
+
const values = Array.from(record.values.values()).sort((left, right) => left.name.value.localeCompare(right.name.value)).map((value) => `${value.name.value}: true`).join(", ");
|
|
379
|
+
const body = values.length === 0 ? "{}" : `{ ${values} }`;
|
|
380
|
+
return `${record.name}: define("${record.name}").enum(${body}, ${renderDirectives(record.directives)})`;
|
|
381
|
+
};
|
|
382
|
+
const renderUnionDefinition = (schema, typeName) => {
|
|
383
|
+
const record = schema.unions.get(typeName);
|
|
384
|
+
if (!record) {
|
|
385
|
+
return "";
|
|
386
|
+
}
|
|
387
|
+
const members = Array.from(record.members.values()).sort((left, right) => left.name.value.localeCompare(right.name.value)).map((member) => `${member.name.value}: true`).join(", ");
|
|
388
|
+
const body = members.length === 0 ? "{}" : `{ ${members} }`;
|
|
389
|
+
return `${record.name}: define("${record.name}").union(${body}, ${renderDirectives(record.directives)})`;
|
|
390
|
+
};
|
|
391
|
+
const collectObjectTypeNames = (schema) => Array.from(schema.objects.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
|
|
392
|
+
const collectInputTypeNames = (schema) => Array.from(schema.inputs.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
|
|
393
|
+
const collectEnumTypeNames = (schema) => Array.from(schema.enums.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
|
|
394
|
+
const collectUnionTypeNames = (schema) => Array.from(schema.unions.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
|
|
395
|
+
const collectScalarNames = (schema) => Array.from(schema.scalars.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
|
|
396
|
+
const multiRuntimeTemplate = ($$) => {
|
|
397
|
+
const imports = [];
|
|
398
|
+
const adapterAliases = new Map();
|
|
399
|
+
const scalarAliases = new Map();
|
|
400
|
+
if ($$.injection.mode === "inject") {
|
|
401
|
+
for (const [schemaName, injection] of $$.injection.perSchema) {
|
|
402
|
+
const adapterAlias = `adapter_${schemaName}`;
|
|
403
|
+
const scalarAlias = `scalar_${schemaName}`;
|
|
404
|
+
adapterAliases.set(schemaName, adapterAlias);
|
|
405
|
+
scalarAliases.set(schemaName, scalarAlias);
|
|
406
|
+
imports.push(`import { adapter as ${adapterAlias} } from "${injection.adapterImportPath}";`);
|
|
407
|
+
imports.push(`import { scalar as ${scalarAlias} } from "${injection.scalarImportPath}";`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
const extraImports = imports.length > 0 ? `${imports.join("\n")}\n` : "";
|
|
411
|
+
const schemaBlocks = [];
|
|
412
|
+
const gqlEntries = [];
|
|
413
|
+
for (const [name, config] of Object.entries($$.schemas)) {
|
|
414
|
+
const schemaVar = `${name}Schema`;
|
|
415
|
+
const adapterVar = $$.injection.mode === "inject" ? adapterAliases.get(name) : `adapter_${name}`;
|
|
416
|
+
const scalarBlock = $$.injection.mode === "inject" ? scalarAliases.get(name) : config.scalarBlock;
|
|
417
|
+
const adapterDefinition = $$.injection.mode === "inject" ? "" : `const ${adapterVar} = createRuntimeAdapter(({ type }) => ({\n nonGraphqlErrorType: type<{ type: "non-graphql-error"; cause: unknown }>(),\n}));\n`;
|
|
418
|
+
schemaBlocks.push(`${adapterDefinition}
|
|
419
|
+
const ${schemaVar} = {
|
|
420
|
+
operations: defineOperationRoots({
|
|
421
|
+
query: "${config.queryType}",
|
|
422
|
+
mutation: "${config.mutationType}",
|
|
423
|
+
subscription: "${config.subscriptionType}",
|
|
424
|
+
}),
|
|
425
|
+
scalar: ${scalarBlock},
|
|
426
|
+
enum: ${config.enumBlock},
|
|
427
|
+
input: ${config.inputBlock},
|
|
428
|
+
object: ${config.objectBlock},
|
|
429
|
+
union: ${config.unionBlock},
|
|
430
|
+
} satisfies AnyGraphqlSchema;
|
|
431
|
+
|
|
432
|
+
export type Schema_${name} = typeof ${schemaVar} & { _?: never };
|
|
433
|
+
export type Adapter_${name} = typeof ${adapterVar} & { _?: never };`);
|
|
434
|
+
gqlEntries.push(` ${name}: createGqlElementComposer<Schema_${name}, Adapter_${name}>(${schemaVar})`);
|
|
435
|
+
}
|
|
436
|
+
const runtimeImport = $$.injection.mode === "inline" ? "\nimport { createRuntimeAdapter } from \"@soda-gql/runtime\";" : "";
|
|
437
|
+
return `\
|
|
438
|
+
import {
|
|
439
|
+
type AnyGraphqlSchema,
|
|
440
|
+
createGqlElementComposer,
|
|
441
|
+
define,
|
|
442
|
+
defineOperationRoots,
|
|
443
|
+
unsafeInputType,
|
|
444
|
+
unsafeOutputType,
|
|
445
|
+
} from "@soda-gql/core";${runtimeImport}
|
|
446
|
+
${extraImports}
|
|
447
|
+
${schemaBlocks.join("\n")}
|
|
448
|
+
|
|
449
|
+
export const gql = {
|
|
450
|
+
${gqlEntries.join(",\n")}
|
|
451
|
+
};
|
|
452
|
+
`;
|
|
453
|
+
};
|
|
454
|
+
const generateMultiSchemaModule = (schemas, options) => {
|
|
455
|
+
const schemaConfigs = {};
|
|
456
|
+
const allStats = {
|
|
457
|
+
objects: 0,
|
|
458
|
+
enums: 0,
|
|
459
|
+
inputs: 0,
|
|
460
|
+
unions: 0
|
|
461
|
+
};
|
|
462
|
+
for (const [name, document] of schemas.entries()) {
|
|
463
|
+
const schema = createSchemaIndex(document);
|
|
464
|
+
const builtinScalarDefinitions = Array.from(builtinScalarTypes.keys()).map((name$1) => renderScalarDefinition(schema.scalars.get(name$1) ?? {
|
|
465
|
+
name: name$1,
|
|
466
|
+
directives: []
|
|
467
|
+
}));
|
|
468
|
+
const customScalarDefinitions = collectScalarNames(schema).filter((name$1) => !builtinScalarTypes.has(name$1)).map((name$1) => {
|
|
469
|
+
const record = schema.scalars.get(name$1);
|
|
470
|
+
return record ? renderScalarDefinition(record) : "";
|
|
471
|
+
}).filter((definition) => definition.length > 0);
|
|
472
|
+
const allScalarDefinitions = builtinScalarDefinitions.concat(customScalarDefinitions);
|
|
473
|
+
const objectTypeNames = collectObjectTypeNames(schema);
|
|
474
|
+
const enumTypeNames = collectEnumTypeNames(schema);
|
|
475
|
+
const inputTypeNames = collectInputTypeNames(schema);
|
|
476
|
+
const unionTypeNames = collectUnionTypeNames(schema);
|
|
477
|
+
const scalarBlock = renderPropertyLines({
|
|
478
|
+
entries: allScalarDefinitions,
|
|
479
|
+
indentSize: 4
|
|
480
|
+
});
|
|
481
|
+
const enumDefinitions = enumTypeNames.map((name$1) => renderEnumDefinition(schema, name$1)).filter((definition) => definition.length > 0);
|
|
482
|
+
const enumBlock = renderPropertyLines({
|
|
483
|
+
entries: enumDefinitions,
|
|
484
|
+
indentSize: 4
|
|
485
|
+
});
|
|
486
|
+
const inputDefinitions = inputTypeNames.map((name$1) => renderInputDefinition(schema, name$1)).filter((definition) => definition.length > 0);
|
|
487
|
+
const inputBlock = renderPropertyLines({
|
|
488
|
+
entries: inputDefinitions,
|
|
489
|
+
indentSize: 4
|
|
490
|
+
});
|
|
491
|
+
const objectDefinitions = objectTypeNames.map((name$1) => renderObjectDefinition(schema, name$1)).filter((definition) => definition.length > 0);
|
|
492
|
+
const objectBlock = renderPropertyLines({
|
|
493
|
+
entries: objectDefinitions,
|
|
494
|
+
indentSize: 4
|
|
495
|
+
});
|
|
496
|
+
const unionDefinitions = unionTypeNames.map((name$1) => renderUnionDefinition(schema, name$1)).filter((definition) => definition.length > 0);
|
|
497
|
+
const unionBlock = renderPropertyLines({
|
|
498
|
+
entries: unionDefinitions,
|
|
499
|
+
indentSize: 4
|
|
500
|
+
});
|
|
501
|
+
const queryType = schema.operationTypes.query ?? "Query";
|
|
502
|
+
const mutationType = schema.operationTypes.mutation ?? "Mutation";
|
|
503
|
+
const subscriptionType = schema.operationTypes.subscription ?? "Subscription";
|
|
504
|
+
schemaConfigs[name] = {
|
|
505
|
+
queryType,
|
|
506
|
+
mutationType,
|
|
507
|
+
subscriptionType,
|
|
508
|
+
scalarBlock,
|
|
509
|
+
enumBlock,
|
|
510
|
+
inputBlock,
|
|
511
|
+
objectBlock,
|
|
512
|
+
unionBlock
|
|
513
|
+
};
|
|
514
|
+
allStats.objects += objectDefinitions.length;
|
|
515
|
+
allStats.enums += enumDefinitions.length;
|
|
516
|
+
allStats.inputs += inputDefinitions.length;
|
|
517
|
+
allStats.unions += unionDefinitions.length;
|
|
518
|
+
}
|
|
519
|
+
const injection = options?.injection ? {
|
|
520
|
+
mode: "inject",
|
|
521
|
+
perSchema: options.injection
|
|
522
|
+
} : { mode: "inline" };
|
|
523
|
+
const code = multiRuntimeTemplate({
|
|
524
|
+
schemas: schemaConfigs,
|
|
525
|
+
injection
|
|
526
|
+
});
|
|
527
|
+
return {
|
|
528
|
+
code,
|
|
529
|
+
stats: allStats
|
|
530
|
+
};
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
//#endregion
|
|
534
|
+
Object.defineProperty(exports, '__toESM', {
|
|
535
|
+
enumerable: true,
|
|
536
|
+
get: function () {
|
|
537
|
+
return __toESM;
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
Object.defineProperty(exports, 'createSchemaIndex', {
|
|
541
|
+
enumerable: true,
|
|
542
|
+
get: function () {
|
|
543
|
+
return createSchemaIndex;
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
Object.defineProperty(exports, 'generateMultiSchemaModule', {
|
|
547
|
+
enumerable: true,
|
|
548
|
+
get: function () {
|
|
549
|
+
return generateMultiSchemaModule;
|
|
550
|
+
}
|
|
551
|
+
});
|