@baeta/plugin-graphql 2.0.0-next.2 → 2.0.0-next.7
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 +17 -0
- package/README.md +42 -17
- package/dist/index.d.ts +5 -3
- package/dist/index.js +540 -632
- package/dist/index.js.map +1 -1
- package/package.json +18 -10
package/dist/index.js
CHANGED
|
@@ -1,314 +1,276 @@
|
|
|
1
|
-
// index.ts
|
|
2
1
|
import { createPluginV1, isMatch } from "@baeta/generator-sdk";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { join as join2 } from "@baeta/util-path";
|
|
6
|
-
import { concatAST } from "graphql";
|
|
7
|
-
|
|
8
|
-
// utils/load.ts
|
|
2
|
+
import { join, normalize, posixPath, relative } from "@baeta/util-path";
|
|
3
|
+
import { Kind, concatAST, validateSchema, visit } from "graphql";
|
|
9
4
|
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
} from "
|
|
13
|
-
import {
|
|
14
|
-
getDocumentNodeFromSchema
|
|
15
|
-
} from "@graphql-tools/utils";
|
|
16
|
-
import { validateSchema } from "graphql";
|
|
17
|
-
async function loadSchema(schemas, cwd, extraLoaders = []) {
|
|
18
|
-
const schemaPointerMap = {};
|
|
19
|
-
for (const ptr of Array.isArray(schemas) ? schemas : [schemas]) {
|
|
20
|
-
schemaPointerMap[ptr] = {};
|
|
21
|
-
}
|
|
22
|
-
const outputSchemaAst = await loadSchemaToolkit(schemaPointerMap, {
|
|
23
|
-
loaders: [new GraphQLFileLoader(), ...extraLoaders],
|
|
24
|
-
cwd,
|
|
25
|
-
includeSources: true
|
|
26
|
-
});
|
|
27
|
-
const errors = validateSchema(outputSchemaAst);
|
|
28
|
-
if (errors.length > 0) {
|
|
29
|
-
const messages = errors.map((e) => e.toString()).join("\n\n--------------------\n\n");
|
|
30
|
-
const subject = errors.length === 1 ? "error" : "errors";
|
|
31
|
-
throw new Error(`Invalid schema. Found ${errors.length} ${subject}:
|
|
5
|
+
import { loadSchema } from "@graphql-tools/load";
|
|
6
|
+
import { getDocumentNodeFromSchema } from "@graphql-tools/utils";
|
|
7
|
+
import { camelCase, pascalCase } from "change-case-all";
|
|
32
8
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
9
|
+
//#region utils/load.ts
|
|
10
|
+
async function loadSchema$1(schemas, cwd, extraLoaders = []) {
|
|
11
|
+
const schemaPointerMap = {};
|
|
12
|
+
for (const ptr of Array.isArray(schemas) ? schemas : [schemas]) schemaPointerMap[ptr] = {};
|
|
13
|
+
const outputSchemaAst = await loadSchema(schemaPointerMap, {
|
|
14
|
+
loaders: [new GraphQLFileLoader(), ...extraLoaders],
|
|
15
|
+
cwd,
|
|
16
|
+
includeSources: true
|
|
17
|
+
});
|
|
18
|
+
const errors = validateSchema(outputSchemaAst);
|
|
19
|
+
if (errors.length > 0) {
|
|
20
|
+
const messages = errors.map((e) => e.toString()).join("\n\n--------------------\n\n");
|
|
21
|
+
const subject = errors.length === 1 ? "error" : "errors";
|
|
22
|
+
throw new Error(`Invalid schema. Found ${errors.length} ${subject}:\n\n${messages}`);
|
|
23
|
+
}
|
|
24
|
+
if (!outputSchemaAst.extensions) outputSchemaAst.extensions = {};
|
|
25
|
+
return {
|
|
26
|
+
outputSchemaAst,
|
|
27
|
+
outputSchema: getDocumentNodeFromSchema(outputSchemaAst)
|
|
28
|
+
};
|
|
42
29
|
}
|
|
43
30
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region utils/path.ts
|
|
33
|
+
const sep = "/";
|
|
47
34
|
function getRelativePath(filepath, basePath) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const [, relativePath] = normalizedFilepath.split(normalizedBasePath);
|
|
53
|
-
return relativePath;
|
|
35
|
+
const normalizedFilepath = normalize(filepath);
|
|
36
|
+
const normalizedBasePath = ensureStartsWithSeparator(ensureEndsWithSeparator(normalize(basePath)));
|
|
37
|
+
const [, relativePath] = normalizedFilepath.split(normalizedBasePath);
|
|
38
|
+
return relativePath;
|
|
54
39
|
}
|
|
55
40
|
function extractModuleDirectory(relativePath) {
|
|
56
|
-
|
|
57
|
-
|
|
41
|
+
const [moduleDirectory] = relativePath.split(sep);
|
|
42
|
+
return moduleDirectory;
|
|
58
43
|
}
|
|
59
|
-
function ensureStartsWithSeparator(
|
|
60
|
-
|
|
44
|
+
function ensureStartsWithSeparator(path$1) {
|
|
45
|
+
return path$1.startsWith(sep) ? path$1 : sep + path$1;
|
|
61
46
|
}
|
|
62
|
-
function ensureEndsWithSeparator(
|
|
63
|
-
|
|
47
|
+
function ensureEndsWithSeparator(path$1) {
|
|
48
|
+
return path$1.endsWith(sep) ? path$1 : path$1 + sep;
|
|
64
49
|
}
|
|
65
50
|
|
|
66
|
-
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region utils/source.ts
|
|
67
53
|
function groupSourcesByModule(sources, basePath) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
existing.push(source);
|
|
80
|
-
map.set(mod, existing);
|
|
81
|
-
}
|
|
82
|
-
return map;
|
|
54
|
+
const map = /* @__PURE__ */ new Map();
|
|
55
|
+
for (const source of sources) {
|
|
56
|
+
if (!source.location) continue;
|
|
57
|
+
const relativePath = getRelativePath(source.location, basePath);
|
|
58
|
+
if (!relativePath) continue;
|
|
59
|
+
const mod = extractModuleDirectory(relativePath);
|
|
60
|
+
const existing = map.get(mod) ?? [];
|
|
61
|
+
existing.push(source);
|
|
62
|
+
map.set(mod, existing);
|
|
63
|
+
}
|
|
64
|
+
return map;
|
|
83
65
|
}
|
|
84
66
|
function getSourcesFromSchema(schema) {
|
|
85
|
-
|
|
86
|
-
return extensions?.extendedSources ?? [];
|
|
67
|
+
return schema.extensions?.extendedSources ?? [];
|
|
87
68
|
}
|
|
88
69
|
|
|
89
|
-
|
|
90
|
-
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region lib/printer/printer-autoload.ts
|
|
91
72
|
function printAutoload(config, modules) {
|
|
92
|
-
|
|
73
|
+
return [printImports(config, modules), printExport(modules)].join("\n\n");
|
|
93
74
|
}
|
|
94
75
|
function printImports(config, modules) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
76
|
+
const dependencyImports = ["import type { ModuleCompilerFactory } from \"@baeta/core/sdk\";", `import type { Ctx, Info } from "./types${config.importExtension}"`];
|
|
77
|
+
const moduleTypeImports = modules.map((module) => `import type { BaetaModuleTypes as ${pascalCase(module)}ModuleTypes } from "./${module}/typedef${config.importExtension}"`);
|
|
78
|
+
const moduleImports = modules.flatMap((module) => `import ${camelCase(module)} from "./${module}/index${config.importExtension}"`);
|
|
79
|
+
return [
|
|
80
|
+
...dependencyImports,
|
|
81
|
+
...moduleTypeImports,
|
|
82
|
+
...moduleImports
|
|
83
|
+
].join("\n");
|
|
98
84
|
}
|
|
99
85
|
function printExport(modules) {
|
|
100
|
-
|
|
86
|
+
return `export default [\n${modules.map(printModuleWithSatisfies).join(",\n")}\n];`;
|
|
87
|
+
}
|
|
88
|
+
function printModuleWithSatisfies(module) {
|
|
89
|
+
return ` ${camelCase(module)} satisfies ModuleCompilerFactory<Ctx, Info, ${pascalCase(module)}ModuleTypes["Factories"]>`;
|
|
101
90
|
}
|
|
102
91
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
import { pascalCase } from "change-case-all";
|
|
106
|
-
|
|
107
|
-
// lib/printer/printer-utils.ts
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region lib/printer/printer-utils.ts
|
|
108
94
|
function buildBlock({ name, lines }) {
|
|
109
|
-
|
|
95
|
+
return [
|
|
96
|
+
`${name} {`,
|
|
97
|
+
...lines.filter(Boolean).map(indent(2)),
|
|
98
|
+
"};"
|
|
99
|
+
].join("\n");
|
|
110
100
|
}
|
|
111
101
|
function buildCodeBlock({ name, lines }) {
|
|
112
|
-
|
|
113
|
-
|
|
102
|
+
const linesWithSep = lines.filter(Boolean).map(indent(2)).join(",\n");
|
|
103
|
+
return [
|
|
104
|
+
`${name} {`,
|
|
105
|
+
linesWithSep,
|
|
106
|
+
"}"
|
|
107
|
+
].join("\n");
|
|
114
108
|
}
|
|
115
109
|
function indent(size) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
110
|
+
const space = new Array(size).fill(" ").join("");
|
|
111
|
+
function indentInner(val) {
|
|
112
|
+
return val.split("\n").map((line) => `${space}${line}`).join("\n");
|
|
113
|
+
}
|
|
114
|
+
return indentInner;
|
|
115
|
+
}
|
|
116
|
+
function makeRelativePathForImport(from, to) {
|
|
117
|
+
const res = posixPath(relative(from, to));
|
|
118
|
+
if (res.startsWith(".") || res.startsWith("/")) return res;
|
|
119
|
+
return `./${res}`;
|
|
121
120
|
}
|
|
122
121
|
|
|
123
|
-
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region lib/printer/printer-module.ts
|
|
124
124
|
function printModuleImports(config, moduleName) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
125
|
+
const typesDir = makeRelativePathForImport(join(config.modulesDir, moduleName), config.typesDir);
|
|
126
|
+
return [
|
|
127
|
+
"import type { DocumentNode, GraphQLScalarType } from \"graphql\";",
|
|
128
|
+
"import * as Baeta from \"@baeta/core/sdk\";",
|
|
129
|
+
`import extensions from "../extensions${config.importExtension}";`,
|
|
130
|
+
`import type {Ctx, Info} from "../types${config.importExtension}";`,
|
|
131
|
+
`import type * as Types from "${typesDir}/types${config.importExtension}";`
|
|
132
|
+
].join("\n");
|
|
133
133
|
}
|
|
134
134
|
function printModuleMetadata(name, doc) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
135
|
+
return buildCodeBlock({
|
|
136
|
+
name: "const moduleMetadata =",
|
|
137
|
+
lines: [
|
|
138
|
+
`id: '${name}'`,
|
|
139
|
+
`dirname: './${name}'`,
|
|
140
|
+
`typedef: ${JSON.stringify(doc)} as unknown as DocumentNode`
|
|
141
|
+
]
|
|
142
|
+
});
|
|
143
143
|
}
|
|
144
144
|
function printBaetaModuleTypes(config) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
...printBaetaModuleTypesForFields(config, true),
|
|
156
|
-
...printBaetaModuleTypesScalars(config)
|
|
157
|
-
]
|
|
158
|
-
})
|
|
159
|
-
]
|
|
160
|
-
});
|
|
145
|
+
return buildBlock({
|
|
146
|
+
name: "export interface BaetaModuleTypes",
|
|
147
|
+
lines: [buildBlock({
|
|
148
|
+
name: "Builders:",
|
|
149
|
+
lines: printBaetaModuleTypesForFields(config, false)
|
|
150
|
+
}), buildBlock({
|
|
151
|
+
name: "Factories:",
|
|
152
|
+
lines: [...printBaetaModuleTypesForFields(config, true), ...printBaetaModuleTypesScalars(config)]
|
|
153
|
+
})]
|
|
154
|
+
});
|
|
161
155
|
}
|
|
162
156
|
function printBaetaModuleTypesForFields(config, isFactory) {
|
|
163
|
-
|
|
164
|
-
(typeName) => printObjectTypeModuleType(typeName, config.registry.picks.objects, isFactory)
|
|
165
|
-
).filter(Boolean);
|
|
157
|
+
return config.registry.defined.objects.map((typeName) => printObjectTypeModuleType(typeName, config.registry.picks.objects, isFactory)).filter(Boolean);
|
|
166
158
|
}
|
|
167
159
|
function printObjectTypeModuleType(typeName, objects, isFactory) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const infoType = getInfoType();
|
|
175
|
-
if (isFactory) {
|
|
176
|
-
return `${typeName}: Baeta.TypeCompilerFactory<${parentType}, ${contextType}, ${infoType}, BaetaModuleObjectTypeFields['${typeName}']['Factory']>`;
|
|
177
|
-
}
|
|
178
|
-
return `${typeName}: Baeta.TypeMethods<${parentType}, ${contextType}, ${infoType}, BaetaModuleObjectTypeFields['${typeName}']['Builder'], BaetaModuleObjectTypeFields['${typeName}']['Factory']>`;
|
|
160
|
+
if (!objects[typeName]) return "";
|
|
161
|
+
const parentType = getParentType(typeName);
|
|
162
|
+
const contextType = getContextType();
|
|
163
|
+
const infoType = getInfoType();
|
|
164
|
+
if (isFactory) return `${typeName}: Baeta.TypeCompilerFactory<${parentType}, ${contextType}, ${infoType}, BaetaModuleObjectTypeFields['${typeName}']['Factory']>`;
|
|
165
|
+
return `${typeName}: Baeta.TypeMethods<${parentType}, ${contextType}, ${infoType}, BaetaModuleObjectTypeFields['${typeName}']['Builder'], BaetaModuleObjectTypeFields['${typeName}']['Factory']>`;
|
|
179
166
|
}
|
|
180
167
|
function printBaetaModuleTypesScalars(config) {
|
|
181
|
-
|
|
168
|
+
return config.registry.defined.scalars.map((scalar) => `${scalar}: GraphQLScalarType`);
|
|
182
169
|
}
|
|
183
170
|
function printModuleObjectTypeFields(config) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
});
|
|
171
|
+
return buildBlock({
|
|
172
|
+
name: "interface BaetaModuleObjectTypeFields",
|
|
173
|
+
lines: config.registry.defined.objects.map((typeName) => printObjectTypeFields(config, typeName, config.registry.picks.objects)).filter(Boolean)
|
|
174
|
+
});
|
|
189
175
|
}
|
|
190
176
|
function printObjectTypeFields(config, typeName, objects) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
name: "Builder:",
|
|
206
|
-
lines: fieldsBuilders
|
|
207
|
-
}),
|
|
208
|
-
buildBlock({
|
|
209
|
-
name: "Factory:",
|
|
210
|
-
lines: fieldsFactories
|
|
211
|
-
})
|
|
212
|
-
]
|
|
213
|
-
});
|
|
177
|
+
const fields = objects[typeName];
|
|
178
|
+
if (!fields || fields.length === 0) return "";
|
|
179
|
+
const fieldsBuilders = fields.map((field) => printObjectTypeFieldBuilders(config, typeName, field));
|
|
180
|
+
const fieldsFactories = fields.map((field) => printObjectTypeFieldFactories(config, typeName, field));
|
|
181
|
+
return buildBlock({
|
|
182
|
+
name: `${typeName}:`,
|
|
183
|
+
lines: [buildBlock({
|
|
184
|
+
name: "Builder:",
|
|
185
|
+
lines: fieldsBuilders
|
|
186
|
+
}), buildBlock({
|
|
187
|
+
name: "Factory:",
|
|
188
|
+
lines: fieldsFactories
|
|
189
|
+
})]
|
|
190
|
+
});
|
|
214
191
|
}
|
|
215
192
|
function printObjectTypeFieldBuilders(config, typeName, field) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return `${field}: Baeta.${namespace}<${resultType}, ${parentType}, ${contextType}, ${argumentsType}, ${infoType}>`;
|
|
193
|
+
const parentType = getParentType(typeName);
|
|
194
|
+
const resultType = getResultType(config, typeName, field);
|
|
195
|
+
const argumentsType = getArgsType(config, typeName, field);
|
|
196
|
+
const contextType = getContextType();
|
|
197
|
+
const infoType = getInfoType();
|
|
198
|
+
return `${field}: Baeta.${typeName === "Subscription" ? "SubscriptionMethods" : "FieldMethods"}<${resultType}, ${parentType}, ${contextType}, ${argumentsType}, ${infoType}>`;
|
|
223
199
|
}
|
|
224
200
|
function printObjectTypeFieldFactories(config, typeName, field) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return `${field}: Baeta.${namespace}<${resultType}, ${resultType}, ${parentType}, ${contextType}, ${argumentsType}, ${infoType}>`;
|
|
201
|
+
const parentType = getParentType(typeName);
|
|
202
|
+
const resultType = getResultType(config, typeName, field);
|
|
203
|
+
const argumentsType = getArgsType(config, typeName, field);
|
|
204
|
+
const contextType = getContextType();
|
|
205
|
+
const infoType = getInfoType();
|
|
206
|
+
return `${field}: Baeta.${typeName === "Subscription" ? "SubscriptionField" : "Field"}<${resultType}, ${resultType}, ${parentType}, ${contextType}, ${argumentsType}, ${infoType}>`;
|
|
232
207
|
}
|
|
233
208
|
function printModuleBuilder(config, moduleName) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
typeNameResolvers,
|
|
252
|
-
`, ${getExtensionsVar()});`
|
|
253
|
-
].join("");
|
|
209
|
+
const builders = buildCodeBlock({
|
|
210
|
+
name: "",
|
|
211
|
+
lines: config.registry.defined.objects.map((typeName) => printObjectTypeBuilder(typeName, config.registry.picks.objects)).filter(Boolean)
|
|
212
|
+
});
|
|
213
|
+
const typeNameResolvers = buildCodeBlock({
|
|
214
|
+
name: "",
|
|
215
|
+
lines: [...config.registry.defined.unions, ...config.registry.defined.interfaces].map((name) => `${name}: ${printTypeNameResolver()}`)
|
|
216
|
+
});
|
|
217
|
+
const infoType = getInfoType();
|
|
218
|
+
const contextType = getContextType();
|
|
219
|
+
return [
|
|
220
|
+
`export const ${pascalCase(moduleName)}Module = Baeta.createModuleBuilder<${contextType}, ${infoType}, BaetaModuleTypes['Builders'], BaetaModuleTypes['Factories']>(moduleMetadata.id, moduleMetadata.typedef,`,
|
|
221
|
+
builders,
|
|
222
|
+
",",
|
|
223
|
+
typeNameResolvers,
|
|
224
|
+
`, ${getExtensionsVar()});`
|
|
225
|
+
].join("");
|
|
254
226
|
}
|
|
255
227
|
function printObjectTypeBuilder(typeName, objects) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
lines: fields
|
|
263
|
-
});
|
|
264
|
-
return `${typeName}: Baeta.createTypeBuilder("${typeName}",${content}, ${getExtensionsVar()})`;
|
|
228
|
+
const fields = objects[typeName]?.map((field) => printObjectTypeFieldBuilder(typeName, field));
|
|
229
|
+
if (fields == null || fields.length === 0) return "";
|
|
230
|
+
return `${typeName}: Baeta.createTypeBuilder("${typeName}",${buildCodeBlock({
|
|
231
|
+
name: "",
|
|
232
|
+
lines: fields
|
|
233
|
+
})}, ${getExtensionsVar()})`;
|
|
265
234
|
}
|
|
266
235
|
function printTypeNameResolver() {
|
|
267
|
-
|
|
236
|
+
return "{ __resolveType: (source: {__typename: string}) => { return source.__typename; }}";
|
|
268
237
|
}
|
|
269
238
|
function getParentType(type) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
239
|
+
if ([
|
|
240
|
+
"Query",
|
|
241
|
+
"Mutation",
|
|
242
|
+
"Subscription"
|
|
243
|
+
].includes(type)) return "{}";
|
|
244
|
+
return `Types.${type}`;
|
|
274
245
|
}
|
|
275
246
|
function getResultType(config, type, field) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
return fieldType;
|
|
247
|
+
const fieldType = config.fieldInfo.get(type)?.get(field)?.type;
|
|
248
|
+
if (fieldType == null) return "{}";
|
|
249
|
+
return fieldType;
|
|
281
250
|
}
|
|
282
251
|
function getArgsType(config, type, field) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return "{}";
|
|
286
|
-
}
|
|
287
|
-
const fieldUpper = field[0].toUpperCase() + field.slice(1);
|
|
288
|
-
return `Types.${type}${fieldUpper}Args`;
|
|
252
|
+
if (!(config.fieldInfo.get(type)?.get(field)?.hasArguments ?? false)) return "{}";
|
|
253
|
+
return `Types.${type}${field[0].toUpperCase() + field.slice(1)}Args`;
|
|
289
254
|
}
|
|
290
255
|
function printObjectTypeFieldBuilder(typeName, field) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
return `${field}: Baeta.createFieldBuilder("${typeName}", "${field}", ${getExtensionsVar()})`;
|
|
256
|
+
if (typeName === "Subscription") return `${field}: Baeta.createSubscriptionBuilder("${field}", ${getExtensionsVar()})`;
|
|
257
|
+
return `${field}: Baeta.createFieldBuilder("${typeName}", "${field}", ${getExtensionsVar()})`;
|
|
295
258
|
}
|
|
296
259
|
function getContextType() {
|
|
297
|
-
|
|
260
|
+
return "Ctx";
|
|
298
261
|
}
|
|
299
262
|
function getInfoType() {
|
|
300
|
-
|
|
263
|
+
return "Info";
|
|
301
264
|
}
|
|
302
265
|
function getExtensionsVar() {
|
|
303
|
-
|
|
266
|
+
return "extensions";
|
|
304
267
|
}
|
|
305
268
|
|
|
306
|
-
|
|
307
|
-
|
|
269
|
+
//#endregion
|
|
270
|
+
//#region lib/printer/printer-templates.ts
|
|
308
271
|
function printTypesTemplate(options) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
import type { BaseObjectTypes, BaseScalars } from '${importDir}/utility${options.importExtension}';
|
|
272
|
+
return `import type { GraphQLResolveInfo } from 'graphql';
|
|
273
|
+
import type { BaseObjectTypes, BaseScalars } from '${makeRelativePathForImport(options.modulesDir, options.typesDir)}/utility${options.importExtension}';
|
|
312
274
|
|
|
313
275
|
export interface Scalars extends BaseScalars {}
|
|
314
276
|
|
|
@@ -320,491 +282,437 @@ export type Info = GraphQLResolveInfo;
|
|
|
320
282
|
`;
|
|
321
283
|
}
|
|
322
284
|
function printExtensionsTemplate() {
|
|
323
|
-
|
|
285
|
+
return `import { createExtensions } from '@baeta/core';
|
|
324
286
|
|
|
325
287
|
export default createExtensions({});
|
|
326
288
|
`;
|
|
327
289
|
}
|
|
328
290
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
import {
|
|
332
|
-
Kind
|
|
333
|
-
} from "graphql";
|
|
334
|
-
|
|
335
|
-
// utils/scalar.ts
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region utils/scalar.ts
|
|
336
293
|
function isScalarType(definitionsMap, defaultScalars, type) {
|
|
337
|
-
|
|
294
|
+
return definitionsMap.scalarTypeMap.has(type.name.value) || defaultScalars.includes(type.name.value);
|
|
338
295
|
}
|
|
339
296
|
|
|
340
|
-
|
|
297
|
+
//#endregion
|
|
298
|
+
//#region lib/printer/printer-types.ts
|
|
341
299
|
function printUtilityTypes() {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
Boolean: "boolean",
|
|
351
|
-
Int: "number",
|
|
352
|
-
Float: "number"
|
|
300
|
+
return ["export type Or<A, B> = void extends A ? B : A;", "export type Maybe<T> = T | null;"].join("\n\n");
|
|
301
|
+
}
|
|
302
|
+
const defaultScalarTypes = {
|
|
303
|
+
ID: "string",
|
|
304
|
+
String: "string",
|
|
305
|
+
Boolean: "boolean",
|
|
306
|
+
Int: "number",
|
|
307
|
+
Float: "number"
|
|
353
308
|
};
|
|
354
309
|
function printBaseScalars(config) {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
lines: [...defaultScalars, ...customScalars]
|
|
366
|
-
});
|
|
310
|
+
const defaultScalars = config.defaultScalars.map((scalar) => {
|
|
311
|
+
const type = defaultScalarTypes[scalar];
|
|
312
|
+
if (type == null) return null;
|
|
313
|
+
return `${scalar}: ${type};`;
|
|
314
|
+
}).filter((el) => el != null);
|
|
315
|
+
const customScalars = [...config.globalDefinitions.scalarTypeMap.values()].map((scalar) => `${scalar.name.value}: unknown;`);
|
|
316
|
+
return buildBlock({
|
|
317
|
+
name: "export type BaseScalars =",
|
|
318
|
+
lines: [...defaultScalars, ...customScalars]
|
|
319
|
+
});
|
|
367
320
|
}
|
|
368
321
|
function printBaseObjectTypes(config) {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
322
|
+
return buildBlock({
|
|
323
|
+
name: "export interface BaseObjectTypes",
|
|
324
|
+
lines: [...config.globalDefinitions.objectTypeMap.values()].map((t) => t.name.value).map((t) => `${t}: unknown;`)
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
function printTypesHeaders(config) {
|
|
328
|
+
const overridesDir = makeRelativePathForImport(config.typesDir, config.modulesDir);
|
|
329
|
+
return [
|
|
330
|
+
`import type * as BaetaUtility from "./utility${config.importExtension}";`,
|
|
331
|
+
`import type * as BaetaOverrides from "${overridesDir}/types${config.importExtension}";`,
|
|
332
|
+
"",
|
|
333
|
+
"export type Scalars = BaetaOverrides.Scalars;"
|
|
334
|
+
].join("\n");
|
|
382
335
|
}
|
|
383
336
|
function printRootTypesFromMap(config, rootTypes, map) {
|
|
384
|
-
|
|
337
|
+
return [...map.values()].filter((type) => rootTypes.includes(type.name.value)).map((type) => printObjectType(config, type)).join("\n\n");
|
|
385
338
|
}
|
|
386
339
|
function printObjectTypeTypesFromMap(config, map) {
|
|
387
|
-
|
|
340
|
+
return [...map.values()].map((type) => printObjectType(config, type));
|
|
388
341
|
}
|
|
389
342
|
function printObjectType(config, type) {
|
|
390
|
-
|
|
391
|
-
|
|
343
|
+
const fields = type.fields?.map((field) => printObjectTypeField(config, field));
|
|
344
|
+
return `export type ${type.name.value} = ${printOrObjectType(type, fields)}`;
|
|
392
345
|
}
|
|
393
346
|
function printObjectTypeField(config, field) {
|
|
394
|
-
|
|
395
|
-
|
|
347
|
+
const optionalMarker = printOptionalMarker(config, field.type);
|
|
348
|
+
return `${field.name.value}${optionalMarker}: ${printType(config, field.type)}`;
|
|
396
349
|
}
|
|
397
350
|
function printEnumTypesFromMap(map) {
|
|
398
|
-
|
|
351
|
+
return [...map.values()].map(printEnumType);
|
|
399
352
|
}
|
|
400
353
|
function printEnumType(type) {
|
|
401
|
-
|
|
402
|
-
|
|
354
|
+
const values = type.values?.map((value) => `'${value.name.value}'`);
|
|
355
|
+
return `export type ${type.name.value} = ${values?.join(" | ") || "never"}`;
|
|
403
356
|
}
|
|
404
357
|
function printInterfaceTypesFromMap(config, map) {
|
|
405
|
-
|
|
358
|
+
return [...map.values()].map((type) => printInterfaceType(config, type));
|
|
406
359
|
}
|
|
407
360
|
function printInterfaceType(config, type) {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
(t) => t.interfaces?.some((i) => i.name.value === type.name.value)
|
|
411
|
-
);
|
|
412
|
-
const types = implementingTypes.map((t) => printNamedTypeForInterface(t.name.value));
|
|
413
|
-
return `export type ${type.name.value} = ${types.join(" | ") || "never"}`;
|
|
361
|
+
const types = Array.from(config.globalDefinitions.objectTypeMap.values()).filter((t) => t.interfaces?.some((i) => i.name.value === type.name.value)).map((t) => printNamedTypeForInterface(t.name.value));
|
|
362
|
+
return `export type ${type.name.value} = ${types.join(" | ") || "never"}`;
|
|
414
363
|
}
|
|
415
364
|
function printNamedTypeForInterface(name) {
|
|
416
|
-
|
|
365
|
+
return `${name} & {__typename: "${name}"}`;
|
|
417
366
|
}
|
|
418
367
|
function printUnionTypesFromMap(config, map) {
|
|
419
|
-
|
|
368
|
+
return [...map.values()].map((type) => printUnionType(config, type));
|
|
420
369
|
}
|
|
421
370
|
function printUnionType(config, type) {
|
|
422
|
-
|
|
423
|
-
|
|
371
|
+
const types = type.types?.map((type$1) => printNamedTypeForUnion(config, type$1));
|
|
372
|
+
return `export type ${type.name.value} = ${types?.join(" | ") || "never"}`;
|
|
424
373
|
}
|
|
425
374
|
function printNamedTypeForUnion(config, type) {
|
|
426
|
-
|
|
375
|
+
return `${printNamedType$1(config, type, false)} & {__typename: "${type.name.value}"}`;
|
|
427
376
|
}
|
|
428
377
|
function printInputObjectTypeTypesFromMap(config, map) {
|
|
429
|
-
|
|
378
|
+
return [...map.values()].map((type) => printInputObjectType(config, type));
|
|
430
379
|
}
|
|
431
380
|
function printInputObjectType(config, type) {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
381
|
+
const fields = type.fields?.map((field) => printInputObjectTypeField(config, field));
|
|
382
|
+
return buildBlock({
|
|
383
|
+
name: `export type ${type.name.value} =`,
|
|
384
|
+
lines: fields ?? []
|
|
385
|
+
});
|
|
437
386
|
}
|
|
438
387
|
function printInputObjectTypeField(config, field) {
|
|
439
|
-
|
|
440
|
-
|
|
388
|
+
const optionalMarker = printOptionalMarker(config, field.type);
|
|
389
|
+
return `${field.name.value}${optionalMarker}: ${printType(config, field.type)}`;
|
|
441
390
|
}
|
|
442
391
|
function printObjectTypeFieldsArgsFromMap(config, map) {
|
|
443
|
-
|
|
392
|
+
return [...map.values()].flatMap((type) => printObjectTypeArgs(config, type));
|
|
444
393
|
}
|
|
445
394
|
function printObjectTypeArgs(config, type) {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
}
|
|
449
|
-
return type.fields.map((field) => printObjectTypeFieldArgs(config, type.name.value, field)).filter(Boolean);
|
|
395
|
+
if (type.fields == null) return [];
|
|
396
|
+
return type.fields.map((field) => printObjectTypeFieldArgs(config, type.name.value, field)).filter(Boolean);
|
|
450
397
|
}
|
|
451
398
|
function printObjectTypeFieldArgs(config, typeName, field) {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
});
|
|
399
|
+
return buildBlock({
|
|
400
|
+
name: `export type ${`${typeName + pascalCase(field.name.value)}Args`} =`,
|
|
401
|
+
lines: field.arguments?.map((arg) => printObjectTypeFieldArg(config, arg)) ?? []
|
|
402
|
+
});
|
|
457
403
|
}
|
|
458
404
|
function printObjectTypeFieldArg(config, arg) {
|
|
459
|
-
|
|
405
|
+
return `${arg.name.value}: ${printType(config, arg.type)}`;
|
|
460
406
|
}
|
|
461
407
|
function printType(config, type, nullable = true) {
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
return printType(config, type.type, false);
|
|
469
|
-
default:
|
|
470
|
-
return type;
|
|
471
|
-
}
|
|
408
|
+
switch (type.kind) {
|
|
409
|
+
case Kind.NAMED_TYPE: return printNamedType$1(config, type, nullable);
|
|
410
|
+
case Kind.LIST_TYPE: return printListType(config, type, nullable);
|
|
411
|
+
case Kind.NON_NULL_TYPE: return printType(config, type.type, false);
|
|
412
|
+
default: return type;
|
|
413
|
+
}
|
|
472
414
|
}
|
|
473
415
|
function printListType(config, type, nullable = true) {
|
|
474
|
-
|
|
416
|
+
return printValue(config, `Array<${printType(config, type.type)}>`, nullable);
|
|
475
417
|
}
|
|
476
|
-
function printNamedType(config, type, nullable = true) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
}
|
|
480
|
-
return printValue(config, type.name.value, nullable);
|
|
418
|
+
function printNamedType$1(config, type, nullable = true) {
|
|
419
|
+
if (isScalarType(config.globalDefinitions, config.defaultScalars, type)) return printScalarType(config, type, nullable);
|
|
420
|
+
return printValue(config, type.name.value, nullable);
|
|
481
421
|
}
|
|
482
422
|
function printScalarType(config, type, nullable = true) {
|
|
483
|
-
|
|
423
|
+
return printValue(config, `Scalars["${type.name.value}"]`, nullable);
|
|
484
424
|
}
|
|
485
425
|
function printValue(config, value, nullable) {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
if (config.withMaybe) {
|
|
490
|
-
return `BaetaUtility.Maybe<${value}>`;
|
|
491
|
-
}
|
|
492
|
-
return `${value} | null`;
|
|
426
|
+
if (!nullable) return value;
|
|
427
|
+
if (config.withMaybe) return `BaetaUtility.Maybe<${value}>`;
|
|
428
|
+
return `${value} | null`;
|
|
493
429
|
}
|
|
494
430
|
function printOptionalMarker(config, type) {
|
|
495
|
-
|
|
431
|
+
return config.withOptional && type.kind !== Kind.NON_NULL_TYPE ? "?" : "";
|
|
496
432
|
}
|
|
497
433
|
function printOrObjectType(type, fields) {
|
|
498
|
-
|
|
499
|
-
${fields?.map(indent(2)).join("\n") ?? ""}
|
|
500
|
-
}>`;
|
|
434
|
+
return `BaetaUtility.Or<BaetaOverrides.ObjectTypes["${type.name.value}"], {\n${fields?.map(indent(2)).join("\n") ?? ""}\n}>`;
|
|
501
435
|
}
|
|
502
436
|
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
visit
|
|
506
|
-
} from "graphql";
|
|
437
|
+
//#endregion
|
|
438
|
+
//#region lib/visitors/definitions-map.ts
|
|
507
439
|
function createRegistry() {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
440
|
+
return {
|
|
441
|
+
scalarTypeMap: /* @__PURE__ */ new Map(),
|
|
442
|
+
enumTypeMap: /* @__PURE__ */ new Map(),
|
|
443
|
+
objectTypeMap: /* @__PURE__ */ new Map(),
|
|
444
|
+
inputObjectTypeMap: /* @__PURE__ */ new Map(),
|
|
445
|
+
unionTypeMap: /* @__PURE__ */ new Map(),
|
|
446
|
+
interfaceTypeMap: /* @__PURE__ */ new Map()
|
|
447
|
+
};
|
|
516
448
|
}
|
|
517
449
|
function createDefinitionsMapFromSources(sources) {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
450
|
+
const registry = createRegistry();
|
|
451
|
+
for (const source of sources) if (source.document) collectTypesFromDocument(source.document, registry, [
|
|
452
|
+
"Query",
|
|
453
|
+
"Mutation",
|
|
454
|
+
"Subscription"
|
|
455
|
+
]);
|
|
456
|
+
return registry;
|
|
525
457
|
}
|
|
526
458
|
function createDefinitionsMapFromDocument(document) {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
459
|
+
const registry = createRegistry();
|
|
460
|
+
collectTypesFromDocument(document, registry);
|
|
461
|
+
return registry;
|
|
530
462
|
}
|
|
531
463
|
function collectTypesFromDocument(document, registry, filterObjectTypes = []) {
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
},
|
|
560
|
-
UnionTypeDefinition(node) {
|
|
561
|
-
if (registry.unionTypeMap.has(node.name.value)) {
|
|
562
|
-
throw new Error(`Union type ${node.name.value} already exists, use 'extend' instead!`);
|
|
563
|
-
}
|
|
564
|
-
registry.unionTypeMap.set(node.name.value, node);
|
|
565
|
-
},
|
|
566
|
-
InterfaceTypeDefinition(node) {
|
|
567
|
-
if (registry.interfaceTypeMap.has(node.name.value)) {
|
|
568
|
-
throw new Error(`Interface type ${node.name.value} already exists. Use 'extend' instead!`);
|
|
569
|
-
}
|
|
570
|
-
registry.interfaceTypeMap.set(node.name.value, node);
|
|
571
|
-
}
|
|
572
|
-
});
|
|
464
|
+
visit(document, {
|
|
465
|
+
ScalarTypeDefinition(node) {
|
|
466
|
+
if (registry.scalarTypeMap.has(node.name.value)) throw new Error(`Scalar type ${node.name.value} already exists`);
|
|
467
|
+
registry.scalarTypeMap.set(node.name.value, node);
|
|
468
|
+
},
|
|
469
|
+
EnumTypeDefinition(node) {
|
|
470
|
+
if (registry.enumTypeMap.has(node.name.value)) throw new Error(`Enum type ${node.name.value} already exists, use 'extend' instead!`);
|
|
471
|
+
registry.enumTypeMap.set(node.name.value, node);
|
|
472
|
+
},
|
|
473
|
+
ObjectTypeDefinition(node) {
|
|
474
|
+
if (filterObjectTypes.includes(node.name.value)) return;
|
|
475
|
+
if (registry.objectTypeMap.has(node.name.value)) throw new Error(`Object type ${node.name.value} already exists, use 'extend' instead!`);
|
|
476
|
+
registry.objectTypeMap.set(node.name.value, node);
|
|
477
|
+
},
|
|
478
|
+
InputObjectTypeDefinition(node) {
|
|
479
|
+
if (registry.inputObjectTypeMap.has(node.name.value)) throw new Error(`Input type ${node.name.value} already exists. Use 'extend' instead!`);
|
|
480
|
+
registry.inputObjectTypeMap.set(node.name.value, node);
|
|
481
|
+
},
|
|
482
|
+
UnionTypeDefinition(node) {
|
|
483
|
+
if (registry.unionTypeMap.has(node.name.value)) throw new Error(`Union type ${node.name.value} already exists, use 'extend' instead!`);
|
|
484
|
+
registry.unionTypeMap.set(node.name.value, node);
|
|
485
|
+
},
|
|
486
|
+
InterfaceTypeDefinition(node) {
|
|
487
|
+
if (registry.interfaceTypeMap.has(node.name.value)) throw new Error(`Interface type ${node.name.value} already exists. Use 'extend' instead!`);
|
|
488
|
+
registry.interfaceTypeMap.set(node.name.value, node);
|
|
489
|
+
}
|
|
490
|
+
});
|
|
573
491
|
}
|
|
574
492
|
|
|
575
|
-
|
|
576
|
-
|
|
493
|
+
//#endregion
|
|
494
|
+
//#region lib/visitors/field-info.ts
|
|
577
495
|
function createFieldInfoMap(definitionsMap, defaultScalars) {
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
return type;
|
|
606
|
-
}
|
|
496
|
+
const map = /* @__PURE__ */ new Map();
|
|
497
|
+
for (const objectType of definitionsMap.objectTypeMap.values()) {
|
|
498
|
+
const objectName = objectType.name.value;
|
|
499
|
+
for (const field of objectType.fields ?? []) {
|
|
500
|
+
const fieldName = field.name.value;
|
|
501
|
+
const type = printNamedType(definitionsMap, defaultScalars, field.type, true);
|
|
502
|
+
const hasArguments = field.arguments != null && field.arguments.length > 0;
|
|
503
|
+
const fieldMap = map.get(objectName) ?? /* @__PURE__ */ new Map();
|
|
504
|
+
fieldMap.set(fieldName, {
|
|
505
|
+
type,
|
|
506
|
+
hasArguments
|
|
507
|
+
});
|
|
508
|
+
map.set(objectName, fieldMap);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return map;
|
|
512
|
+
}
|
|
513
|
+
function printNamedType(definitionsMap, defaultScalars, type, nullable = true) {
|
|
514
|
+
const withNullable = nullable ? " | null" : "";
|
|
515
|
+
switch (type.kind) {
|
|
516
|
+
case Kind.NAMED_TYPE:
|
|
517
|
+
if (isScalarType(definitionsMap, defaultScalars, type)) return `Types.Scalars["${type.name.value}"]${withNullable}`;
|
|
518
|
+
return `Types.${type.name.value}${withNullable}`;
|
|
519
|
+
case Kind.LIST_TYPE: return `Array<${printNamedType(definitionsMap, defaultScalars, type.type, nullable)}>${withNullable}`;
|
|
520
|
+
case Kind.NON_NULL_TYPE: return printNamedType(definitionsMap, defaultScalars, type.type, false);
|
|
521
|
+
default: return type;
|
|
522
|
+
}
|
|
607
523
|
}
|
|
608
524
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
525
|
+
//#endregion
|
|
526
|
+
//#region lib/visitors/module-registry.ts
|
|
527
|
+
const registryKeys = [
|
|
528
|
+
"objects",
|
|
529
|
+
"interfaces",
|
|
530
|
+
"unions",
|
|
531
|
+
"scalars"
|
|
532
|
+
];
|
|
614
533
|
function createModuleRegistry(document) {
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
defined,
|
|
643
|
-
picks
|
|
644
|
-
};
|
|
534
|
+
const picks = createObject(registryKeys, () => ({}));
|
|
535
|
+
const defined = createObject(registryKeys, () => []);
|
|
536
|
+
visit(document, {
|
|
537
|
+
ObjectTypeDefinition(node) {
|
|
538
|
+
defined.objects.push(node.name.value);
|
|
539
|
+
collectFields(node, picks.objects);
|
|
540
|
+
},
|
|
541
|
+
ObjectTypeExtension(node) {
|
|
542
|
+
pushUnique(defined.objects, node.name.value);
|
|
543
|
+
collectFields(node, picks.objects);
|
|
544
|
+
},
|
|
545
|
+
InterfaceTypeDefinition(node) {
|
|
546
|
+
defined.interfaces.push(node.name.value);
|
|
547
|
+
collectFields(node, picks.interfaces);
|
|
548
|
+
},
|
|
549
|
+
UnionTypeDefinition(node) {
|
|
550
|
+
defined.unions.push(node.name.value);
|
|
551
|
+
collectUnionTypes(node, picks);
|
|
552
|
+
},
|
|
553
|
+
ScalarTypeDefinition(node) {
|
|
554
|
+
defined.scalars.push(node.name.value);
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
return {
|
|
558
|
+
defined,
|
|
559
|
+
picks
|
|
560
|
+
};
|
|
645
561
|
}
|
|
646
562
|
function collectFields(node, picksObj) {
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
for (const field of node.fields) {
|
|
653
|
-
picksObj[name].push(field.name.value);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
563
|
+
const name = node.name.value;
|
|
564
|
+
if (node.fields) {
|
|
565
|
+
if (!picksObj[name]) picksObj[name] = [];
|
|
566
|
+
for (const field of node.fields) picksObj[name].push(field.name.value);
|
|
567
|
+
}
|
|
656
568
|
}
|
|
657
569
|
function collectUnionTypes(node, picks) {
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
for (const type of node.types) {
|
|
664
|
-
picks.unions[name].push(type.name.value);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
570
|
+
const name = node.name.value;
|
|
571
|
+
if (node.types) {
|
|
572
|
+
if (!picks.unions[name]) picks.unions[name] = [];
|
|
573
|
+
for (const type of node.types) picks.unions[name].push(type.name.value);
|
|
574
|
+
}
|
|
667
575
|
}
|
|
668
576
|
function pushUnique(list, item) {
|
|
669
|
-
|
|
670
|
-
list.push(item);
|
|
671
|
-
}
|
|
577
|
+
if (!list.includes(item)) list.push(item);
|
|
672
578
|
}
|
|
673
579
|
function createObject(keys, valueFn) {
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
}
|
|
678
|
-
return obj;
|
|
580
|
+
const obj = {};
|
|
581
|
+
for (const key of keys) obj[key] = valueFn(key);
|
|
582
|
+
return obj;
|
|
679
583
|
}
|
|
680
584
|
|
|
681
|
-
|
|
585
|
+
//#endregion
|
|
586
|
+
//#region lib/codegen.ts
|
|
682
587
|
async function generate(options) {
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
588
|
+
const { outputSchema, outputSchemaAst } = await loadSchema$1(options.schemas, options.cwd, options.loaders);
|
|
589
|
+
const sources = getSourcesFromSchema(outputSchemaAst);
|
|
590
|
+
const sourcesByModule = groupSourcesByModule(sources, options.modulesDir);
|
|
591
|
+
const modules = Array.from(sourcesByModule.keys());
|
|
592
|
+
const globalDefinitions = createDefinitionsMapFromDocument(outputSchema);
|
|
593
|
+
const modulesDefinitions = createDefinitionsMapFromSources(sources);
|
|
594
|
+
const defaultScalars = [
|
|
595
|
+
"ID",
|
|
596
|
+
"Int",
|
|
597
|
+
"Float",
|
|
598
|
+
"String",
|
|
599
|
+
"Boolean"
|
|
600
|
+
];
|
|
601
|
+
const config = {
|
|
602
|
+
globalDefinitions,
|
|
603
|
+
withMaybe: false,
|
|
604
|
+
withOptional: false,
|
|
605
|
+
defaultScalars,
|
|
606
|
+
importExtension: options.importExtension,
|
|
607
|
+
typesDir: options.typesDir,
|
|
608
|
+
modulesDir: options.modulesDir
|
|
609
|
+
};
|
|
610
|
+
const fieldInfo = createFieldInfoMap(globalDefinitions, defaultScalars);
|
|
611
|
+
const typesContent = [
|
|
612
|
+
printTypesHeaders(config),
|
|
613
|
+
printRootTypesFromMap(config, [
|
|
614
|
+
"Query",
|
|
615
|
+
"Mutation",
|
|
616
|
+
"Subscription"
|
|
617
|
+
], globalDefinitions.objectTypeMap),
|
|
618
|
+
...printEnumTypesFromMap(globalDefinitions.enumTypeMap),
|
|
619
|
+
...printObjectTypeTypesFromMap(config, modulesDefinitions.objectTypeMap),
|
|
620
|
+
...printObjectTypeFieldsArgsFromMap(config, globalDefinitions.objectTypeMap),
|
|
621
|
+
...printInputObjectTypeTypesFromMap(config, globalDefinitions.inputObjectTypeMap),
|
|
622
|
+
...printInterfaceTypesFromMap(config, globalDefinitions.interfaceTypeMap),
|
|
623
|
+
...printUnionTypesFromMap(config, globalDefinitions.unionTypeMap)
|
|
624
|
+
].join("\n\n");
|
|
625
|
+
const utilityContent = [
|
|
626
|
+
printUtilityTypes(),
|
|
627
|
+
printBaseScalars(config),
|
|
628
|
+
printBaseObjectTypes(config)
|
|
629
|
+
].join("\n\n");
|
|
630
|
+
const files = [
|
|
631
|
+
{
|
|
632
|
+
filename: join(options.typesDir, "types.ts"),
|
|
633
|
+
content: typesContent
|
|
634
|
+
},
|
|
635
|
+
{
|
|
636
|
+
filename: join(options.typesDir, "utility.ts"),
|
|
637
|
+
content: utilityContent
|
|
638
|
+
},
|
|
639
|
+
{
|
|
640
|
+
filename: join(options.modulesDir, "index.ts"),
|
|
641
|
+
content: printAutoload({ importExtension: options.importExtension }, modules)
|
|
642
|
+
},
|
|
643
|
+
{
|
|
644
|
+
filename: join(options.modulesDir, "types.ts"),
|
|
645
|
+
content: printTypesTemplate({
|
|
646
|
+
importExtension: options.importExtension,
|
|
647
|
+
typesDir: options.typesDir,
|
|
648
|
+
modulesDir: options.modulesDir
|
|
649
|
+
}),
|
|
650
|
+
options: {
|
|
651
|
+
disableOverwrite: true,
|
|
652
|
+
disableBiomeV1Header: true,
|
|
653
|
+
disableBiomeV2Header: true,
|
|
654
|
+
disableEslintHeader: true,
|
|
655
|
+
disableGenerationNoticeHeader: true
|
|
656
|
+
}
|
|
657
|
+
},
|
|
658
|
+
{
|
|
659
|
+
filename: join(options.modulesDir, "extensions.ts"),
|
|
660
|
+
content: printExtensionsTemplate(),
|
|
661
|
+
options: {
|
|
662
|
+
disableOverwrite: true,
|
|
663
|
+
disableBiomeV1Header: true,
|
|
664
|
+
disableBiomeV2Header: true,
|
|
665
|
+
disableEslintHeader: true,
|
|
666
|
+
disableGenerationNoticeHeader: true
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
];
|
|
670
|
+
for (const module of modules) {
|
|
671
|
+
const documents = sourcesByModule.get(module)?.map((s) => s.document).filter((el) => el != null) ?? [];
|
|
672
|
+
if (documents.length === 0) continue;
|
|
673
|
+
const document = concatAST(documents);
|
|
674
|
+
const config$1 = {
|
|
675
|
+
typesDir: options.typesDir,
|
|
676
|
+
fieldInfo,
|
|
677
|
+
importExtension: options.importExtension,
|
|
678
|
+
modulesDir: options.modulesDir,
|
|
679
|
+
registry: createModuleRegistry(document)
|
|
680
|
+
};
|
|
681
|
+
files.push({
|
|
682
|
+
filename: join(options.modulesDir, `/${module}/${options.moduleDefinitionName}`),
|
|
683
|
+
content: [
|
|
684
|
+
printModuleImports(config$1, module),
|
|
685
|
+
printModuleMetadata(module, document),
|
|
686
|
+
printBaetaModuleTypes(config$1),
|
|
687
|
+
printModuleObjectTypeFields(config$1),
|
|
688
|
+
printModuleBuilder(config$1, module)
|
|
689
|
+
].join("\n\n")
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
return files;
|
|
782
693
|
}
|
|
783
694
|
|
|
784
|
-
|
|
695
|
+
//#endregion
|
|
696
|
+
//#region index.ts
|
|
785
697
|
function graphqlPlugin() {
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
}
|
|
807
|
-
export {
|
|
808
|
-
graphqlPlugin
|
|
809
|
-
};
|
|
698
|
+
return createPluginV1({
|
|
699
|
+
name: "graphql",
|
|
700
|
+
actionName: "GraphQL modules",
|
|
701
|
+
watch: (options, watcher, reload) => {
|
|
702
|
+
const handleChange = (file) => {
|
|
703
|
+
if (isMatch(file.relativePath, options.schemas)) reload(file);
|
|
704
|
+
};
|
|
705
|
+
watcher.on("update", handleChange);
|
|
706
|
+
watcher.on("delete", handleChange);
|
|
707
|
+
},
|
|
708
|
+
generate: async (ctx, next) => {
|
|
709
|
+
const items = await generate(ctx.generatorOptions);
|
|
710
|
+
for (const item of items) ctx.fileManager.createAndAdd(item.filename, item.content, "graphql", item.options);
|
|
711
|
+
return next();
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
//#endregion
|
|
717
|
+
export { graphqlPlugin };
|
|
810
718
|
//# sourceMappingURL=index.js.map
|