@baeta/plugin-graphql 1.0.11 → 2.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/dist/index.js +675 -1010
- package/dist/index.js.map +1 -1
- package/package.json +10 -18
- package/dist/index.d.ts +0 -5
package/dist/index.js
CHANGED
|
@@ -1,53 +1,24 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
6
|
-
|
|
7
1
|
// index.ts
|
|
8
2
|
import { createPluginV1, isMatch } from "@baeta/generator-sdk";
|
|
9
3
|
|
|
10
4
|
// lib/codegen.ts
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import * as typescriptPlugin from "@graphql-codegen/typescript";
|
|
14
|
-
|
|
15
|
-
// utils/cache.ts
|
|
16
|
-
function createCache() {
|
|
17
|
-
const cache = /* @__PURE__ */ new Map();
|
|
18
|
-
return function ensure(namespace, key, factory) {
|
|
19
|
-
const cacheKey = `${namespace}:${key}`;
|
|
20
|
-
const cachedValue = cache.get(cacheKey);
|
|
21
|
-
if (cachedValue) {
|
|
22
|
-
return cachedValue;
|
|
23
|
-
}
|
|
24
|
-
const value = factory();
|
|
25
|
-
cache.set(cacheKey, value);
|
|
26
|
-
return value;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
5
|
+
import { join as join2 } from "@baeta/util-path";
|
|
6
|
+
import { concatAST } from "graphql";
|
|
29
7
|
|
|
30
8
|
// utils/load.ts
|
|
31
|
-
import { getCachedDocumentNodeFromSchema as getCachedDocumentNodeFromSchema2 } from "@graphql-codegen/plugin-helpers";
|
|
32
9
|
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
|
|
33
10
|
import {
|
|
34
11
|
loadSchema as loadSchemaToolkit
|
|
35
12
|
} from "@graphql-tools/load";
|
|
13
|
+
import {
|
|
14
|
+
getDocumentNodeFromSchema
|
|
15
|
+
} from "@graphql-tools/utils";
|
|
36
16
|
import { validateSchema } from "graphql";
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
function hashContent(content) {
|
|
43
|
-
return createHash("sha256").update(content).digest("hex");
|
|
44
|
-
}
|
|
45
|
-
function hashSchema(schema) {
|
|
46
|
-
return hashContent(print(getCachedDocumentNodeFromSchema(schema)));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// utils/load.ts
|
|
50
|
-
async function loadSchema(schemaPointerMap, cwd, extraLoaders = []) {
|
|
17
|
+
async function loadSchema(schemas, cwd, extraLoaders = []) {
|
|
18
|
+
const schemaPointerMap = {};
|
|
19
|
+
for (const ptr of Array.isArray(schemas) ? schemas : [schemas]) {
|
|
20
|
+
schemaPointerMap[ptr] = {};
|
|
21
|
+
}
|
|
51
22
|
const outputSchemaAst = await loadSchemaToolkit(schemaPointerMap, {
|
|
52
23
|
loaders: [new GraphQLFileLoader(), ...extraLoaders],
|
|
53
24
|
cwd,
|
|
@@ -64,1056 +35,750 @@ ${messages}`);
|
|
|
64
35
|
if (!outputSchemaAst.extensions) {
|
|
65
36
|
outputSchemaAst.extensions = {};
|
|
66
37
|
}
|
|
67
|
-
outputSchemaAst.extensions.hash = hashSchema(outputSchemaAst);
|
|
68
38
|
return {
|
|
69
39
|
outputSchemaAst,
|
|
70
|
-
outputSchema:
|
|
40
|
+
outputSchema: getDocumentNodeFromSchema(outputSchemaAst)
|
|
71
41
|
};
|
|
72
42
|
}
|
|
73
43
|
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
44
|
+
// utils/path.ts
|
|
45
|
+
import path, { normalize } from "@baeta/util-path";
|
|
46
|
+
var sep = "/";
|
|
47
|
+
function getRelativePath(filepath, basePath) {
|
|
48
|
+
const normalizedFilepath = normalize(filepath);
|
|
49
|
+
const normalizedBasePath = ensureStartsWithSeparator(
|
|
50
|
+
ensureEndsWithSeparator(normalize(basePath))
|
|
51
|
+
);
|
|
52
|
+
const [, relativePath] = normalizedFilepath.split(normalizedBasePath);
|
|
53
|
+
return relativePath;
|
|
54
|
+
}
|
|
55
|
+
function extractModuleDirectory(relativePath) {
|
|
56
|
+
const [moduleDirectory] = relativePath.split(sep);
|
|
57
|
+
return moduleDirectory;
|
|
58
|
+
}
|
|
59
|
+
function ensureStartsWithSeparator(path2) {
|
|
60
|
+
return path2.startsWith(sep) ? path2 : sep + path2;
|
|
61
|
+
}
|
|
62
|
+
function ensureEndsWithSeparator(path2) {
|
|
63
|
+
return path2.endsWith(sep) ? path2 : path2 + sep;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// utils/source.ts
|
|
67
|
+
function groupSourcesByModule(sources, basePath) {
|
|
68
|
+
const map = /* @__PURE__ */ new Map();
|
|
69
|
+
for (const source of sources) {
|
|
70
|
+
if (!source.location) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const relativePath = getRelativePath(source.location, basePath);
|
|
74
|
+
if (!relativePath) {
|
|
75
|
+
continue;
|
|
98
76
|
}
|
|
99
|
-
|
|
100
|
-
|
|
77
|
+
const mod = extractModuleDirectory(relativePath);
|
|
78
|
+
const existing = map.get(mod) ?? [];
|
|
79
|
+
existing.push(source);
|
|
80
|
+
map.set(mod, existing);
|
|
101
81
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
var context_default = { plugin };
|
|
82
|
+
return map;
|
|
83
|
+
}
|
|
84
|
+
function getSourcesFromSchema(schema) {
|
|
85
|
+
const extensions = schema.extensions;
|
|
86
|
+
return extensions?.extendedSources ?? [];
|
|
87
|
+
}
|
|
109
88
|
|
|
110
|
-
// lib/
|
|
111
|
-
import {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
89
|
+
// lib/printer/printer-autoload.ts
|
|
90
|
+
import { camelCase } from "change-case-all";
|
|
91
|
+
function printAutoload(config, modules) {
|
|
92
|
+
return [printImports(config, modules), printExport(modules)].join("\n\n");
|
|
93
|
+
}
|
|
94
|
+
function printImports(config, modules) {
|
|
95
|
+
return modules.map(
|
|
96
|
+
(module) => `import ${camelCase(module)} from "./${module}/index${config.importExtension}"`
|
|
97
|
+
).join("\n");
|
|
98
|
+
}
|
|
99
|
+
function printExport(modules) {
|
|
100
|
+
return `export default [${modules.map((m) => camelCase(m)).join(", ")}];`;
|
|
101
|
+
}
|
|
117
102
|
|
|
118
|
-
// lib/
|
|
103
|
+
// lib/printer/printer-module.ts
|
|
104
|
+
import { join, relative } from "@baeta/util-path";
|
|
119
105
|
import { pascalCase } from "change-case-all";
|
|
120
|
-
import {
|
|
121
|
-
isInterfaceType,
|
|
122
|
-
isScalarType,
|
|
123
|
-
isUnionType,
|
|
124
|
-
Kind as Kind3,
|
|
125
|
-
visit
|
|
126
|
-
} from "graphql";
|
|
127
106
|
|
|
128
|
-
// lib/
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
} from "graphql";
|
|
132
|
-
import hash from "murmurhash";
|
|
133
|
-
function getObjectTypeHash(node) {
|
|
134
|
-
const fieldNames = buildFieldsNames(node.fields);
|
|
135
|
-
const typeFields = fieldNames.map(([fieldName, fieldType]) => `${fieldName}:${fieldType}`).join(",");
|
|
136
|
-
const fieldMap = {};
|
|
137
|
-
for (const [fieldName, fieldType] of fieldNames) {
|
|
138
|
-
fieldMap[fieldName] = hash.v3(fieldType).toString(36);
|
|
139
|
-
}
|
|
140
|
-
return {
|
|
141
|
-
hash: hash.v3(typeFields).toString(36),
|
|
142
|
-
fieldsHashes: fieldMap
|
|
143
|
-
};
|
|
107
|
+
// lib/printer/printer-utils.ts
|
|
108
|
+
function buildBlock({ name, lines }) {
|
|
109
|
+
return [`${name} {`, ...lines.filter(Boolean).map(indent(2)), "};"].join("\n");
|
|
144
110
|
}
|
|
145
|
-
function
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
const fieldsNames = [];
|
|
150
|
-
for (const field of fields) {
|
|
151
|
-
const fieldName = field.name.value;
|
|
152
|
-
const fieldType = buildTypeName(field.type);
|
|
153
|
-
fieldsNames.push([fieldName, fieldType]);
|
|
154
|
-
}
|
|
155
|
-
return fieldsNames.sort(([a], [b]) => a.localeCompare(b));
|
|
111
|
+
function buildCodeBlock({ name, lines }) {
|
|
112
|
+
const linesWithSep = lines.filter(Boolean).map(indent(2)).join(",\n");
|
|
113
|
+
return [`${name} {`, linesWithSep, "}"].join("\n");
|
|
156
114
|
}
|
|
157
|
-
function
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (node.kind === Kind.LIST_TYPE) {
|
|
162
|
-
return `[${buildTypeName(node.type)}]`;
|
|
163
|
-
}
|
|
164
|
-
if (node.kind === Kind.NON_NULL_TYPE) {
|
|
165
|
-
return `${buildTypeName(node.type)}!`;
|
|
115
|
+
function indent(size) {
|
|
116
|
+
const space = new Array(size).fill(" ").join("");
|
|
117
|
+
function indentInner(val) {
|
|
118
|
+
return val.split("\n").map((line) => `${space}${line}`).join("\n");
|
|
166
119
|
}
|
|
167
|
-
return
|
|
120
|
+
return indentInner;
|
|
168
121
|
}
|
|
169
122
|
|
|
170
|
-
// lib/
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
import {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
import
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
123
|
+
// lib/printer/printer-module.ts
|
|
124
|
+
function printModuleImports(config, moduleName) {
|
|
125
|
+
const typesDir = relative(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 * as Types from "${typesDir}/types${config.importExtension}";`
|
|
132
|
+
].join("\n");
|
|
133
|
+
}
|
|
134
|
+
function printModuleMetadata(name, doc) {
|
|
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
|
+
}
|
|
144
|
+
function printBaetaModuleTypes(config) {
|
|
145
|
+
return buildBlock({
|
|
146
|
+
name: "interface BaetaModuleTypes",
|
|
147
|
+
lines: [
|
|
148
|
+
buildBlock({
|
|
149
|
+
name: "Builders:",
|
|
150
|
+
lines: printBaetaModuleTypesForFields(config, false)
|
|
151
|
+
}),
|
|
152
|
+
buildBlock({
|
|
153
|
+
name: "Factories:",
|
|
154
|
+
lines: [
|
|
155
|
+
...printBaetaModuleTypesForFields(config, true),
|
|
156
|
+
...printBaetaModuleTypesScalars(config)
|
|
157
|
+
]
|
|
158
|
+
})
|
|
159
|
+
]
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
function printBaetaModuleTypesForFields(config, isFactory) {
|
|
163
|
+
return config.registry.defined.objects.map(
|
|
164
|
+
(typeName) => printObjectTypeModuleType(typeName, config.registry.picks.objects, isFactory)
|
|
165
|
+
).filter(Boolean);
|
|
166
|
+
}
|
|
167
|
+
function printObjectTypeModuleType(typeName, objects, isFactory) {
|
|
168
|
+
const object = objects[typeName];
|
|
169
|
+
if (!object) {
|
|
170
|
+
return "";
|
|
186
171
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
findRelated(field);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (node.interfaces) {
|
|
196
|
-
for (const _interface of node.interfaces) {
|
|
197
|
-
findRelated(_interface);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
} else if (node.kind === Kind2.INPUT_OBJECT_TYPE_DEFINITION || node.kind === Kind2.INPUT_OBJECT_TYPE_EXTENSION) {
|
|
201
|
-
markAsUsed(node.name.value);
|
|
202
|
-
if (node.fields) {
|
|
203
|
-
for (const field of node.fields) {
|
|
204
|
-
findRelated(field);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
} else if (node.kind === Kind2.INTERFACE_TYPE_DEFINITION || node.kind === Kind2.INTERFACE_TYPE_EXTENSION) {
|
|
208
|
-
markAsUsed(node.name.value);
|
|
209
|
-
if (node.fields) {
|
|
210
|
-
for (const field of node.fields) {
|
|
211
|
-
findRelated(field);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (node.interfaces) {
|
|
215
|
-
for (const _interface of node.interfaces) {
|
|
216
|
-
findRelated(_interface);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
} else if (node.kind === Kind2.UNION_TYPE_DEFINITION || node.kind === Kind2.UNION_TYPE_EXTENSION) {
|
|
220
|
-
markAsUsed(node.name.value);
|
|
221
|
-
if (node.types) {
|
|
222
|
-
for (const type of node.types) {
|
|
223
|
-
findRelated(type);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
} else if (node.kind === Kind2.ENUM_TYPE_DEFINITION || node.kind === Kind2.ENUM_TYPE_EXTENSION) {
|
|
227
|
-
markAsUsed(node.name.value);
|
|
228
|
-
} else if (node.kind === Kind2.SCALAR_TYPE_DEFINITION || node.kind === Kind2.SCALAR_TYPE_EXTENSION) {
|
|
229
|
-
if (!isGraphQLPrimitive(node.name.value)) {
|
|
230
|
-
markAsUsed(node.name.value);
|
|
231
|
-
}
|
|
232
|
-
} else if (node.kind === Kind2.INPUT_VALUE_DEFINITION) {
|
|
233
|
-
findRelated(resolveTypeNode(node.type));
|
|
234
|
-
} else if (node.kind === Kind2.FIELD_DEFINITION) {
|
|
235
|
-
findRelated(resolveTypeNode(node.type));
|
|
236
|
-
if (node.arguments) {
|
|
237
|
-
for (const argument of node.arguments) {
|
|
238
|
-
findRelated(argument);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
} else if (node.kind === Kind2.NAMED_TYPE && // Named type
|
|
242
|
-
!isGraphQLPrimitive(node.name.value)) {
|
|
243
|
-
markAsUsed(node.name.value);
|
|
244
|
-
}
|
|
172
|
+
const parentType = getParentType(typeName);
|
|
173
|
+
const contextType = getContextType();
|
|
174
|
+
const infoType = getInfoType();
|
|
175
|
+
if (isFactory) {
|
|
176
|
+
return `${typeName}: Baeta.TypeCompilerFactory<${parentType}, ${contextType}, ${infoType}, BaetaModuleObjectTypeFields['${typeName}']['Factory']>`;
|
|
245
177
|
}
|
|
246
|
-
return
|
|
247
|
-
}
|
|
248
|
-
function
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
wrapOptional: (str) => {
|
|
257
|
-
return `Types.Maybe<${str}>`;
|
|
258
|
-
},
|
|
259
|
-
wrapArray: (str) => {
|
|
260
|
-
return `Array<${str}>`;
|
|
261
|
-
}
|
|
178
|
+
return `${typeName}: Baeta.TypeMethods<${parentType}, ${contextType}, ${infoType}, BaetaModuleObjectTypeFields['${typeName}']['Builder'], BaetaModuleObjectTypeFields['${typeName}']['Factory']>`;
|
|
179
|
+
}
|
|
180
|
+
function printBaetaModuleTypesScalars(config) {
|
|
181
|
+
return config.registry.defined.scalars.map((scalar) => `${scalar}: GraphQLScalarType`);
|
|
182
|
+
}
|
|
183
|
+
function printModuleObjectTypeFields(config) {
|
|
184
|
+
const objects = config.registry.defined.objects.map((typeName) => printObjectTypeFields(config, typeName, config.registry.picks.objects)).filter(Boolean);
|
|
185
|
+
return buildBlock({
|
|
186
|
+
name: "interface BaetaModuleObjectTypeFields",
|
|
187
|
+
lines: objects
|
|
262
188
|
});
|
|
263
|
-
|
|
264
|
-
|
|
189
|
+
}
|
|
190
|
+
function printObjectTypeFields(config, typeName, objects) {
|
|
191
|
+
const fields = objects[typeName];
|
|
192
|
+
if (!fields || fields.length === 0) {
|
|
193
|
+
return "";
|
|
265
194
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
195
|
+
const fieldsBuilders = fields.map(
|
|
196
|
+
(field) => printObjectTypeFieldBuilders(config, typeName, field)
|
|
197
|
+
);
|
|
198
|
+
const fieldsFactories = fields.map(
|
|
199
|
+
(field) => printObjectTypeFieldFactories(config, typeName, field)
|
|
200
|
+
);
|
|
201
|
+
return buildBlock({
|
|
202
|
+
name: `${typeName}:`,
|
|
203
|
+
lines: [
|
|
204
|
+
buildBlock({
|
|
205
|
+
name: "Builder:",
|
|
206
|
+
lines: fieldsBuilders
|
|
207
|
+
}),
|
|
208
|
+
buildBlock({
|
|
209
|
+
name: "Factory:",
|
|
210
|
+
lines: fieldsFactories
|
|
211
|
+
})
|
|
212
|
+
]
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
function printObjectTypeFieldBuilders(config, typeName, field) {
|
|
216
|
+
const parentType = getParentType(typeName);
|
|
217
|
+
const resultType = getResultType(config, typeName, field);
|
|
218
|
+
const argumentsType = getArgsType(config, typeName, field);
|
|
219
|
+
const contextType = getContextType();
|
|
220
|
+
const infoType = getInfoType();
|
|
221
|
+
const namespace = typeName === "Subscription" ? "SubscriptionMethods" : "FieldMethods";
|
|
222
|
+
return `${field}: Baeta.${namespace}<${resultType}, ${parentType}, ${contextType}, ${argumentsType}, ${infoType}>`;
|
|
223
|
+
}
|
|
224
|
+
function printObjectTypeFieldFactories(config, typeName, field) {
|
|
225
|
+
const parentType = getParentType(typeName);
|
|
226
|
+
const resultType = getResultType(config, typeName, field);
|
|
227
|
+
const argumentsType = getArgsType(config, typeName, field);
|
|
228
|
+
const contextType = getContextType();
|
|
229
|
+
const infoType = getInfoType();
|
|
230
|
+
const namespace = typeName === "Subscription" ? "SubscriptionField" : "Field";
|
|
231
|
+
return `${field}: Baeta.${namespace}<${resultType}, ${resultType}, ${parentType}, ${contextType}, ${argumentsType}, ${infoType}>`;
|
|
232
|
+
}
|
|
233
|
+
function printModuleBuilder(config, moduleName) {
|
|
234
|
+
const objectTypes = config.registry.defined.objects.map((typeName) => printObjectTypeBuilder(typeName, config.registry.picks.objects)).filter(Boolean);
|
|
235
|
+
const builders = buildCodeBlock({
|
|
236
|
+
name: "",
|
|
237
|
+
lines: objectTypes
|
|
238
|
+
});
|
|
239
|
+
const typeNameResolvers = buildCodeBlock({
|
|
240
|
+
name: "",
|
|
241
|
+
lines: [...config.registry.defined.unions, ...config.registry.defined.interfaces].map(
|
|
242
|
+
(name) => `${name}: ${printTypeNameResolver()}`
|
|
243
|
+
)
|
|
244
|
+
});
|
|
245
|
+
const infoType = getInfoType();
|
|
246
|
+
const contextType = getContextType();
|
|
247
|
+
return [
|
|
248
|
+
`export const ${pascalCase(moduleName)}Module = Baeta.createModuleBuilder<${contextType}, ${infoType}, BaetaModuleTypes['Builders'], BaetaModuleTypes['Factories']>(moduleMetadata.id, moduleMetadata.typedef,`,
|
|
249
|
+
builders,
|
|
250
|
+
",",
|
|
251
|
+
typeNameResolvers,
|
|
252
|
+
`, ${getExtensionsVar()});`
|
|
253
|
+
].join("");
|
|
254
|
+
}
|
|
255
|
+
function printObjectTypeBuilder(typeName, objects) {
|
|
256
|
+
const fields = objects[typeName]?.map((field) => printObjectTypeFieldBuilder(typeName, field));
|
|
257
|
+
if (fields == null || fields.length === 0) {
|
|
258
|
+
return "";
|
|
274
259
|
}
|
|
275
|
-
|
|
260
|
+
const content = buildCodeBlock({
|
|
261
|
+
name: "",
|
|
262
|
+
lines: fields
|
|
263
|
+
});
|
|
264
|
+
return `${typeName}: Baeta.createTypeBuilder("${typeName}",${content}, ${getExtensionsVar()})`;
|
|
265
|
+
}
|
|
266
|
+
function printTypeNameResolver() {
|
|
267
|
+
return "{ __resolveType: (source: {__typename: string}) => { return source.__typename; }}";
|
|
276
268
|
}
|
|
277
|
-
function
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (!schema) {
|
|
281
|
-
return { fieldTypes, fieldArguments };
|
|
269
|
+
function getParentType(type) {
|
|
270
|
+
if (["Query", "Mutation", "Subscription"].includes(type)) {
|
|
271
|
+
return "{}";
|
|
282
272
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const fieldsMap = schemaType.getFields();
|
|
290
|
-
for (const field of type.astNode.fields ?? []) {
|
|
291
|
-
collectObjectFieldArguments(type.astNode, field, fieldArguments);
|
|
292
|
-
collectObjectFieldType(type.astNode, field, fieldsMap, fieldTypes);
|
|
293
|
-
}
|
|
273
|
+
return `Types.${type}`;
|
|
274
|
+
}
|
|
275
|
+
function getResultType(config, type, field) {
|
|
276
|
+
const fieldType = config.fieldInfo.get(type)?.get(field)?.type;
|
|
277
|
+
if (fieldType == null) {
|
|
278
|
+
return "{}";
|
|
294
279
|
}
|
|
295
|
-
return
|
|
280
|
+
return fieldType;
|
|
296
281
|
}
|
|
297
|
-
function
|
|
298
|
-
|
|
299
|
-
|
|
282
|
+
function getArgsType(config, type, field) {
|
|
283
|
+
const hasArgs = config.fieldInfo.get(type)?.get(field)?.hasArguments ?? false;
|
|
284
|
+
if (!hasArgs) {
|
|
285
|
+
return "{}";
|
|
300
286
|
}
|
|
301
|
-
|
|
302
|
-
|
|
287
|
+
const fieldUpper = field[0].toUpperCase() + field.slice(1);
|
|
288
|
+
return `Types.${type}${fieldUpper}Args`;
|
|
289
|
+
}
|
|
290
|
+
function printObjectTypeFieldBuilder(typeName, field) {
|
|
291
|
+
if (typeName === "Subscription") {
|
|
292
|
+
return `${field}: Baeta.createSubscriptionBuilder("${field}", ${getExtensionsVar()})`;
|
|
303
293
|
}
|
|
304
|
-
return
|
|
294
|
+
return `${field}: Baeta.createFieldBuilder("${typeName}", "${field}", ${getExtensionsVar()})`;
|
|
305
295
|
}
|
|
306
|
-
function
|
|
307
|
-
return
|
|
296
|
+
function getContextType() {
|
|
297
|
+
return "Ctx";
|
|
308
298
|
}
|
|
309
|
-
function
|
|
310
|
-
return
|
|
299
|
+
function getInfoType() {
|
|
300
|
+
return "Info";
|
|
311
301
|
}
|
|
312
|
-
function
|
|
313
|
-
return
|
|
302
|
+
function getExtensionsVar() {
|
|
303
|
+
return "extensions";
|
|
314
304
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
return
|
|
305
|
+
|
|
306
|
+
// lib/printer/printer-templates.ts
|
|
307
|
+
import { relative as relative2 } from "@baeta/util-path";
|
|
308
|
+
function printTypesTemplate(options) {
|
|
309
|
+
const importDir = relative2(options.modulesDir, options.typesDir);
|
|
310
|
+
return `import type { GraphQLResolveInfo } from 'graphql';
|
|
311
|
+
import type { BaseObjectTypes, BaseScalars } from '${importDir}/utility${options.importExtension}';
|
|
312
|
+
|
|
313
|
+
export interface Scalars extends BaseScalars {}
|
|
314
|
+
|
|
315
|
+
export interface ObjectTypes extends BaseObjectTypes {}
|
|
316
|
+
|
|
317
|
+
export type Ctx = {};
|
|
318
|
+
|
|
319
|
+
export type Info = GraphQLResolveInfo;
|
|
320
|
+
`;
|
|
321
321
|
}
|
|
322
|
-
function
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
322
|
+
function printExtensionsTemplate() {
|
|
323
|
+
return `import { createExtensions } from '@baeta/core';
|
|
324
|
+
|
|
325
|
+
export default createExtensions({});
|
|
326
|
+
`;
|
|
327
327
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
328
|
+
|
|
329
|
+
// lib/printer/printer-types.ts
|
|
330
|
+
import { pascalCase as pascalCase2 } from "change-case-all";
|
|
331
|
+
import {
|
|
332
|
+
Kind
|
|
333
|
+
} from "graphql";
|
|
334
|
+
|
|
335
|
+
// utils/scalar.ts
|
|
336
|
+
function isScalarType(definitionsMap, defaultScalars, type) {
|
|
337
|
+
return definitionsMap.scalarTypeMap.has(type.name.value) || defaultScalars.includes(type.name.value);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// lib/printer/printer-types.ts
|
|
341
|
+
function printUtilityTypes() {
|
|
342
|
+
return [
|
|
343
|
+
"export type Or<A, B> = void extends A ? B : A;",
|
|
344
|
+
"export type Maybe<T> = T | null;"
|
|
345
|
+
].join("\n\n");
|
|
346
|
+
}
|
|
347
|
+
var defaultScalarTypes = {
|
|
348
|
+
ID: "string",
|
|
349
|
+
String: "string",
|
|
350
|
+
Boolean: "boolean",
|
|
351
|
+
Int: "number",
|
|
352
|
+
Float: "number"
|
|
335
353
|
};
|
|
336
|
-
function
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
if (
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
354
|
+
function printBaseScalars(config) {
|
|
355
|
+
const defaultScalars = config.defaultScalars.map((scalar) => {
|
|
356
|
+
const type = defaultScalarTypes[scalar];
|
|
357
|
+
if (type == null) return null;
|
|
358
|
+
return `${scalar}: ${type};`;
|
|
359
|
+
}).filter((el) => el != null);
|
|
360
|
+
const customScalars = [...config.globalDefinitions.scalarTypeMap.values()].map(
|
|
361
|
+
(scalar) => `${scalar.name.value}: unknown;`
|
|
362
|
+
);
|
|
363
|
+
return buildBlock({
|
|
364
|
+
name: "export type BaseScalars =",
|
|
365
|
+
lines: [...defaultScalars, ...customScalars]
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
function printBaseObjectTypes(config) {
|
|
369
|
+
const objectTypes = [...config.globalDefinitions.objectTypeMap.values()].map((t) => t.name.value);
|
|
370
|
+
return buildBlock({
|
|
371
|
+
name: "export interface BaseObjectTypes",
|
|
372
|
+
lines: objectTypes.map((t) => `${t}: unknown;`)
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
function printTypesHeaders() {
|
|
376
|
+
return [
|
|
377
|
+
'import * as BaetaUtility from "./utility.ts";',
|
|
378
|
+
'import * as BaetaOverrides from "../modules/types.ts";',
|
|
379
|
+
"",
|
|
380
|
+
"export type Scalars = BaetaOverrides.Scalars;"
|
|
381
|
+
].join("\n");
|
|
382
|
+
}
|
|
383
|
+
function printRootTypesFromMap(config, rootTypes, map) {
|
|
384
|
+
return [...map.values()].filter((type) => rootTypes.includes(type.name.value)).map((type) => printObjectType(config, type)).join("\n\n");
|
|
385
|
+
}
|
|
386
|
+
function printObjectTypeTypesFromMap(config, map) {
|
|
387
|
+
return [...map.values()].map((type) => printObjectType(config, type));
|
|
388
|
+
}
|
|
389
|
+
function printObjectType(config, type) {
|
|
390
|
+
const fields = type.fields?.map((field) => printObjectTypeField(config, field));
|
|
391
|
+
return `export type ${type.name.value} = ${printOrObjectType(type, fields)}`;
|
|
392
|
+
}
|
|
393
|
+
function printObjectTypeField(config, field) {
|
|
394
|
+
const optionalMarker = printOptionalMarker(config, field.type);
|
|
395
|
+
return `${field.name.value}${optionalMarker}: ${printType(config, field.type)}`;
|
|
396
|
+
}
|
|
397
|
+
function printEnumTypesFromMap(map) {
|
|
398
|
+
return [...map.values()].map(printEnumType);
|
|
399
|
+
}
|
|
400
|
+
function printEnumType(type) {
|
|
401
|
+
const values = type.values?.map((value) => `'${value.name.value}'`);
|
|
402
|
+
return `export type ${type.name.value} = ${values?.join(" | ") || "never"}`;
|
|
403
|
+
}
|
|
404
|
+
function printInterfaceTypesFromMap(config, map) {
|
|
405
|
+
return [...map.values()].map((type) => printInterfaceType(config, type));
|
|
406
|
+
}
|
|
407
|
+
function printInterfaceType(config, type) {
|
|
408
|
+
const objectTypes = Array.from(config.globalDefinitions.objectTypeMap.values());
|
|
409
|
+
const implementingTypes = objectTypes.filter(
|
|
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"}`;
|
|
414
|
+
}
|
|
415
|
+
function printNamedTypeForInterface(name) {
|
|
416
|
+
return `${name} & {__typename: "${name}"}`;
|
|
417
|
+
}
|
|
418
|
+
function printUnionTypesFromMap(config, map) {
|
|
419
|
+
return [...map.values()].map((type) => printUnionType(config, type));
|
|
420
|
+
}
|
|
421
|
+
function printUnionType(config, type) {
|
|
422
|
+
const types = type.types?.map((type2) => printNamedTypeForUnion(config, type2));
|
|
423
|
+
return `export type ${type.name.value} = ${types?.join(" | ") || "never"}`;
|
|
424
|
+
}
|
|
425
|
+
function printNamedTypeForUnion(config, type) {
|
|
426
|
+
return `${printNamedType(config, type, false)} & {__typename: "${type.name.value}"}`;
|
|
427
|
+
}
|
|
428
|
+
function printInputObjectTypeTypesFromMap(config, map) {
|
|
429
|
+
return [...map.values()].map((type) => printInputObjectType(config, type));
|
|
430
|
+
}
|
|
431
|
+
function printInputObjectType(config, type) {
|
|
432
|
+
const fields = type.fields?.map((field) => printInputObjectTypeField(config, field));
|
|
433
|
+
return buildBlock({
|
|
434
|
+
name: `export type ${type.name.value} =`,
|
|
435
|
+
lines: fields ?? []
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
function printInputObjectTypeField(config, field) {
|
|
439
|
+
const optionalMarker = printOptionalMarker(config, field.type);
|
|
440
|
+
return `${field.name.value}${optionalMarker}: ${printType(config, field.type)}`;
|
|
441
|
+
}
|
|
442
|
+
function printObjectTypeFieldsArgsFromMap(config, map) {
|
|
443
|
+
return [...map.values()].flatMap((type) => printObjectTypeArgs(config, type));
|
|
444
|
+
}
|
|
445
|
+
function printObjectTypeArgs(config, type) {
|
|
446
|
+
if (type.fields == null) {
|
|
447
|
+
return [];
|
|
350
448
|
}
|
|
351
|
-
return
|
|
449
|
+
return type.fields.map((field) => printObjectTypeFieldArgs(config, type.name.value, field)).filter(Boolean);
|
|
352
450
|
}
|
|
353
|
-
function
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
|
|
451
|
+
function printObjectTypeFieldArgs(config, typeName, field) {
|
|
452
|
+
const name = `${typeName + pascalCase2(field.name.value)}Args`;
|
|
453
|
+
return buildBlock({
|
|
454
|
+
name: `export type ${name} =`,
|
|
455
|
+
lines: field.arguments?.map((arg) => printObjectTypeFieldArg(config, arg)) ?? []
|
|
456
|
+
});
|
|
357
457
|
}
|
|
358
|
-
function
|
|
359
|
-
|
|
360
|
-
return normalize(parsedPath.dir);
|
|
458
|
+
function printObjectTypeFieldArg(config, arg) {
|
|
459
|
+
return `${arg.name.value}: ${printType(config, arg.type)}`;
|
|
361
460
|
}
|
|
362
|
-
function
|
|
363
|
-
|
|
461
|
+
function printType(config, type, nullable = true) {
|
|
462
|
+
switch (type.kind) {
|
|
463
|
+
case Kind.NAMED_TYPE:
|
|
464
|
+
return printNamedType(config, type, nullable);
|
|
465
|
+
case Kind.LIST_TYPE:
|
|
466
|
+
return printListType(config, type, nullable);
|
|
467
|
+
case Kind.NON_NULL_TYPE:
|
|
468
|
+
return printType(config, type.type, false);
|
|
469
|
+
default:
|
|
470
|
+
return type;
|
|
471
|
+
}
|
|
364
472
|
}
|
|
365
|
-
function
|
|
366
|
-
return
|
|
473
|
+
function printListType(config, type, nullable = true) {
|
|
474
|
+
return printValue(config, `Array<${printType(config, type.type)}>`, nullable);
|
|
367
475
|
}
|
|
368
|
-
function
|
|
369
|
-
if (
|
|
370
|
-
return
|
|
476
|
+
function printNamedType(config, type, nullable = true) {
|
|
477
|
+
if (isScalarType(config.globalDefinitions, config.defaultScalars, type)) {
|
|
478
|
+
return printScalarType(config, type, nullable);
|
|
371
479
|
}
|
|
372
|
-
return
|
|
480
|
+
return printValue(config, type.name.value, nullable);
|
|
373
481
|
}
|
|
374
|
-
function
|
|
375
|
-
|
|
376
|
-
|
|
482
|
+
function printScalarType(config, type, nullable = true) {
|
|
483
|
+
return printValue(config, `Scalars["${type.name.value}"]`, nullable);
|
|
484
|
+
}
|
|
485
|
+
function printValue(config, value, nullable) {
|
|
486
|
+
if (!nullable) {
|
|
487
|
+
return value;
|
|
488
|
+
}
|
|
489
|
+
if (config.withMaybe) {
|
|
490
|
+
return `BaetaUtility.Maybe<${value}>`;
|
|
377
491
|
}
|
|
492
|
+
return `${value} | null`;
|
|
378
493
|
}
|
|
379
|
-
function
|
|
380
|
-
return
|
|
494
|
+
function printOptionalMarker(config, type) {
|
|
495
|
+
return config.withOptional && type.kind !== Kind.NON_NULL_TYPE ? "?" : "";
|
|
381
496
|
}
|
|
382
|
-
function
|
|
383
|
-
return
|
|
497
|
+
function printOrObjectType(type, fields) {
|
|
498
|
+
return `BaetaUtility.Or<BaetaOverrides.ObjectTypes["${type.name.value}"], {
|
|
499
|
+
${fields?.map(indent(2)).join("\n") ?? ""}
|
|
500
|
+
}>`;
|
|
384
501
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
502
|
+
|
|
503
|
+
// lib/visitors/definitions-map.ts
|
|
504
|
+
import {
|
|
505
|
+
visit
|
|
506
|
+
} from "graphql";
|
|
507
|
+
function createRegistry() {
|
|
508
|
+
return {
|
|
509
|
+
scalarTypeMap: /* @__PURE__ */ new Map(),
|
|
510
|
+
enumTypeMap: /* @__PURE__ */ new Map(),
|
|
511
|
+
objectTypeMap: /* @__PURE__ */ new Map(),
|
|
512
|
+
inputObjectTypeMap: /* @__PURE__ */ new Map(),
|
|
513
|
+
unionTypeMap: /* @__PURE__ */ new Map(),
|
|
514
|
+
interfaceTypeMap: /* @__PURE__ */ new Map()
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
function createDefinitionsMapFromSources(sources) {
|
|
518
|
+
const registry = createRegistry();
|
|
519
|
+
for (const source of sources) {
|
|
520
|
+
if (source.document) {
|
|
521
|
+
collectTypesFromDocument(source.document, registry, ["Query", "Mutation", "Subscription"]);
|
|
522
|
+
}
|
|
389
523
|
}
|
|
390
|
-
return
|
|
524
|
+
return registry;
|
|
525
|
+
}
|
|
526
|
+
function createDefinitionsMapFromDocument(document) {
|
|
527
|
+
const registry = createRegistry();
|
|
528
|
+
collectTypesFromDocument(document, registry);
|
|
529
|
+
return registry;
|
|
530
|
+
}
|
|
531
|
+
function collectTypesFromDocument(document, registry, filterObjectTypes = []) {
|
|
532
|
+
visit(document, {
|
|
533
|
+
ScalarTypeDefinition(node) {
|
|
534
|
+
if (registry.scalarTypeMap.has(node.name.value)) {
|
|
535
|
+
throw new Error(`Scalar type ${node.name.value} already exists`);
|
|
536
|
+
}
|
|
537
|
+
registry.scalarTypeMap.set(node.name.value, node);
|
|
538
|
+
},
|
|
539
|
+
EnumTypeDefinition(node) {
|
|
540
|
+
if (registry.enumTypeMap.has(node.name.value)) {
|
|
541
|
+
throw new Error(`Enum type ${node.name.value} already exists, use 'extend' instead!`);
|
|
542
|
+
}
|
|
543
|
+
registry.enumTypeMap.set(node.name.value, node);
|
|
544
|
+
},
|
|
545
|
+
ObjectTypeDefinition(node) {
|
|
546
|
+
if (filterObjectTypes.includes(node.name.value)) {
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
if (registry.objectTypeMap.has(node.name.value)) {
|
|
550
|
+
throw new Error(`Object type ${node.name.value} already exists, use 'extend' instead!`);
|
|
551
|
+
}
|
|
552
|
+
registry.objectTypeMap.set(node.name.value, node);
|
|
553
|
+
},
|
|
554
|
+
InputObjectTypeDefinition(node) {
|
|
555
|
+
if (registry.inputObjectTypeMap.has(node.name.value)) {
|
|
556
|
+
throw new Error(`Input type ${node.name.value} already exists. Use 'extend' instead!`);
|
|
557
|
+
}
|
|
558
|
+
registry.inputObjectTypeMap.set(node.name.value, node);
|
|
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
|
+
});
|
|
391
573
|
}
|
|
392
574
|
|
|
393
|
-
// lib/
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
575
|
+
// lib/visitors/field-info.ts
|
|
576
|
+
import { Kind as Kind2 } from "graphql";
|
|
577
|
+
function createFieldInfoMap(definitionsMap, defaultScalars) {
|
|
578
|
+
const map = /* @__PURE__ */ new Map();
|
|
579
|
+
for (const objectType of definitionsMap.objectTypeMap.values()) {
|
|
580
|
+
const objectName = objectType.name.value;
|
|
581
|
+
for (const field of objectType.fields ?? []) {
|
|
582
|
+
const fieldName = field.name.value;
|
|
583
|
+
const type = printNamedType2(definitionsMap, defaultScalars, field.type, true);
|
|
584
|
+
const hasArguments = field.arguments != null && field.arguments.length > 0;
|
|
585
|
+
const fieldMap = map.get(objectName) ?? /* @__PURE__ */ new Map();
|
|
586
|
+
fieldMap.set(fieldName, { type, hasArguments });
|
|
587
|
+
map.set(objectName, fieldMap);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return map;
|
|
591
|
+
}
|
|
592
|
+
function printNamedType2(definitionsMap, defaultScalars, type, nullable = true) {
|
|
593
|
+
const withNullable = nullable ? " | null" : "";
|
|
594
|
+
switch (type.kind) {
|
|
595
|
+
case Kind2.NAMED_TYPE:
|
|
596
|
+
if (isScalarType(definitionsMap, defaultScalars, type)) {
|
|
597
|
+
return `Types.Scalars["${type.name.value}"]${withNullable}`;
|
|
598
|
+
}
|
|
599
|
+
return `Types.${type.name.value}${withNullable}`;
|
|
600
|
+
case Kind2.LIST_TYPE:
|
|
601
|
+
return `Array<${printNamedType2(definitionsMap, defaultScalars, type.type, nullable)}>${withNullable}`;
|
|
602
|
+
case Kind2.NON_NULL_TYPE:
|
|
603
|
+
return printNamedType2(definitionsMap, defaultScalars, type.type, false);
|
|
604
|
+
default:
|
|
605
|
+
return type;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// lib/visitors/module-registry.ts
|
|
610
|
+
import {
|
|
611
|
+
visit as visit2
|
|
612
|
+
} from "graphql";
|
|
613
|
+
var registryKeys = ["objects", "interfaces", "unions", "scalars"];
|
|
614
|
+
function createModuleRegistry(document) {
|
|
414
615
|
const picks = createObject(
|
|
415
616
|
registryKeys,
|
|
416
617
|
() => ({})
|
|
417
618
|
);
|
|
418
619
|
const defined = createObject(registryKeys, () => []);
|
|
419
|
-
|
|
420
|
-
const hashes = {};
|
|
421
|
-
const usedTypes = collectUsedTypes(doc);
|
|
422
|
-
visit(doc, {
|
|
620
|
+
visit2(document, {
|
|
423
621
|
ObjectTypeDefinition(node) {
|
|
424
|
-
|
|
622
|
+
defined.objects.push(node.name.value);
|
|
623
|
+
collectFields(node, picks.objects);
|
|
425
624
|
},
|
|
426
625
|
ObjectTypeExtension(node) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
InputObjectTypeDefinition(node) {
|
|
430
|
-
collectTypeDefinition(node);
|
|
431
|
-
},
|
|
432
|
-
InputObjectTypeExtension(node) {
|
|
433
|
-
collectTypeExtension(node);
|
|
626
|
+
pushUnique(defined.objects, node.name.value);
|
|
627
|
+
collectFields(node, picks.objects);
|
|
434
628
|
},
|
|
435
629
|
InterfaceTypeDefinition(node) {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
InterfaceTypeExtension(node) {
|
|
439
|
-
collectTypeExtension(node);
|
|
440
|
-
},
|
|
441
|
-
ScalarTypeDefinition(node) {
|
|
442
|
-
collectTypeDefinition(node);
|
|
630
|
+
defined.interfaces.push(node.name.value);
|
|
631
|
+
collectFields(node, picks.interfaces);
|
|
443
632
|
},
|
|
444
633
|
UnionTypeDefinition(node) {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
UnionTypeExtension(node) {
|
|
448
|
-
collectTypeExtension(node);
|
|
449
|
-
},
|
|
450
|
-
EnumTypeDefinition(node) {
|
|
451
|
-
collectTypeDefinition(node);
|
|
634
|
+
defined.unions.push(node.name.value);
|
|
635
|
+
collectUnionTypes(node, picks);
|
|
452
636
|
},
|
|
453
|
-
|
|
454
|
-
|
|
637
|
+
ScalarTypeDefinition(node) {
|
|
638
|
+
defined.scalars.push(node.name.value);
|
|
455
639
|
}
|
|
456
640
|
});
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
)
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
'import type { DocumentNode } from "graphql";',
|
|
468
|
-
'import * as Baeta from "@baeta/core/sdk";'
|
|
469
|
-
];
|
|
470
|
-
if (extensionsPath) {
|
|
471
|
-
imports.push(`import baetaExtensions from "${extensionsPath}";`);
|
|
472
|
-
}
|
|
473
|
-
imports.push("\n");
|
|
474
|
-
let content = [
|
|
475
|
-
printDefinedFields(),
|
|
476
|
-
printDefinedEnumValues(),
|
|
477
|
-
printDefinedInputFields(),
|
|
478
|
-
printSchemaTypes(usedTypes),
|
|
479
|
-
printMetadata()
|
|
480
|
-
].filter(Boolean).join("\n\n");
|
|
481
|
-
const moduleNamespace = baseVisitor.convertName(name, {
|
|
482
|
-
suffix: "Module",
|
|
483
|
-
useTypesPrefix: false,
|
|
484
|
-
useTypesSuffix: false
|
|
485
|
-
});
|
|
486
|
-
if (encapsulate === "namespace") {
|
|
487
|
-
content = `${shouldDeclare ? "declare" : "export"} namespace ${baseVisitor.convertName(name, {
|
|
488
|
-
suffix: "Module",
|
|
489
|
-
useTypesPrefix: false,
|
|
490
|
-
useTypesSuffix: false
|
|
491
|
-
})} {
|
|
492
|
-
${shouldDeclare ? `${indent(2)(imports.join("\n"))}
|
|
493
|
-
` : ""}${indent(2)(content)}
|
|
494
|
-
}`;
|
|
495
|
-
}
|
|
496
|
-
return [...shouldDeclare ? [] : imports, content, printFactoryMethod()].filter(Boolean).join("\n");
|
|
497
|
-
function printMetadata() {
|
|
498
|
-
return `export namespace ModuleMetadata {
|
|
499
|
-
export const id = '${name}';
|
|
500
|
-
export const dirname = './${name}';
|
|
501
|
-
export const hashes = ${JSON.stringify(hashes)};
|
|
502
|
-
export const typedef = ${JSON.stringify(doc)} as unknown as DocumentNode;
|
|
503
|
-
${printBaetaManager()}
|
|
504
|
-
}`;
|
|
505
|
-
}
|
|
506
|
-
function printDefinedFields() {
|
|
507
|
-
return buildBlock({
|
|
508
|
-
name: "interface DefinedFields",
|
|
509
|
-
lines: [...visited.objects, ...visited.interfaces].map(
|
|
510
|
-
(typeName) => `${typeName}: ${printPicks(typeName, {
|
|
511
|
-
...picks.objects,
|
|
512
|
-
...picks.interfaces
|
|
513
|
-
})};`
|
|
514
|
-
)
|
|
515
|
-
});
|
|
516
|
-
}
|
|
517
|
-
function printFactoryMethod() {
|
|
518
|
-
const name2 = moduleNamespace.slice(0, moduleNamespace.length - 6);
|
|
519
|
-
const createModuleFn = `create${name2}Module`;
|
|
520
|
-
const getModuleFn = `get${name2}Module`;
|
|
521
|
-
const extensionsArg = extensionsPath ? ", baetaExtensions" : "";
|
|
522
|
-
return `
|
|
523
|
-
export const ${createModuleFn} = () => Baeta.createModuleManager(ModuleMetadata${extensionsArg});
|
|
524
|
-
export const ${getModuleFn} = Baeta.createSingletonModule(${createModuleFn});
|
|
525
|
-
`;
|
|
526
|
-
}
|
|
527
|
-
function printObjectFieldResolverBuilder(typeName, field) {
|
|
528
|
-
const parentType = getParentType(typeName);
|
|
529
|
-
const resultType = getResultType(typeName, field);
|
|
530
|
-
const argumentsType = getArgsType(typeName, field);
|
|
531
|
-
const contextType = getContextType();
|
|
532
|
-
return `${field}: module.createResolverBuilder<${resultType}, ${parentType}, ${contextType}, ${argumentsType}>("${typeName}", "${field}"),`;
|
|
533
|
-
}
|
|
534
|
-
function printObjectResolverBuilder(typeName, objects) {
|
|
535
|
-
const fields = objects[typeName]?.filter(unique).map((field) => printObjectFieldResolverBuilder(typeName, field)) ?? [];
|
|
536
|
-
if (fields.length === 0) {
|
|
537
|
-
return "";
|
|
538
|
-
}
|
|
539
|
-
const parentType = getParentType(typeName);
|
|
540
|
-
const contextType = getContextType();
|
|
541
|
-
const addons = [`...module.createTypeMethods<${parentType}, ${contextType}>("${typeName}"),`];
|
|
542
|
-
const contentBody = [...addons, ...fields].map(indent(2)).join("\n");
|
|
543
|
-
const content2 = `{
|
|
544
|
-
${contentBody}
|
|
545
|
-
}`;
|
|
546
|
-
return `${typeName}: ${content2},`;
|
|
547
|
-
}
|
|
548
|
-
function printSubscriptionFieldBuilder(field) {
|
|
549
|
-
const parentType = getParentType("Subscription");
|
|
550
|
-
const resultType = getResultType("Subscription", field);
|
|
551
|
-
const argumentsType = getArgsType("Subscription", field);
|
|
552
|
-
const contextType = getContextType();
|
|
553
|
-
return `${field}: module.createSubscriptionBuilder<${resultType}, ${parentType}, ${contextType}, ${argumentsType}>("${field}"),`;
|
|
554
|
-
}
|
|
555
|
-
function printSubscriptionObjectBuilder() {
|
|
556
|
-
const subscriptions = picks.objects.Subscription?.filter(unique) ?? [];
|
|
557
|
-
if (subscriptions.length === 0) {
|
|
558
|
-
return "";
|
|
559
|
-
}
|
|
560
|
-
const fields = subscriptions.map((subscription) => printSubscriptionFieldBuilder(subscription));
|
|
561
|
-
const parentType = getParentType("Subscription");
|
|
562
|
-
const contextType = getContextType();
|
|
563
|
-
const addons = [`...module.createSubscriptionMethods<${parentType}, ${contextType}>(),`];
|
|
564
|
-
const contentBody = [...addons, ...fields].map(indent(2)).join("\n");
|
|
565
|
-
const content2 = `{
|
|
566
|
-
${contentBody}
|
|
567
|
-
}`;
|
|
568
|
-
return `Subscription: ${content2},`;
|
|
569
|
-
}
|
|
570
|
-
function printScalarBuilder() {
|
|
571
|
-
const scalars = visited.scalars;
|
|
572
|
-
if (scalars.length === 0) {
|
|
573
|
-
return "";
|
|
574
|
-
}
|
|
575
|
-
const fields = scalars.map((scalar) => `${scalar}: module.createScalarBuilder("${scalar}"),`);
|
|
576
|
-
const content2 = fields.map(indent(2)).join("\n");
|
|
577
|
-
return `Scalar: {
|
|
578
|
-
${content2}
|
|
579
|
-
},`;
|
|
580
|
-
}
|
|
581
|
-
function printTypenameResolverBuilder(name2, resultNamespace, valueNamespace) {
|
|
582
|
-
const resultType = `${importNamespace}.${resultNamespace}["${name2}"]`;
|
|
583
|
-
const valueType = `${importNamespace}.${valueNamespace}["${name2}"]`;
|
|
584
|
-
const contextType = getContextType();
|
|
585
|
-
const resolver = `$resolveType: module.createResolveType<${resultType}, ${valueType}, ${contextType}>("${name2}"),`;
|
|
586
|
-
return `${name2}: {
|
|
587
|
-
${indent(2)(resolver)}
|
|
588
|
-
},`;
|
|
589
|
-
}
|
|
590
|
-
function printInterfaceBuilder() {
|
|
591
|
-
const interfaces = defined.interfaces;
|
|
592
|
-
if (interfaces.length === 0) {
|
|
593
|
-
return "";
|
|
594
|
-
}
|
|
595
|
-
return interfaces.map(
|
|
596
|
-
(interfaceName) => printTypenameResolverBuilder(
|
|
597
|
-
interfaceName,
|
|
598
|
-
"DefinedInterfacesResults",
|
|
599
|
-
"DefinedInterfacesWithoutExtensions"
|
|
600
|
-
)
|
|
601
|
-
).join("\n");
|
|
602
|
-
}
|
|
603
|
-
function printUnionBuilder() {
|
|
604
|
-
const unions = defined.unions;
|
|
605
|
-
if (unions.length === 0) {
|
|
606
|
-
return "";
|
|
607
|
-
}
|
|
608
|
-
return unions.map(
|
|
609
|
-
(unionName) => printTypenameResolverBuilder(
|
|
610
|
-
unionName,
|
|
611
|
-
"DefinedUnionsResults",
|
|
612
|
-
"DefinedUnionsWithoutExtensions"
|
|
613
|
-
)
|
|
614
|
-
).join("\n");
|
|
615
|
-
}
|
|
616
|
-
function printBaetaManager() {
|
|
617
|
-
const objects = visited.objects.filter((type) => type !== "Subscription").map((typeName) => printObjectResolverBuilder(typeName, picks.objects)).filter(Boolean);
|
|
618
|
-
const contextType = getContextType();
|
|
619
|
-
const addons = [`...module.createModuleMethods<${contextType}>(),`];
|
|
620
|
-
const bodyFields = [
|
|
621
|
-
...addons,
|
|
622
|
-
...objects,
|
|
623
|
-
printScalarBuilder(),
|
|
624
|
-
printSubscriptionObjectBuilder(),
|
|
625
|
-
printInterfaceBuilder(),
|
|
626
|
-
printUnionBuilder()
|
|
627
|
-
];
|
|
628
|
-
const body = bodyFields.filter(Boolean).map(indent(6)).join("\n");
|
|
629
|
-
const content2 = `{
|
|
630
|
-
${body}
|
|
631
|
-
}`;
|
|
632
|
-
return `
|
|
633
|
-
export function createManager(module: Baeta.ModuleBuilder) {
|
|
634
|
-
return ${content2};
|
|
635
|
-
}`;
|
|
636
|
-
}
|
|
637
|
-
function printDefinedEnumValues() {
|
|
638
|
-
return buildBlock({
|
|
639
|
-
name: "interface DefinedEnumValues",
|
|
640
|
-
lines: visited.enums.map((typeName) => `${typeName}: ${printPicks(typeName, picks.enums)};`)
|
|
641
|
-
});
|
|
642
|
-
}
|
|
643
|
-
function encapsulateTypeName(typeName) {
|
|
644
|
-
if (encapsulate === "prefix") {
|
|
645
|
-
return `${pascalCase(name)}_${typeName}`;
|
|
646
|
-
}
|
|
647
|
-
return typeName;
|
|
648
|
-
}
|
|
649
|
-
function printDefinedInputFields() {
|
|
650
|
-
return buildBlock({
|
|
651
|
-
name: "interface DefinedInputFields",
|
|
652
|
-
lines: visited.inputs.map(
|
|
653
|
-
(typeName) => `${typeName}: ${printPicks(typeName, picks.inputs)};`
|
|
654
|
-
)
|
|
655
|
-
});
|
|
656
|
-
}
|
|
657
|
-
function printSchemaTypes(types) {
|
|
658
|
-
return types.filter((type) => !visited.scalars.includes(type)).map(printExportType).join("\n");
|
|
659
|
-
}
|
|
660
|
-
function printPicks(typeName, records) {
|
|
661
|
-
return records[typeName].filter(unique).map(withQuotes).join(" | ");
|
|
662
|
-
}
|
|
663
|
-
function printTypeBody(typeName) {
|
|
664
|
-
const normalizedTypeName = baseVisitor.convertName(typeName, {
|
|
665
|
-
useTypesSuffix: true,
|
|
666
|
-
useTypesPrefix: true
|
|
667
|
-
});
|
|
668
|
-
const coreType = `${importNamespace}.${normalizedTypeName}`;
|
|
669
|
-
if (external.enums.includes(typeName) || external.objects.includes(typeName)) {
|
|
670
|
-
if (schema && isScalarType(schema.getType(typeName))) {
|
|
671
|
-
return `${importNamespace}.Scalars['${typeName}']`;
|
|
672
|
-
}
|
|
673
|
-
return `Pick<${coreType}, ${importNamespace}.DefinedFieldsWithoutExtensions["${typeName}"]>`;
|
|
674
|
-
}
|
|
675
|
-
if (external.unions.includes(typeName)) {
|
|
676
|
-
return `${importNamespace}.DefinedUnionsWithoutExtensions["${typeName}"]`;
|
|
677
|
-
}
|
|
678
|
-
if (external.interfaces.includes(typeName)) {
|
|
679
|
-
return `Pick<${coreType}, ${importNamespace}.DefinedFieldsWithoutExtensions["${typeName}"]>`;
|
|
680
|
-
}
|
|
681
|
-
if (defined.enums.includes(typeName) && picks.enums[typeName]) {
|
|
682
|
-
return `DefinedEnumValues['${typeName}']`;
|
|
683
|
-
}
|
|
684
|
-
if (defined.unions.includes(typeName) && picks.unions[typeName]) {
|
|
685
|
-
return `${importNamespace}.DefinedUnionsWithoutExtensions["${typeName}"]`;
|
|
686
|
-
}
|
|
687
|
-
if (defined.objects.includes(typeName) && picks.objects[typeName]) {
|
|
688
|
-
return `Pick<${coreType}, DefinedFields['${typeName}']>`;
|
|
689
|
-
}
|
|
690
|
-
if (defined.interfaces.includes(typeName) && picks.interfaces[typeName]) {
|
|
691
|
-
return `${importNamespace}.DefinedInterfacesWithoutExtensions["${typeName}"]`;
|
|
692
|
-
}
|
|
693
|
-
if (defined.inputs.includes(typeName) && picks.inputs[typeName]) {
|
|
694
|
-
return `Pick<${coreType}, DefinedInputFields['${typeName}']>`;
|
|
695
|
-
}
|
|
696
|
-
if (isScalarType(schema?.getType(typeName))) {
|
|
697
|
-
return coreType;
|
|
698
|
-
}
|
|
699
|
-
if (isInterfaceType(schema?.getType(typeName))) {
|
|
700
|
-
return `${importNamespace}.DefinedInterfacesWithoutExtensions["${typeName}"]`;
|
|
701
|
-
}
|
|
702
|
-
if (isUnionType(schema?.getType(typeName))) {
|
|
703
|
-
return `${importNamespace}.DefinedUnionsWithoutExtensions["${typeName}"]`;
|
|
704
|
-
}
|
|
705
|
-
return `Pick<${coreType}, ${importNamespace}.DefinedFieldsWithoutExtensions["${typeName}"]>`;
|
|
706
|
-
}
|
|
707
|
-
function printExportType(typeName) {
|
|
708
|
-
return `export type ${encapsulateTypeName(typeName)} = ${printTypeBody(typeName)};`;
|
|
709
|
-
}
|
|
710
|
-
function collectFields(node, picksObj) {
|
|
711
|
-
const name2 = node.name.value;
|
|
712
|
-
if (node.fields) {
|
|
713
|
-
if (!picksObj[name2]) {
|
|
714
|
-
picksObj[name2] = [];
|
|
715
|
-
}
|
|
716
|
-
for (const field of node.fields) {
|
|
717
|
-
picksObj[name2].push(field.name.value);
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
function collectValuesFromEnum(node) {
|
|
722
|
-
const name2 = node.name.value;
|
|
723
|
-
if (node.values) {
|
|
724
|
-
if (!picks.enums[name2]) {
|
|
725
|
-
picks.enums[name2] = [];
|
|
726
|
-
}
|
|
727
|
-
for (const field of node.values) {
|
|
728
|
-
picks.enums[name2].push(field.name.value);
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
function collectUnionTypes(node) {
|
|
733
|
-
const name2 = node.name.value;
|
|
734
|
-
if (node.types) {
|
|
735
|
-
if (!picks.unions[name2]) {
|
|
736
|
-
picks.unions[name2] = [];
|
|
737
|
-
}
|
|
738
|
-
for (const type of node.types) {
|
|
739
|
-
picks.unions[name2].push(type.name.value);
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
function collectTypeDefinition(node) {
|
|
744
|
-
const name2 = node.name.value;
|
|
745
|
-
switch (node.kind) {
|
|
746
|
-
case Kind3.OBJECT_TYPE_DEFINITION: {
|
|
747
|
-
defined.objects.push(name2);
|
|
748
|
-
collectFields(node, picks.objects);
|
|
749
|
-
hashes[name2] = getObjectTypeHash(node);
|
|
750
|
-
break;
|
|
751
|
-
}
|
|
752
|
-
case Kind3.ENUM_TYPE_DEFINITION: {
|
|
753
|
-
defined.enums.push(name2);
|
|
754
|
-
collectValuesFromEnum(node);
|
|
755
|
-
break;
|
|
756
|
-
}
|
|
757
|
-
case Kind3.INPUT_OBJECT_TYPE_DEFINITION: {
|
|
758
|
-
defined.inputs.push(name2);
|
|
759
|
-
collectFields(node, picks.inputs);
|
|
760
|
-
break;
|
|
761
|
-
}
|
|
762
|
-
case Kind3.SCALAR_TYPE_DEFINITION: {
|
|
763
|
-
defined.scalars.push(name2);
|
|
764
|
-
break;
|
|
765
|
-
}
|
|
766
|
-
case Kind3.INTERFACE_TYPE_DEFINITION: {
|
|
767
|
-
defined.interfaces.push(name2);
|
|
768
|
-
collectFields(node, picks.interfaces);
|
|
769
|
-
break;
|
|
770
|
-
}
|
|
771
|
-
case Kind3.UNION_TYPE_DEFINITION: {
|
|
772
|
-
defined.unions.push(name2);
|
|
773
|
-
collectUnionTypes(node);
|
|
774
|
-
break;
|
|
775
|
-
}
|
|
641
|
+
return {
|
|
642
|
+
defined,
|
|
643
|
+
picks
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
function collectFields(node, picksObj) {
|
|
647
|
+
const name = node.name.value;
|
|
648
|
+
if (node.fields) {
|
|
649
|
+
if (!picksObj[name]) {
|
|
650
|
+
picksObj[name] = [];
|
|
776
651
|
}
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
const name2 = node.name.value;
|
|
780
|
-
switch (node.kind) {
|
|
781
|
-
case Kind3.OBJECT_TYPE_EXTENSION: {
|
|
782
|
-
collectFields(node, picks.objects);
|
|
783
|
-
hashes[name2] = getObjectTypeHash(node);
|
|
784
|
-
if (rootTypes.includes(name2)) {
|
|
785
|
-
pushUnique(defined.objects, name2);
|
|
786
|
-
return;
|
|
787
|
-
}
|
|
788
|
-
pushUnique(extended.objects, name2);
|
|
789
|
-
break;
|
|
790
|
-
}
|
|
791
|
-
case Kind3.ENUM_TYPE_EXTENSION: {
|
|
792
|
-
collectValuesFromEnum(node);
|
|
793
|
-
pushUnique(extended.enums, name2);
|
|
794
|
-
break;
|
|
795
|
-
}
|
|
796
|
-
case Kind3.INPUT_OBJECT_TYPE_EXTENSION: {
|
|
797
|
-
collectFields(node, picks.inputs);
|
|
798
|
-
pushUnique(extended.inputs, name2);
|
|
799
|
-
break;
|
|
800
|
-
}
|
|
801
|
-
case Kind3.INTERFACE_TYPE_EXTENSION: {
|
|
802
|
-
collectFields(node, picks.interfaces);
|
|
803
|
-
pushUnique(extended.interfaces, name2);
|
|
804
|
-
break;
|
|
805
|
-
}
|
|
806
|
-
case Kind3.UNION_TYPE_EXTENSION: {
|
|
807
|
-
pushUnique(extended.unions, name2);
|
|
808
|
-
break;
|
|
809
|
-
}
|
|
652
|
+
for (const field of node.fields) {
|
|
653
|
+
picksObj[name].push(field.name.value);
|
|
810
654
|
}
|
|
811
655
|
}
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
656
|
+
}
|
|
657
|
+
function collectUnionTypes(node, picks) {
|
|
658
|
+
const name = node.name.value;
|
|
659
|
+
if (node.types) {
|
|
660
|
+
if (!picks.unions[name]) {
|
|
661
|
+
picks.unions[name] = [];
|
|
815
662
|
}
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
function getResultType(type, field) {
|
|
819
|
-
return fieldTypes?.[type]?.[field] || "{ }";
|
|
820
|
-
}
|
|
821
|
-
function getArgsType(type, field) {
|
|
822
|
-
const hasArgs = fieldArguments?.[type]?.[field] ?? false;
|
|
823
|
-
if (!hasArgs) {
|
|
824
|
-
return "{ }";
|
|
663
|
+
for (const type of node.types) {
|
|
664
|
+
picks.unions[name].push(type.name.value);
|
|
825
665
|
}
|
|
826
|
-
const fieldUpper = field[0].toUpperCase() + field.slice(1);
|
|
827
|
-
return `Types.${type}${fieldUpper}Args`;
|
|
828
666
|
}
|
|
829
|
-
|
|
830
|
-
|
|
667
|
+
}
|
|
668
|
+
function pushUnique(list, item) {
|
|
669
|
+
if (!list.includes(item)) {
|
|
670
|
+
list.push(item);
|
|
831
671
|
}
|
|
832
672
|
}
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
const { baseOutputDir } = options;
|
|
838
|
-
const { baseTypesPath, extensionsPath, encapsulateModuleTypes, importExtension } = options.presetConfig;
|
|
839
|
-
const requireRootResolvers = getConfigValue(options?.presetConfig.requireRootResolvers, false);
|
|
840
|
-
const cwd = resolve(options.presetConfig.cwd || process.cwd());
|
|
841
|
-
const importTypesNamespace = options.presetConfig.importTypesNamespace || "Types";
|
|
842
|
-
if (!baseTypesPath) {
|
|
843
|
-
throw new Error(
|
|
844
|
-
`Preset "graphql-modules" requires you to specify "baseTypesPath" configuration and point it to your base types file (generated by "typescript" plugin)!`
|
|
845
|
-
);
|
|
846
|
-
}
|
|
847
|
-
if (!options.schemaAst?.extensions.sources) {
|
|
848
|
-
throw new Error(`Preset "graphql-modules" requires to use GraphQL SDL`);
|
|
849
|
-
}
|
|
850
|
-
const extensions = options.schemaAst?.extensions;
|
|
851
|
-
const sources = extensions?.extendedSources ?? [];
|
|
852
|
-
const sourcesByModuleMap = groupSourcesByModule(sources, baseOutputDir);
|
|
853
|
-
const modules = Object.keys(sourcesByModuleMap);
|
|
854
|
-
const baseVisitor = new BaseVisitor(options.config, {});
|
|
855
|
-
const { fieldTypes, fieldArguments } = collectObjectFieldTypesAndArguments(options.schemaAst);
|
|
856
|
-
const baseOutput = {
|
|
857
|
-
filename: resolve(cwd, baseOutputDir, baseTypesPath),
|
|
858
|
-
schema: options.schema,
|
|
859
|
-
documents: options.documents,
|
|
860
|
-
plugins: [
|
|
861
|
-
...options.plugins,
|
|
862
|
-
{
|
|
863
|
-
"modules-exported-scalars": {}
|
|
864
|
-
},
|
|
865
|
-
{
|
|
866
|
-
"modules-exported-picks": {}
|
|
867
|
-
}
|
|
868
|
-
],
|
|
869
|
-
pluginMap: {
|
|
870
|
-
...options.pluginMap,
|
|
871
|
-
"modules-exported-scalars": {
|
|
872
|
-
plugin: (schema) => {
|
|
873
|
-
const typeMap = schema.getTypeMap();
|
|
874
|
-
return Object.keys(typeMap).map((t) => {
|
|
875
|
-
if (t && typeMap[t] && isScalarType2(typeMap[t]) && !isGraphQLPrimitive(t)) {
|
|
876
|
-
const convertedName = baseVisitor.convertName(t);
|
|
877
|
-
return `export type ${convertedName} = Scalars["${t}"];`;
|
|
878
|
-
}
|
|
879
|
-
return null;
|
|
880
|
-
}).filter(Boolean).join("\n");
|
|
881
|
-
}
|
|
882
|
-
},
|
|
883
|
-
"modules-exported-picks": {
|
|
884
|
-
plugin: () => {
|
|
885
|
-
const typePicks = [];
|
|
886
|
-
const unionPicks = [];
|
|
887
|
-
const unionResults = [];
|
|
888
|
-
const interfacePicks = [];
|
|
889
|
-
const interfaceResults = [];
|
|
890
|
-
const unionTypesMap = {};
|
|
891
|
-
const interfacesTypesMap = {};
|
|
892
|
-
for (const moduleName of modules) {
|
|
893
|
-
const sources2 = sourcesByModuleMap[moduleName];
|
|
894
|
-
const documents = sources2.map((source) => source.document);
|
|
895
|
-
const moduleDocument = concatAST(documents);
|
|
896
|
-
const types = moduleDocument.definitions.filter(
|
|
897
|
-
(def) => def.kind === "ObjectTypeDefinition"
|
|
898
|
-
);
|
|
899
|
-
const unionsDefinition2 = moduleDocument.definitions.filter(
|
|
900
|
-
(def) => def.kind === "UnionTypeDefinition"
|
|
901
|
-
);
|
|
902
|
-
const unionsExtensions = moduleDocument.definitions.filter(
|
|
903
|
-
(def) => def.kind === "UnionTypeExtension"
|
|
904
|
-
);
|
|
905
|
-
const unions = [...unionsDefinition2, ...unionsExtensions];
|
|
906
|
-
for (const type of types) {
|
|
907
|
-
const name = type.name.value;
|
|
908
|
-
for (const interfaceNode of type.interfaces ?? []) {
|
|
909
|
-
const interfaceName = interfaceNode.name.value;
|
|
910
|
-
if (interfacesTypesMap[interfaceName] == null) {
|
|
911
|
-
interfacesTypesMap[interfaceName] = [];
|
|
912
|
-
}
|
|
913
|
-
interfacesTypesMap[interfaceName]?.push(name);
|
|
914
|
-
}
|
|
915
|
-
if (name === "Query" || name === "Mutation" || name === "Subscription") {
|
|
916
|
-
continue;
|
|
917
|
-
}
|
|
918
|
-
const fields = type.fields ?? [];
|
|
919
|
-
if (fields.length === 0) {
|
|
920
|
-
continue;
|
|
921
|
-
}
|
|
922
|
-
const fieldNames = fields.map((f) => `"${f.name.value}"`).join(" | ");
|
|
923
|
-
typePicks.push(` ${name}: ${fieldNames};`);
|
|
924
|
-
}
|
|
925
|
-
for (const union of unions) {
|
|
926
|
-
const types2 = union.types?.map((t) => t.name.value) ?? [];
|
|
927
|
-
if (unionTypesMap[union.name.value] == null) {
|
|
928
|
-
unionTypesMap[union.name.value] = [];
|
|
929
|
-
}
|
|
930
|
-
unionTypesMap[union.name.value]?.push(...types2);
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
for (const unionName in unionTypesMap) {
|
|
934
|
-
const types = unionTypesMap[unionName];
|
|
935
|
-
if (types == null || types.length === 0) {
|
|
936
|
-
unionPicks.push(` ${unionName}: never;`);
|
|
937
|
-
unionResults.push(` ${unionName}: null;`);
|
|
938
|
-
continue;
|
|
939
|
-
}
|
|
940
|
-
const typePicks2 = types.map((t) => {
|
|
941
|
-
const normalizedTypeName = baseVisitor.convertName(t, {
|
|
942
|
-
useTypesSuffix: true,
|
|
943
|
-
useTypesPrefix: true
|
|
944
|
-
});
|
|
945
|
-
return `Pick<${normalizedTypeName}, DefinedFieldsWithoutExtensions["${t}"] | "__typename">`;
|
|
946
|
-
});
|
|
947
|
-
const resultPicks = types.map((t) => `"${t}"`);
|
|
948
|
-
unionPicks.push(` ${unionName}: ${typePicks2.join(" | ")};`);
|
|
949
|
-
unionResults.push(` ${unionName}: ${resultPicks.join(" | ")} | null;`);
|
|
950
|
-
}
|
|
951
|
-
for (const interfaceName in interfacesTypesMap) {
|
|
952
|
-
const types = interfacesTypesMap[interfaceName];
|
|
953
|
-
if (types == null || types.length === 0) {
|
|
954
|
-
interfacePicks.push(` ${interfaceName}: never;`);
|
|
955
|
-
interfaceResults.push(` ${interfaceName}: null;`);
|
|
956
|
-
continue;
|
|
957
|
-
}
|
|
958
|
-
const typePicks2 = types.map((t) => {
|
|
959
|
-
const normalizedTypeName = baseVisitor.convertName(t, {
|
|
960
|
-
useTypesSuffix: true,
|
|
961
|
-
useTypesPrefix: true
|
|
962
|
-
});
|
|
963
|
-
return `Pick<${normalizedTypeName}, DefinedFieldsWithoutExtensions["${t}"] | "__typename">`;
|
|
964
|
-
});
|
|
965
|
-
const resultPicks = types.map((t) => `"${t}"`);
|
|
966
|
-
interfacePicks.push(` ${interfaceName}: ${typePicks2.join(" | ")};`);
|
|
967
|
-
interfaceResults.push(` ${interfaceName}: ${resultPicks.join(" | ")} | null;`);
|
|
968
|
-
}
|
|
969
|
-
const fieldsDefinition = `export type DefinedFieldsWithoutExtensions = {
|
|
970
|
-
${typePicks.join("\n")}
|
|
971
|
-
};`;
|
|
972
|
-
const unionsDefinition = `export type DefinedUnionsWithoutExtensions = {
|
|
973
|
-
${unionPicks.join("\n")}
|
|
974
|
-
};`;
|
|
975
|
-
const unionsResultsDefinition = `export type DefinedUnionsResults = {
|
|
976
|
-
${unionResults.join("\n")}
|
|
977
|
-
};`;
|
|
978
|
-
const interfacesDefinition = `export type DefinedInterfacesWithoutExtensions = {
|
|
979
|
-
${interfacePicks.join("\n")}
|
|
980
|
-
};`;
|
|
981
|
-
const interfacesResultsDefinition = `export type DefinedInterfacesResults = {
|
|
982
|
-
${interfaceResults.join(
|
|
983
|
-
"\n"
|
|
984
|
-
)}
|
|
985
|
-
};`;
|
|
986
|
-
const result = [
|
|
987
|
-
fieldsDefinition,
|
|
988
|
-
unionsDefinition,
|
|
989
|
-
unionsResultsDefinition,
|
|
990
|
-
interfacesDefinition,
|
|
991
|
-
interfacesResultsDefinition
|
|
992
|
-
].join("\n\n");
|
|
993
|
-
return `
|
|
994
|
-
${result}
|
|
995
|
-
`;
|
|
996
|
-
}
|
|
997
|
-
}
|
|
998
|
-
},
|
|
999
|
-
config: {
|
|
1000
|
-
...options.config,
|
|
1001
|
-
enumsAsTypes: true
|
|
1002
|
-
},
|
|
1003
|
-
schemaAst: options.schemaAst
|
|
1004
|
-
};
|
|
1005
|
-
const baseTypesFilename = basename(baseTypesPath).replace(/\.(js|ts|d.ts)$/, "") + (importExtension || "");
|
|
1006
|
-
const baseTypesDir = stripFilename(baseOutput.filename);
|
|
1007
|
-
const outputs = modules.map((moduleName) => {
|
|
1008
|
-
const filename = resolve(cwd, baseOutputDir, moduleName, options.presetConfig.filename);
|
|
1009
|
-
const dirpath = stripFilename(filename);
|
|
1010
|
-
const relativePath = relative(dirpath, baseTypesDir);
|
|
1011
|
-
const importPath = options.presetConfig.importBaseTypesFrom || normalize(join(relativePath, baseTypesFilename));
|
|
1012
|
-
const sources2 = sourcesByModuleMap[moduleName];
|
|
1013
|
-
const documents = sources2.map((source) => source.document);
|
|
1014
|
-
const moduleDocument = concatAST(documents);
|
|
1015
|
-
const shouldDeclare = filename.endsWith(".d.ts");
|
|
1016
|
-
return {
|
|
1017
|
-
filename,
|
|
1018
|
-
schema: options.schema,
|
|
1019
|
-
documents: [],
|
|
1020
|
-
plugins: [
|
|
1021
|
-
...options.plugins.filter((p) => typeof p === "object" && !!p.add),
|
|
1022
|
-
{
|
|
1023
|
-
"graphql-modules-plugin": {}
|
|
1024
|
-
}
|
|
1025
|
-
],
|
|
1026
|
-
pluginMap: {
|
|
1027
|
-
...options.pluginMap,
|
|
1028
|
-
"graphql-modules-plugin": {
|
|
1029
|
-
plugin: (schema) => buildModule(moduleName, moduleDocument, {
|
|
1030
|
-
importNamespace: importTypesNamespace,
|
|
1031
|
-
importPath,
|
|
1032
|
-
encapsulate: encapsulateModuleTypes || "namespace",
|
|
1033
|
-
requireRootResolvers,
|
|
1034
|
-
shouldDeclare,
|
|
1035
|
-
schema,
|
|
1036
|
-
baseVisitor,
|
|
1037
|
-
useGraphQLModules: false,
|
|
1038
|
-
fieldTypes,
|
|
1039
|
-
fieldArguments,
|
|
1040
|
-
extensionsPath,
|
|
1041
|
-
rootTypes: [
|
|
1042
|
-
schema.getQueryType()?.name || "",
|
|
1043
|
-
schema.getMutationType()?.name || "",
|
|
1044
|
-
schema.getSubscriptionType()?.name || ""
|
|
1045
|
-
].filter(Boolean)
|
|
1046
|
-
})
|
|
1047
|
-
}
|
|
1048
|
-
},
|
|
1049
|
-
config: options.config,
|
|
1050
|
-
schemaAst: options.schemaAst
|
|
1051
|
-
};
|
|
1052
|
-
});
|
|
1053
|
-
return [baseOutput].concat(outputs);
|
|
673
|
+
function createObject(keys, valueFn) {
|
|
674
|
+
const obj = {};
|
|
675
|
+
for (const key of keys) {
|
|
676
|
+
obj[key] = valueFn(key);
|
|
1054
677
|
}
|
|
1055
|
-
|
|
678
|
+
return obj;
|
|
679
|
+
}
|
|
1056
680
|
|
|
1057
681
|
// lib/codegen.ts
|
|
1058
682
|
async function generate(options) {
|
|
1059
|
-
const
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
683
|
+
const { outputSchema, outputSchemaAst } = await loadSchema(
|
|
684
|
+
options.schemas,
|
|
685
|
+
options.cwd,
|
|
686
|
+
options.loaders
|
|
687
|
+
);
|
|
688
|
+
const sources = getSourcesFromSchema(outputSchemaAst);
|
|
689
|
+
const sourcesByModule = groupSourcesByModule(sources, options.modulesDir);
|
|
690
|
+
const modules = Array.from(sourcesByModule.keys());
|
|
691
|
+
const globalDefinitions = createDefinitionsMapFromDocument(outputSchema);
|
|
692
|
+
const modulesDefinitions = createDefinitionsMapFromSources(sources);
|
|
693
|
+
const defaultScalars = ["ID", "Int", "Float", "String", "Boolean"];
|
|
694
|
+
const config = {
|
|
695
|
+
globalDefinitions,
|
|
696
|
+
withMaybe: false,
|
|
697
|
+
withOptional: false,
|
|
698
|
+
defaultScalars
|
|
1071
699
|
};
|
|
1072
|
-
const
|
|
1073
|
-
const
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
700
|
+
const fieldInfo = createFieldInfoMap(globalDefinitions, defaultScalars);
|
|
701
|
+
const typesContent = [
|
|
702
|
+
printTypesHeaders(),
|
|
703
|
+
printRootTypesFromMap(
|
|
704
|
+
config,
|
|
705
|
+
["Query", "Mutation", "Subscription"],
|
|
706
|
+
globalDefinitions.objectTypeMap
|
|
707
|
+
),
|
|
708
|
+
...printEnumTypesFromMap(globalDefinitions.enumTypeMap),
|
|
709
|
+
...printObjectTypeTypesFromMap(config, modulesDefinitions.objectTypeMap),
|
|
710
|
+
...printObjectTypeFieldsArgsFromMap(config, globalDefinitions.objectTypeMap),
|
|
711
|
+
...printInputObjectTypeTypesFromMap(config, globalDefinitions.inputObjectTypeMap),
|
|
712
|
+
...printInterfaceTypesFromMap(config, globalDefinitions.interfaceTypeMap),
|
|
713
|
+
...printUnionTypesFromMap(config, globalDefinitions.unionTypeMap)
|
|
714
|
+
].join("\n\n");
|
|
715
|
+
const utilityContent = [
|
|
716
|
+
printUtilityTypes(),
|
|
717
|
+
printBaseScalars(config),
|
|
718
|
+
printBaseObjectTypes(config)
|
|
719
|
+
].join("\n\n");
|
|
720
|
+
const files = [
|
|
721
|
+
{
|
|
722
|
+
filename: join2(options.typesDir, "types.ts"),
|
|
723
|
+
content: typesContent
|
|
1093
724
|
},
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
725
|
+
{
|
|
726
|
+
filename: join2(options.typesDir, "utility.ts"),
|
|
727
|
+
content: utilityContent
|
|
728
|
+
},
|
|
729
|
+
{
|
|
730
|
+
filename: join2(options.modulesDir, "index.ts"),
|
|
731
|
+
content: printAutoload({ importExtension: options.importExtension }, modules)
|
|
732
|
+
},
|
|
733
|
+
{
|
|
734
|
+
filename: join2(options.modulesDir, "types.ts"),
|
|
735
|
+
content: printTypesTemplate({
|
|
736
|
+
importExtension: options.importExtension,
|
|
737
|
+
typesDir: options.typesDir,
|
|
738
|
+
modulesDir: options.modulesDir
|
|
739
|
+
}),
|
|
740
|
+
options: {
|
|
741
|
+
allowOverwrite: false,
|
|
742
|
+
disableBiomeHeader: true,
|
|
743
|
+
disableEslintHeader: true,
|
|
744
|
+
disableGenerationNoticeHeader: true
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
filename: join2(options.modulesDir, "extensions.ts"),
|
|
749
|
+
content: printExtensionsTemplate(),
|
|
750
|
+
options: {
|
|
751
|
+
allowOverwrite: false,
|
|
752
|
+
disableBiomeHeader: true,
|
|
753
|
+
disableEslintHeader: true,
|
|
754
|
+
disableGenerationNoticeHeader: true
|
|
755
|
+
}
|
|
1104
756
|
}
|
|
1105
|
-
|
|
1106
|
-
const
|
|
1107
|
-
const
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
757
|
+
];
|
|
758
|
+
for (const module of modules) {
|
|
759
|
+
const sources2 = sourcesByModule.get(module);
|
|
760
|
+
const documents = sources2?.map((s) => s.document).filter((el) => el != null) ?? [];
|
|
761
|
+
if (documents.length === 0) continue;
|
|
762
|
+
const document = concatAST(documents);
|
|
763
|
+
const config2 = {
|
|
764
|
+
typesDir: options.typesDir,
|
|
765
|
+
fieldInfo,
|
|
766
|
+
importExtension: options.importExtension,
|
|
767
|
+
modulesDir: options.modulesDir,
|
|
768
|
+
registry: createModuleRegistry(document)
|
|
1114
769
|
};
|
|
1115
|
-
|
|
1116
|
-
|
|
770
|
+
files.push({
|
|
771
|
+
filename: `src/modules/${module}/${options.moduleDefinitionName}`,
|
|
772
|
+
content: [
|
|
773
|
+
printModuleImports(config2, module),
|
|
774
|
+
printModuleMetadata(module, document),
|
|
775
|
+
printBaetaModuleTypes(config2),
|
|
776
|
+
printModuleObjectTypeFields(config2),
|
|
777
|
+
printModuleBuilder(config2, module)
|
|
778
|
+
].join("\n\n")
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
return files;
|
|
1117
782
|
}
|
|
1118
783
|
|
|
1119
784
|
// index.ts
|
|
@@ -1133,7 +798,7 @@ function graphqlPlugin() {
|
|
|
1133
798
|
generate: async (ctx, next) => {
|
|
1134
799
|
const items = await generate(ctx.generatorOptions);
|
|
1135
800
|
for (const item of items) {
|
|
1136
|
-
ctx.fileManager.createAndAdd(item.filename, item.content, "graphql");
|
|
801
|
+
ctx.fileManager.createAndAdd(item.filename, item.content, "graphql", item.options);
|
|
1137
802
|
}
|
|
1138
803
|
return next();
|
|
1139
804
|
}
|