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