@baeta/plugin-graphql 0.1.2 → 0.1.4
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 +22 -0
- package/LICENSE +21 -0
- package/README.md +139 -0
- package/package.json +20 -15
- package/dist/index.cjs +0 -1180
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -5
package/dist/index.cjs
DELETED
|
@@ -1,1180 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }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
|
-
// index.ts
|
|
8
|
-
var _generatorsdk = require('@baeta/generator-sdk');
|
|
9
|
-
|
|
10
|
-
// lib/codegen.ts
|
|
11
|
-
var _utilpath = require('@baeta/util-path');
|
|
12
|
-
var _core = require('@graphql-codegen/core');
|
|
13
|
-
var _pluginhelpers = require('@graphql-codegen/plugin-helpers');
|
|
14
|
-
var _typescript = require('@graphql-codegen/typescript'); var typescriptPlugin = _interopRequireWildcard(_typescript);
|
|
15
|
-
|
|
16
|
-
// utils/cache.ts
|
|
17
|
-
function createCache() {
|
|
18
|
-
const cache = /* @__PURE__ */ new Map();
|
|
19
|
-
return function ensure(namespace, key, factory) {
|
|
20
|
-
const cacheKey = `${namespace}:${key}`;
|
|
21
|
-
const cachedValue = cache.get(cacheKey);
|
|
22
|
-
if (cachedValue) {
|
|
23
|
-
return cachedValue;
|
|
24
|
-
}
|
|
25
|
-
const value = factory();
|
|
26
|
-
cache.set(cacheKey, value);
|
|
27
|
-
return value;
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// utils/load.ts
|
|
32
|
-
|
|
33
|
-
var _graphqlfileloader = require('@graphql-tools/graphql-file-loader');
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
var _load = require('@graphql-tools/load');
|
|
37
|
-
var _graphql = require('graphql');
|
|
38
|
-
|
|
39
|
-
// utils/hash.ts
|
|
40
|
-
var _crypto = require('crypto');
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
function hashContent(content) {
|
|
44
|
-
return _crypto.createHash.call(void 0, "sha256").update(content).digest("hex");
|
|
45
|
-
}
|
|
46
|
-
function hashSchema(schema) {
|
|
47
|
-
return hashContent(_graphql.print.call(void 0, _pluginhelpers.getCachedDocumentNodeFromSchema.call(void 0, schema)));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// utils/load.ts
|
|
51
|
-
async function loadSchema(schemaPointerMap, cwd, extraLoaders = []) {
|
|
52
|
-
const outputSchemaAst = await _load.loadSchema.call(void 0, schemaPointerMap, {
|
|
53
|
-
loaders: [new (0, _graphqlfileloader.GraphQLFileLoader)(), ...extraLoaders],
|
|
54
|
-
cwd,
|
|
55
|
-
includeSources: true
|
|
56
|
-
});
|
|
57
|
-
const errors = _graphql.validateSchema.call(void 0, outputSchemaAst);
|
|
58
|
-
if (errors.length > 0) {
|
|
59
|
-
const messages = errors.map((e) => e.toString()).join("\n\n--------------------\n\n");
|
|
60
|
-
const subject = errors.length === 1 ? "error" : "errors";
|
|
61
|
-
throw new Error(`Invalid schema. Found ${errors.length} ${subject}:
|
|
62
|
-
|
|
63
|
-
${messages}`);
|
|
64
|
-
}
|
|
65
|
-
if (!outputSchemaAst.extensions) {
|
|
66
|
-
outputSchemaAst.extensions = {};
|
|
67
|
-
}
|
|
68
|
-
outputSchemaAst.extensions.hash = hashSchema(outputSchemaAst);
|
|
69
|
-
return {
|
|
70
|
-
outputSchemaAst,
|
|
71
|
-
outputSchema: _pluginhelpers.getCachedDocumentNodeFromSchema.call(void 0, outputSchemaAst)
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// utils/scalars.ts
|
|
76
|
-
|
|
77
|
-
var _visitorplugincommon = require('@graphql-codegen/visitor-plugin-common');
|
|
78
|
-
function buildScalarMap(map, cwd, typesPath) {
|
|
79
|
-
if (!map) {
|
|
80
|
-
return {};
|
|
81
|
-
}
|
|
82
|
-
const fixedMap = {};
|
|
83
|
-
for (const key in map) {
|
|
84
|
-
const mapped = _visitorplugincommon.parseMapper.call(void 0, map[key]);
|
|
85
|
-
if (!mapped.isExternal) {
|
|
86
|
-
fixedMap[key] = map[key];
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
if (_utilpath.isAbsolute.call(void 0, mapped.source)) {
|
|
90
|
-
fixedMap[key] = map[key];
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const scalarPath = _utilpath.joinSafe.call(void 0, cwd, mapped.source);
|
|
94
|
-
const relativePath = _utilpath.relative.call(void 0, _utilpath.dirname.call(void 0, typesPath), scalarPath);
|
|
95
|
-
if (mapped.default) {
|
|
96
|
-
fixedMap[key] = `${relativePath}#default`;
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
fixedMap[key] = `${relativePath}#${mapped.import}`;
|
|
100
|
-
}
|
|
101
|
-
return fixedMap;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// lib/context/index.ts
|
|
105
|
-
var context_exports = {};
|
|
106
|
-
__export(context_exports, {
|
|
107
|
-
default: () => context_default,
|
|
108
|
-
plugin: () => plugin
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
var plugin = async (schema, documents, config) => {
|
|
112
|
-
const prepend = [];
|
|
113
|
-
const mapper = _visitorplugincommon.parseMapper.call(void 0, config.contextType || "any");
|
|
114
|
-
if (mapper.isExternal && mapper.source) {
|
|
115
|
-
const identifier = mapper.default ? "ContextType" : `${mapper.import} as ContextType`;
|
|
116
|
-
const result = _visitorplugincommon.buildMapperImport.call(void 0,
|
|
117
|
-
mapper.source,
|
|
118
|
-
[
|
|
119
|
-
{
|
|
120
|
-
identifier,
|
|
121
|
-
asDefault: mapper.default
|
|
122
|
-
}
|
|
123
|
-
],
|
|
124
|
-
true
|
|
125
|
-
);
|
|
126
|
-
if (result) {
|
|
127
|
-
prepend.push(result);
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
prepend.push(`type ContextType = ${mapper.type}`);
|
|
131
|
-
}
|
|
132
|
-
prepend.push("export type { ContextType }");
|
|
133
|
-
return {
|
|
134
|
-
content: "",
|
|
135
|
-
prepend
|
|
136
|
-
};
|
|
137
|
-
};
|
|
138
|
-
var context_default = { plugin };
|
|
139
|
-
|
|
140
|
-
// lib/modules/index.ts
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
// lib/modules/builder.ts
|
|
149
|
-
var _changecaseall = require('change-case-all');
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
// lib/modules/hashes.ts
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
var _murmurhash = require('murmurhash'); var _murmurhash2 = _interopRequireDefault(_murmurhash);
|
|
163
|
-
function getObjectTypeHash(node) {
|
|
164
|
-
const fieldNames = buildFieldsNames(node.fields);
|
|
165
|
-
const typeFields = fieldNames.map(([fieldName, fieldType]) => `${fieldName}:${fieldType}`).join(",");
|
|
166
|
-
const fieldMap = {};
|
|
167
|
-
for (const [fieldName, fieldType] of fieldNames) {
|
|
168
|
-
fieldMap[fieldName] = _murmurhash2.default.v3(fieldType).toString(36);
|
|
169
|
-
}
|
|
170
|
-
return {
|
|
171
|
-
hash: _murmurhash2.default.v3(typeFields).toString(36),
|
|
172
|
-
fieldsHashes: fieldMap
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
function buildFieldsNames(fields = []) {
|
|
176
|
-
if (fields.length === 0) {
|
|
177
|
-
return [];
|
|
178
|
-
}
|
|
179
|
-
const fieldsNames = [];
|
|
180
|
-
for (const field of fields) {
|
|
181
|
-
const fieldName = field.name.value;
|
|
182
|
-
const fieldType = buildTypeName(field.type);
|
|
183
|
-
fieldsNames.push([fieldName, fieldType]);
|
|
184
|
-
}
|
|
185
|
-
return fieldsNames.sort(([a], [b]) => a.localeCompare(b));
|
|
186
|
-
}
|
|
187
|
-
function buildTypeName(node) {
|
|
188
|
-
if (node.kind === _graphql.Kind.NAMED_TYPE) {
|
|
189
|
-
return node.name.value;
|
|
190
|
-
}
|
|
191
|
-
if (node.kind === _graphql.Kind.LIST_TYPE) {
|
|
192
|
-
return `[${buildTypeName(node.type)}]`;
|
|
193
|
-
}
|
|
194
|
-
if (node.kind === _graphql.Kind.NON_NULL_TYPE) {
|
|
195
|
-
return `${buildTypeName(node.type)}!`;
|
|
196
|
-
}
|
|
197
|
-
return "";
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// lib/modules/utils.ts
|
|
201
|
-
var _os = require('os');
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
var _parsefilepath = require('parse-filepath'); var _parsefilepath2 = _interopRequireDefault(_parsefilepath);
|
|
208
|
-
var sep = "/";
|
|
209
|
-
function collectUsedTypes(doc) {
|
|
210
|
-
const used = [];
|
|
211
|
-
for (const definition of doc.definitions) {
|
|
212
|
-
findRelated(definition);
|
|
213
|
-
}
|
|
214
|
-
function markAsUsed(type) {
|
|
215
|
-
pushUnique(used, type);
|
|
216
|
-
}
|
|
217
|
-
function findRelated(node) {
|
|
218
|
-
if (node.kind === _graphql.Kind.OBJECT_TYPE_DEFINITION || node.kind === _graphql.Kind.OBJECT_TYPE_EXTENSION) {
|
|
219
|
-
markAsUsed(node.name.value);
|
|
220
|
-
if (node.fields) {
|
|
221
|
-
for (const field of node.fields) {
|
|
222
|
-
findRelated(field);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
if (node.interfaces) {
|
|
226
|
-
for (const _interface of node.interfaces) {
|
|
227
|
-
findRelated(_interface);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
} else if (node.kind === _graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION || node.kind === _graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION) {
|
|
231
|
-
markAsUsed(node.name.value);
|
|
232
|
-
if (node.fields) {
|
|
233
|
-
for (const field of node.fields) {
|
|
234
|
-
findRelated(field);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
} else if (node.kind === _graphql.Kind.INTERFACE_TYPE_DEFINITION || node.kind === _graphql.Kind.INTERFACE_TYPE_EXTENSION) {
|
|
238
|
-
markAsUsed(node.name.value);
|
|
239
|
-
if (node.fields) {
|
|
240
|
-
for (const field of node.fields) {
|
|
241
|
-
findRelated(field);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
if (node.interfaces) {
|
|
245
|
-
for (const _interface of node.interfaces) {
|
|
246
|
-
findRelated(_interface);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
} else if (node.kind === _graphql.Kind.UNION_TYPE_DEFINITION || node.kind === _graphql.Kind.UNION_TYPE_EXTENSION) {
|
|
250
|
-
markAsUsed(node.name.value);
|
|
251
|
-
if (node.types) {
|
|
252
|
-
for (const type of node.types) {
|
|
253
|
-
findRelated(type);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
} else if (node.kind === _graphql.Kind.ENUM_TYPE_DEFINITION || node.kind === _graphql.Kind.ENUM_TYPE_EXTENSION) {
|
|
257
|
-
markAsUsed(node.name.value);
|
|
258
|
-
} else if (node.kind === _graphql.Kind.SCALAR_TYPE_DEFINITION || node.kind === _graphql.Kind.SCALAR_TYPE_EXTENSION) {
|
|
259
|
-
if (!isGraphQLPrimitive(node.name.value)) {
|
|
260
|
-
markAsUsed(node.name.value);
|
|
261
|
-
}
|
|
262
|
-
} else if (node.kind === _graphql.Kind.INPUT_VALUE_DEFINITION) {
|
|
263
|
-
findRelated(resolveTypeNode(node.type));
|
|
264
|
-
} else if (node.kind === _graphql.Kind.FIELD_DEFINITION) {
|
|
265
|
-
findRelated(resolveTypeNode(node.type));
|
|
266
|
-
if (node.arguments) {
|
|
267
|
-
for (const argument of node.arguments) {
|
|
268
|
-
findRelated(argument);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
} else if (node.kind === _graphql.Kind.NAMED_TYPE && // Named type
|
|
272
|
-
!isGraphQLPrimitive(node.name.value)) {
|
|
273
|
-
markAsUsed(node.name.value);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return used;
|
|
277
|
-
}
|
|
278
|
-
function collectObjectFieldType(node, fieldDefinition, fieldsMap, fieldTypes) {
|
|
279
|
-
const objectName = node.name.value;
|
|
280
|
-
const fieldName = fieldDefinition.name.value;
|
|
281
|
-
const field = fieldsMap[fieldDefinition.name.value];
|
|
282
|
-
const baseName = _pluginhelpers.getBaseType.call(void 0, field.type).name;
|
|
283
|
-
const isDefaultScalar = _visitorplugincommon.DEFAULT_SCALARS[baseName] != null;
|
|
284
|
-
const name = isDefaultScalar ? `Types.Scalars["${baseName}"]["output"]` : baseName;
|
|
285
|
-
const type = _visitorplugincommon.wrapTypeWithModifiers.call(void 0, name, field.type, {
|
|
286
|
-
wrapOptional: (str) => {
|
|
287
|
-
return `Types.Maybe<${str}>`;
|
|
288
|
-
},
|
|
289
|
-
wrapArray: (str) => {
|
|
290
|
-
return `Array<${str}>`;
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
if (fieldTypes[objectName] == null) {
|
|
294
|
-
fieldTypes[objectName] = {};
|
|
295
|
-
}
|
|
296
|
-
fieldTypes[objectName][fieldName] = type;
|
|
297
|
-
}
|
|
298
|
-
function collectObjectFieldArguments(node, fieldDefinition, fieldArguments) {
|
|
299
|
-
const objectName = node.name.value;
|
|
300
|
-
const fieldName = fieldDefinition.name.value;
|
|
301
|
-
const hasArguments = fieldDefinition.arguments != null && fieldDefinition.arguments.length > 0;
|
|
302
|
-
if (fieldArguments[objectName] == null) {
|
|
303
|
-
fieldArguments[objectName] = {};
|
|
304
|
-
}
|
|
305
|
-
fieldArguments[objectName][fieldName] = hasArguments;
|
|
306
|
-
}
|
|
307
|
-
function collectObjectFieldTypesAndArguments(schema) {
|
|
308
|
-
const fieldTypes = {};
|
|
309
|
-
const fieldArguments = {};
|
|
310
|
-
if (!schema) {
|
|
311
|
-
return { fieldTypes, fieldArguments };
|
|
312
|
-
}
|
|
313
|
-
const schemaTypes = schema.getTypeMap();
|
|
314
|
-
for (const type of Object.values(schemaTypes)) {
|
|
315
|
-
if (_optionalChain([type, 'access', _ => _.astNode, 'optionalAccess', _2 => _2.kind]) !== _graphql.Kind.OBJECT_TYPE_DEFINITION) {
|
|
316
|
-
continue;
|
|
317
|
-
}
|
|
318
|
-
const schemaType = schemaTypes[type.name];
|
|
319
|
-
const fieldsMap = schemaType.getFields();
|
|
320
|
-
for (const field of _nullishCoalesce(type.astNode.fields, () => ( []))) {
|
|
321
|
-
collectObjectFieldArguments(type.astNode, field, fieldArguments);
|
|
322
|
-
collectObjectFieldType(type.astNode, field, fieldsMap, fieldTypes);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
return { fieldTypes, fieldArguments };
|
|
326
|
-
}
|
|
327
|
-
function resolveTypeNode(node) {
|
|
328
|
-
if (node.kind === _graphql.Kind.LIST_TYPE) {
|
|
329
|
-
return resolveTypeNode(node.type);
|
|
330
|
-
}
|
|
331
|
-
if (node.kind === _graphql.Kind.NON_NULL_TYPE) {
|
|
332
|
-
return resolveTypeNode(node.type);
|
|
333
|
-
}
|
|
334
|
-
return node;
|
|
335
|
-
}
|
|
336
|
-
function isGraphQLPrimitive(name) {
|
|
337
|
-
return ["String", "Boolean", "ID", "Float", "Int"].includes(name);
|
|
338
|
-
}
|
|
339
|
-
function unique(val, i, all) {
|
|
340
|
-
return i === all.indexOf(val);
|
|
341
|
-
}
|
|
342
|
-
function withQuotes(val) {
|
|
343
|
-
return `'${val}'`;
|
|
344
|
-
}
|
|
345
|
-
function indent(size) {
|
|
346
|
-
const space = new Array(size).fill(" ").join("");
|
|
347
|
-
function indentInner(val) {
|
|
348
|
-
return val.split("\n").map((line) => `${space}${line}`).join("\n");
|
|
349
|
-
}
|
|
350
|
-
return indentInner;
|
|
351
|
-
}
|
|
352
|
-
function buildBlock({ name, lines }) {
|
|
353
|
-
if (!lines.length) {
|
|
354
|
-
return "";
|
|
355
|
-
}
|
|
356
|
-
return [`${name} {`, ...lines.map(indent(2)), "};"].join("\n");
|
|
357
|
-
}
|
|
358
|
-
var getRelativePath = (filepath, basePath) => {
|
|
359
|
-
const normalizedFilepath = normalize(filepath);
|
|
360
|
-
const normalizedBasePath = ensureStartsWithSeparator(
|
|
361
|
-
normalize(ensureEndsWithSeparator(basePath))
|
|
362
|
-
);
|
|
363
|
-
const [, relativePath] = normalizedFilepath.split(normalizedBasePath);
|
|
364
|
-
return relativePath;
|
|
365
|
-
};
|
|
366
|
-
function groupSourcesByModule(sources, basePath) {
|
|
367
|
-
const grouped = {};
|
|
368
|
-
for (const source of sources) {
|
|
369
|
-
if (!source.location) {
|
|
370
|
-
continue;
|
|
371
|
-
}
|
|
372
|
-
const relativePath = getRelativePath(source.location, basePath);
|
|
373
|
-
if (relativePath) {
|
|
374
|
-
const mod = extractModuleDirectory(source.location, basePath);
|
|
375
|
-
if (!grouped[mod]) {
|
|
376
|
-
grouped[mod] = [];
|
|
377
|
-
}
|
|
378
|
-
grouped[mod].push(source);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
return grouped;
|
|
382
|
-
}
|
|
383
|
-
function extractModuleDirectory(filepath, basePath) {
|
|
384
|
-
const relativePath = getRelativePath(filepath, basePath);
|
|
385
|
-
const [moduleDirectory] = relativePath.split(sep);
|
|
386
|
-
return moduleDirectory;
|
|
387
|
-
}
|
|
388
|
-
function stripFilename(path) {
|
|
389
|
-
const parsedPath = _parsefilepath2.default.call(void 0, path);
|
|
390
|
-
return normalize(parsedPath.dir);
|
|
391
|
-
}
|
|
392
|
-
function normalize(path) {
|
|
393
|
-
return path.replace(/\\/g, "/");
|
|
394
|
-
}
|
|
395
|
-
function ensureEndsWithSeparator(path) {
|
|
396
|
-
return path.endsWith(sep) ? path : path + sep;
|
|
397
|
-
}
|
|
398
|
-
function ensureStartsWithSeparator(path) {
|
|
399
|
-
if (_os.platform.call(void 0, ) === "win32") {
|
|
400
|
-
return path;
|
|
401
|
-
}
|
|
402
|
-
return path.startsWith(".") ? path.replace(/^(..\/)|(.\/)/, "/") : path.startsWith("/") ? path : `/${path}`;
|
|
403
|
-
}
|
|
404
|
-
function pushUnique(list, item) {
|
|
405
|
-
if (!list.includes(item)) {
|
|
406
|
-
list.push(item);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
function concatByKey(left, right, key) {
|
|
410
|
-
return [.../* @__PURE__ */ new Set([...left[key], ...right[key]])];
|
|
411
|
-
}
|
|
412
|
-
function uniqueByKey(left, right, key) {
|
|
413
|
-
return left[key].filter((item) => !right[key].includes(item));
|
|
414
|
-
}
|
|
415
|
-
function createObject(keys, valueFn) {
|
|
416
|
-
const obj = {};
|
|
417
|
-
for (const key of keys) {
|
|
418
|
-
obj[key] = valueFn(key);
|
|
419
|
-
}
|
|
420
|
-
return obj;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// lib/modules/builder.ts
|
|
424
|
-
var registryKeys = [
|
|
425
|
-
"objects",
|
|
426
|
-
"inputs",
|
|
427
|
-
"interfaces",
|
|
428
|
-
"scalars",
|
|
429
|
-
"unions",
|
|
430
|
-
"enums"
|
|
431
|
-
];
|
|
432
|
-
function buildModule(name, doc, {
|
|
433
|
-
importNamespace,
|
|
434
|
-
importPath,
|
|
435
|
-
encapsulate,
|
|
436
|
-
requireRootResolvers,
|
|
437
|
-
shouldDeclare,
|
|
438
|
-
rootTypes,
|
|
439
|
-
schema,
|
|
440
|
-
baseVisitor,
|
|
441
|
-
fieldTypes,
|
|
442
|
-
fieldArguments,
|
|
443
|
-
extensionsPath
|
|
444
|
-
}) {
|
|
445
|
-
const picks = createObject(
|
|
446
|
-
registryKeys,
|
|
447
|
-
() => ({})
|
|
448
|
-
);
|
|
449
|
-
const defined = createObject(registryKeys, () => []);
|
|
450
|
-
const extended = createObject(registryKeys, () => []);
|
|
451
|
-
const hashes = {};
|
|
452
|
-
const usedTypes = collectUsedTypes(doc);
|
|
453
|
-
_graphql.visit.call(void 0, doc, {
|
|
454
|
-
ObjectTypeDefinition(node) {
|
|
455
|
-
collectTypeDefinition(node);
|
|
456
|
-
},
|
|
457
|
-
ObjectTypeExtension(node) {
|
|
458
|
-
collectTypeExtension(node);
|
|
459
|
-
},
|
|
460
|
-
InputObjectTypeDefinition(node) {
|
|
461
|
-
collectTypeDefinition(node);
|
|
462
|
-
},
|
|
463
|
-
InputObjectTypeExtension(node) {
|
|
464
|
-
collectTypeExtension(node);
|
|
465
|
-
},
|
|
466
|
-
InterfaceTypeDefinition(node) {
|
|
467
|
-
collectTypeDefinition(node);
|
|
468
|
-
},
|
|
469
|
-
InterfaceTypeExtension(node) {
|
|
470
|
-
collectTypeExtension(node);
|
|
471
|
-
},
|
|
472
|
-
ScalarTypeDefinition(node) {
|
|
473
|
-
collectTypeDefinition(node);
|
|
474
|
-
},
|
|
475
|
-
UnionTypeDefinition(node) {
|
|
476
|
-
collectTypeDefinition(node);
|
|
477
|
-
},
|
|
478
|
-
UnionTypeExtension(node) {
|
|
479
|
-
collectTypeExtension(node);
|
|
480
|
-
},
|
|
481
|
-
EnumTypeDefinition(node) {
|
|
482
|
-
collectTypeDefinition(node);
|
|
483
|
-
},
|
|
484
|
-
EnumTypeExtension(node) {
|
|
485
|
-
collectTypeExtension(node);
|
|
486
|
-
}
|
|
487
|
-
});
|
|
488
|
-
const visited = createObject(
|
|
489
|
-
registryKeys,
|
|
490
|
-
(key) => concatByKey(defined, extended, key)
|
|
491
|
-
);
|
|
492
|
-
const external = createObject(
|
|
493
|
-
registryKeys,
|
|
494
|
-
(key) => uniqueByKey(extended, defined, key)
|
|
495
|
-
);
|
|
496
|
-
const imports = [
|
|
497
|
-
`import * as ${importNamespace} from "${importPath}";`,
|
|
498
|
-
'import type { DocumentNode } from "graphql";',
|
|
499
|
-
'import * as Baeta from "@baeta/core/sdk";'
|
|
500
|
-
];
|
|
501
|
-
if (extensionsPath) {
|
|
502
|
-
imports.push(`import baetaExtensions from "${extensionsPath}";`);
|
|
503
|
-
}
|
|
504
|
-
imports.push("\n");
|
|
505
|
-
let content = [
|
|
506
|
-
printDefinedFields(),
|
|
507
|
-
printDefinedEnumValues(),
|
|
508
|
-
printDefinedInputFields(),
|
|
509
|
-
printSchemaTypes(usedTypes),
|
|
510
|
-
printMetadata()
|
|
511
|
-
].filter(Boolean).join("\n\n");
|
|
512
|
-
const moduleNamespace = baseVisitor.convertName(name, {
|
|
513
|
-
suffix: "Module",
|
|
514
|
-
useTypesPrefix: false,
|
|
515
|
-
useTypesSuffix: false
|
|
516
|
-
});
|
|
517
|
-
if (encapsulate === "namespace") {
|
|
518
|
-
content = `${shouldDeclare ? "declare" : "export"} namespace ${baseVisitor.convertName(name, {
|
|
519
|
-
suffix: "Module",
|
|
520
|
-
useTypesPrefix: false,
|
|
521
|
-
useTypesSuffix: false
|
|
522
|
-
})} {
|
|
523
|
-
${shouldDeclare ? `${indent(2)(imports.join("\n"))}
|
|
524
|
-
` : ""}${indent(2)(content)}
|
|
525
|
-
}`;
|
|
526
|
-
}
|
|
527
|
-
return [...shouldDeclare ? [] : imports, content, printFactoryMethod()].filter(Boolean).join("\n");
|
|
528
|
-
function printMetadata() {
|
|
529
|
-
return `export namespace ModuleMetadata {
|
|
530
|
-
export const id = '${name}';
|
|
531
|
-
export const dirname = './${name}';
|
|
532
|
-
export const hashes = ${JSON.stringify(hashes)};
|
|
533
|
-
export const typedef = ${JSON.stringify(doc)} as unknown as DocumentNode;
|
|
534
|
-
${printBaetaManager()}
|
|
535
|
-
}`;
|
|
536
|
-
}
|
|
537
|
-
function printDefinedFields() {
|
|
538
|
-
return buildBlock({
|
|
539
|
-
name: "interface DefinedFields",
|
|
540
|
-
lines: [...visited.objects, ...visited.interfaces].map(
|
|
541
|
-
(typeName) => `${typeName}: ${printPicks(typeName, {
|
|
542
|
-
...picks.objects,
|
|
543
|
-
...picks.interfaces
|
|
544
|
-
})};`
|
|
545
|
-
)
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
function printFactoryMethod() {
|
|
549
|
-
const name2 = moduleNamespace.slice(0, moduleNamespace.length - 6);
|
|
550
|
-
const createModuleFn = `create${name2}Module`;
|
|
551
|
-
const getModuleFn = `get${name2}Module`;
|
|
552
|
-
const extensionsArg = extensionsPath ? ", baetaExtensions" : "";
|
|
553
|
-
return `
|
|
554
|
-
export const ${createModuleFn} = () => Baeta.createModuleManager(ModuleMetadata${extensionsArg});
|
|
555
|
-
export const ${getModuleFn} = Baeta.createSingletonModule(${createModuleFn});
|
|
556
|
-
`;
|
|
557
|
-
}
|
|
558
|
-
function printObjectFieldResolverBuilder(typeName, field) {
|
|
559
|
-
const parentType = getParentType(typeName);
|
|
560
|
-
const resultType = getResultType(typeName, field);
|
|
561
|
-
const argumentsType = getArgsType(typeName, field);
|
|
562
|
-
const contextType = getContextType();
|
|
563
|
-
return `${field}: module.createResolverBuilder<${resultType}, ${parentType}, ${contextType}, ${argumentsType}>("${typeName}", "${field}"),`;
|
|
564
|
-
}
|
|
565
|
-
function printObjectResolverBuilder(typeName, objects) {
|
|
566
|
-
const fields = _nullishCoalesce(_optionalChain([objects, 'access', _3 => _3[typeName], 'optionalAccess', _4 => _4.filter, 'call', _5 => _5(unique), 'access', _6 => _6.map, 'call', _7 => _7((field) => printObjectFieldResolverBuilder(typeName, field))]), () => ( []));
|
|
567
|
-
if (fields.length === 0) {
|
|
568
|
-
return "";
|
|
569
|
-
}
|
|
570
|
-
const parentType = getParentType(typeName);
|
|
571
|
-
const contextType = getContextType();
|
|
572
|
-
const addons = [`...module.createTypeMethods<${parentType}, ${contextType}>("${typeName}"),`];
|
|
573
|
-
const contentBody = [...addons, ...fields].map(indent(2)).join("\n");
|
|
574
|
-
const content2 = `{
|
|
575
|
-
${contentBody}
|
|
576
|
-
}`;
|
|
577
|
-
return `${typeName}: ${content2},`;
|
|
578
|
-
}
|
|
579
|
-
function printSubscriptionFieldBuilder(field) {
|
|
580
|
-
const parentType = getParentType("Subscription");
|
|
581
|
-
const resultType = getResultType("Subscription", field);
|
|
582
|
-
const argumentsType = getArgsType("Subscription", field);
|
|
583
|
-
const contextType = getContextType();
|
|
584
|
-
return `${field}: module.createSubscriptionBuilder<${resultType}, ${parentType}, ${contextType}, ${argumentsType}>("${field}"),`;
|
|
585
|
-
}
|
|
586
|
-
function printSubscriptionObjectBuilder() {
|
|
587
|
-
const subscriptions = _nullishCoalesce(_optionalChain([picks, 'access', _8 => _8.objects, 'access', _9 => _9.Subscription, 'optionalAccess', _10 => _10.filter, 'call', _11 => _11(unique)]), () => ( []));
|
|
588
|
-
if (subscriptions.length === 0) {
|
|
589
|
-
return "";
|
|
590
|
-
}
|
|
591
|
-
const fields = subscriptions.map((subscription) => printSubscriptionFieldBuilder(subscription));
|
|
592
|
-
const parentType = getParentType("Subscription");
|
|
593
|
-
const contextType = getContextType();
|
|
594
|
-
const addons = [`...module.createSubscriptionMethods<${parentType}, ${contextType}>(),`];
|
|
595
|
-
const contentBody = [...addons, ...fields].map(indent(2)).join("\n");
|
|
596
|
-
const content2 = `{
|
|
597
|
-
${contentBody}
|
|
598
|
-
}`;
|
|
599
|
-
return `Subscription: ${content2},`;
|
|
600
|
-
}
|
|
601
|
-
function printScalarBuilder() {
|
|
602
|
-
const scalars = visited.scalars;
|
|
603
|
-
if (scalars.length === 0) {
|
|
604
|
-
return "";
|
|
605
|
-
}
|
|
606
|
-
const fields = scalars.map((scalar) => `${scalar}: module.createScalarBuilder("${scalar}"),`);
|
|
607
|
-
const content2 = fields.map(indent(2)).join("\n");
|
|
608
|
-
return `Scalar: {
|
|
609
|
-
${content2}
|
|
610
|
-
},`;
|
|
611
|
-
}
|
|
612
|
-
function printTypenameResolverBuilder(name2, resultNamespace, valueNamespace) {
|
|
613
|
-
const resultType = `${importNamespace}.${resultNamespace}["${name2}"]`;
|
|
614
|
-
const valueType = `${importNamespace}.${valueNamespace}["${name2}"]`;
|
|
615
|
-
const contextType = getContextType();
|
|
616
|
-
const resolver = `$resolveType: module.createResolveType<${resultType}, ${valueType}, ${contextType}>("${name2}"),`;
|
|
617
|
-
return `${name2}: {
|
|
618
|
-
${indent(2)(resolver)}
|
|
619
|
-
},`;
|
|
620
|
-
}
|
|
621
|
-
function printInterfaceBuilder() {
|
|
622
|
-
const interfaces = defined.interfaces;
|
|
623
|
-
if (interfaces.length === 0) {
|
|
624
|
-
return "";
|
|
625
|
-
}
|
|
626
|
-
return interfaces.map(
|
|
627
|
-
(interfaceName) => printTypenameResolverBuilder(
|
|
628
|
-
interfaceName,
|
|
629
|
-
"DefinedInterfacesResults",
|
|
630
|
-
"DefinedInterfacesWithoutExtensions"
|
|
631
|
-
)
|
|
632
|
-
).join("\n");
|
|
633
|
-
}
|
|
634
|
-
function printUnionBuilder() {
|
|
635
|
-
const unions = defined.unions;
|
|
636
|
-
if (unions.length === 0) {
|
|
637
|
-
return "";
|
|
638
|
-
}
|
|
639
|
-
return unions.map(
|
|
640
|
-
(unionName) => printTypenameResolverBuilder(
|
|
641
|
-
unionName,
|
|
642
|
-
"DefinedUnionsResults",
|
|
643
|
-
"DefinedUnionsWithoutExtensions"
|
|
644
|
-
)
|
|
645
|
-
).join("\n");
|
|
646
|
-
}
|
|
647
|
-
function printBaetaManager() {
|
|
648
|
-
const objects = visited.objects.filter((type) => type !== "Subscription").map((typeName) => printObjectResolverBuilder(typeName, picks.objects)).filter(Boolean);
|
|
649
|
-
const contextType = getContextType();
|
|
650
|
-
const addons = [`...module.createModuleMethods<${contextType}>(),`];
|
|
651
|
-
const bodyFields = [
|
|
652
|
-
...addons,
|
|
653
|
-
...objects,
|
|
654
|
-
printScalarBuilder(),
|
|
655
|
-
printSubscriptionObjectBuilder(),
|
|
656
|
-
printInterfaceBuilder(),
|
|
657
|
-
printUnionBuilder()
|
|
658
|
-
];
|
|
659
|
-
const body = bodyFields.filter(Boolean).map(indent(6)).join("\n");
|
|
660
|
-
const content2 = `{
|
|
661
|
-
${body}
|
|
662
|
-
}`;
|
|
663
|
-
return `
|
|
664
|
-
export function createManager(module: Baeta.ModuleBuilder) {
|
|
665
|
-
return ${content2};
|
|
666
|
-
}`;
|
|
667
|
-
}
|
|
668
|
-
function printDefinedEnumValues() {
|
|
669
|
-
return buildBlock({
|
|
670
|
-
name: "interface DefinedEnumValues",
|
|
671
|
-
lines: visited.enums.map((typeName) => `${typeName}: ${printPicks(typeName, picks.enums)};`)
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
|
-
function encapsulateTypeName(typeName) {
|
|
675
|
-
if (encapsulate === "prefix") {
|
|
676
|
-
return `${_changecaseall.pascalCase.call(void 0, name)}_${typeName}`;
|
|
677
|
-
}
|
|
678
|
-
return typeName;
|
|
679
|
-
}
|
|
680
|
-
function printDefinedInputFields() {
|
|
681
|
-
return buildBlock({
|
|
682
|
-
name: "interface DefinedInputFields",
|
|
683
|
-
lines: visited.inputs.map(
|
|
684
|
-
(typeName) => `${typeName}: ${printPicks(typeName, picks.inputs)};`
|
|
685
|
-
)
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
function printSchemaTypes(types) {
|
|
689
|
-
return types.filter((type) => !visited.scalars.includes(type)).map(printExportType).join("\n");
|
|
690
|
-
}
|
|
691
|
-
function printPicks(typeName, records) {
|
|
692
|
-
return records[typeName].filter(unique).map(withQuotes).join(" | ");
|
|
693
|
-
}
|
|
694
|
-
function printTypeBody(typeName) {
|
|
695
|
-
const normalizedTypeName = baseVisitor.convertName(typeName, {
|
|
696
|
-
useTypesSuffix: true,
|
|
697
|
-
useTypesPrefix: true
|
|
698
|
-
});
|
|
699
|
-
const coreType = `${importNamespace}.${normalizedTypeName}`;
|
|
700
|
-
if (external.enums.includes(typeName) || external.objects.includes(typeName)) {
|
|
701
|
-
if (schema && _graphql.isScalarType.call(void 0, schema.getType(typeName))) {
|
|
702
|
-
return `${importNamespace}.Scalars['${typeName}']`;
|
|
703
|
-
}
|
|
704
|
-
return `Pick<${coreType}, ${importNamespace}.DefinedFieldsWithoutExtensions["${typeName}"]>`;
|
|
705
|
-
}
|
|
706
|
-
if (external.unions.includes(typeName)) {
|
|
707
|
-
return `${importNamespace}.DefinedUnionsWithoutExtensions["${typeName}"]`;
|
|
708
|
-
}
|
|
709
|
-
if (external.interfaces.includes(typeName)) {
|
|
710
|
-
return `Pick<${coreType}, ${importNamespace}.DefinedFieldsWithoutExtensions["${typeName}"]>`;
|
|
711
|
-
}
|
|
712
|
-
if (defined.enums.includes(typeName) && picks.enums[typeName]) {
|
|
713
|
-
return `DefinedEnumValues['${typeName}']`;
|
|
714
|
-
}
|
|
715
|
-
if (defined.unions.includes(typeName) && picks.unions[typeName]) {
|
|
716
|
-
return `${importNamespace}.DefinedUnionsWithoutExtensions["${typeName}"]`;
|
|
717
|
-
}
|
|
718
|
-
if (defined.objects.includes(typeName) && picks.objects[typeName]) {
|
|
719
|
-
return `Pick<${coreType}, DefinedFields['${typeName}']>`;
|
|
720
|
-
}
|
|
721
|
-
if (defined.interfaces.includes(typeName) && picks.interfaces[typeName]) {
|
|
722
|
-
return `${importNamespace}.DefinedInterfacesWithoutExtensions["${typeName}"]`;
|
|
723
|
-
}
|
|
724
|
-
if (defined.inputs.includes(typeName) && picks.inputs[typeName]) {
|
|
725
|
-
return `Pick<${coreType}, DefinedInputFields['${typeName}']>`;
|
|
726
|
-
}
|
|
727
|
-
if (_graphql.isScalarType.call(void 0, _optionalChain([schema, 'optionalAccess', _12 => _12.getType, 'call', _13 => _13(typeName)]))) {
|
|
728
|
-
return coreType;
|
|
729
|
-
}
|
|
730
|
-
if (_graphql.isInterfaceType.call(void 0, _optionalChain([schema, 'optionalAccess', _14 => _14.getType, 'call', _15 => _15(typeName)]))) {
|
|
731
|
-
return `${importNamespace}.DefinedInterfacesWithoutExtensions["${typeName}"]`;
|
|
732
|
-
}
|
|
733
|
-
if (_graphql.isUnionType.call(void 0, _optionalChain([schema, 'optionalAccess', _16 => _16.getType, 'call', _17 => _17(typeName)]))) {
|
|
734
|
-
return `${importNamespace}.DefinedUnionsWithoutExtensions["${typeName}"]`;
|
|
735
|
-
}
|
|
736
|
-
return `Pick<${coreType}, ${importNamespace}.DefinedFieldsWithoutExtensions["${typeName}"]>`;
|
|
737
|
-
}
|
|
738
|
-
function printExportType(typeName) {
|
|
739
|
-
return `export type ${encapsulateTypeName(typeName)} = ${printTypeBody(typeName)};`;
|
|
740
|
-
}
|
|
741
|
-
function collectFields(node, picksObj) {
|
|
742
|
-
const name2 = node.name.value;
|
|
743
|
-
if (node.fields) {
|
|
744
|
-
if (!picksObj[name2]) {
|
|
745
|
-
picksObj[name2] = [];
|
|
746
|
-
}
|
|
747
|
-
for (const field of node.fields) {
|
|
748
|
-
picksObj[name2].push(field.name.value);
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
function collectValuesFromEnum(node) {
|
|
753
|
-
const name2 = node.name.value;
|
|
754
|
-
if (node.values) {
|
|
755
|
-
if (!picks.enums[name2]) {
|
|
756
|
-
picks.enums[name2] = [];
|
|
757
|
-
}
|
|
758
|
-
for (const field of node.values) {
|
|
759
|
-
picks.enums[name2].push(field.name.value);
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
function collectUnionTypes(node) {
|
|
764
|
-
const name2 = node.name.value;
|
|
765
|
-
if (node.types) {
|
|
766
|
-
if (!picks.unions[name2]) {
|
|
767
|
-
picks.unions[name2] = [];
|
|
768
|
-
}
|
|
769
|
-
for (const type of node.types) {
|
|
770
|
-
picks.unions[name2].push(type.name.value);
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
function collectTypeDefinition(node) {
|
|
775
|
-
const name2 = node.name.value;
|
|
776
|
-
switch (node.kind) {
|
|
777
|
-
case _graphql.Kind.OBJECT_TYPE_DEFINITION: {
|
|
778
|
-
defined.objects.push(name2);
|
|
779
|
-
collectFields(node, picks.objects);
|
|
780
|
-
hashes[name2] = getObjectTypeHash(node);
|
|
781
|
-
break;
|
|
782
|
-
}
|
|
783
|
-
case _graphql.Kind.ENUM_TYPE_DEFINITION: {
|
|
784
|
-
defined.enums.push(name2);
|
|
785
|
-
collectValuesFromEnum(node);
|
|
786
|
-
break;
|
|
787
|
-
}
|
|
788
|
-
case _graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION: {
|
|
789
|
-
defined.inputs.push(name2);
|
|
790
|
-
collectFields(node, picks.inputs);
|
|
791
|
-
break;
|
|
792
|
-
}
|
|
793
|
-
case _graphql.Kind.SCALAR_TYPE_DEFINITION: {
|
|
794
|
-
defined.scalars.push(name2);
|
|
795
|
-
break;
|
|
796
|
-
}
|
|
797
|
-
case _graphql.Kind.INTERFACE_TYPE_DEFINITION: {
|
|
798
|
-
defined.interfaces.push(name2);
|
|
799
|
-
collectFields(node, picks.interfaces);
|
|
800
|
-
break;
|
|
801
|
-
}
|
|
802
|
-
case _graphql.Kind.UNION_TYPE_DEFINITION: {
|
|
803
|
-
defined.unions.push(name2);
|
|
804
|
-
collectUnionTypes(node);
|
|
805
|
-
break;
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
function collectTypeExtension(node) {
|
|
810
|
-
const name2 = node.name.value;
|
|
811
|
-
switch (node.kind) {
|
|
812
|
-
case _graphql.Kind.OBJECT_TYPE_EXTENSION: {
|
|
813
|
-
collectFields(node, picks.objects);
|
|
814
|
-
hashes[name2] = getObjectTypeHash(node);
|
|
815
|
-
if (rootTypes.includes(name2)) {
|
|
816
|
-
pushUnique(defined.objects, name2);
|
|
817
|
-
return;
|
|
818
|
-
}
|
|
819
|
-
pushUnique(extended.objects, name2);
|
|
820
|
-
break;
|
|
821
|
-
}
|
|
822
|
-
case _graphql.Kind.ENUM_TYPE_EXTENSION: {
|
|
823
|
-
collectValuesFromEnum(node);
|
|
824
|
-
pushUnique(extended.enums, name2);
|
|
825
|
-
break;
|
|
826
|
-
}
|
|
827
|
-
case _graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION: {
|
|
828
|
-
collectFields(node, picks.inputs);
|
|
829
|
-
pushUnique(extended.inputs, name2);
|
|
830
|
-
break;
|
|
831
|
-
}
|
|
832
|
-
case _graphql.Kind.INTERFACE_TYPE_EXTENSION: {
|
|
833
|
-
collectFields(node, picks.interfaces);
|
|
834
|
-
pushUnique(extended.interfaces, name2);
|
|
835
|
-
break;
|
|
836
|
-
}
|
|
837
|
-
case _graphql.Kind.UNION_TYPE_EXTENSION: {
|
|
838
|
-
pushUnique(extended.unions, name2);
|
|
839
|
-
break;
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
function getParentType(type) {
|
|
844
|
-
if (["Query", "Mutation", "Subscription"].includes(type)) {
|
|
845
|
-
return "{ }";
|
|
846
|
-
}
|
|
847
|
-
return type;
|
|
848
|
-
}
|
|
849
|
-
function getResultType(type, field) {
|
|
850
|
-
return _optionalChain([fieldTypes, 'optionalAccess', _18 => _18[type], 'optionalAccess', _19 => _19[field]]) || "{ }";
|
|
851
|
-
}
|
|
852
|
-
function getArgsType(type, field) {
|
|
853
|
-
const hasArgs = _nullishCoalesce(_optionalChain([fieldArguments, 'optionalAccess', _20 => _20[type], 'optionalAccess', _21 => _21[field]]), () => ( false));
|
|
854
|
-
if (!hasArgs) {
|
|
855
|
-
return "{ }";
|
|
856
|
-
}
|
|
857
|
-
const fieldUpper = field[0].toUpperCase() + field.slice(1);
|
|
858
|
-
return `Types.${type}${fieldUpper}Args`;
|
|
859
|
-
}
|
|
860
|
-
function getContextType() {
|
|
861
|
-
return "Types.ContextType";
|
|
862
|
-
}
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
// lib/modules/index.ts
|
|
866
|
-
var preset = {
|
|
867
|
-
buildGeneratesSection: (options) => {
|
|
868
|
-
const { baseOutputDir } = options;
|
|
869
|
-
const { baseTypesPath, extensionsPath, encapsulateModuleTypes, importExtension } = options.presetConfig;
|
|
870
|
-
const requireRootResolvers = _visitorplugincommon.getConfigValue.call(void 0, _optionalChain([options, 'optionalAccess', _22 => _22.presetConfig, 'access', _23 => _23.requireRootResolvers]), false);
|
|
871
|
-
const cwd = _utilpath.resolve.call(void 0, options.presetConfig.cwd || process.cwd());
|
|
872
|
-
const importTypesNamespace = options.presetConfig.importTypesNamespace || "Types";
|
|
873
|
-
if (!baseTypesPath) {
|
|
874
|
-
throw new Error(
|
|
875
|
-
`Preset "graphql-modules" requires you to specify "baseTypesPath" configuration and point it to your base types file (generated by "typescript" plugin)!`
|
|
876
|
-
);
|
|
877
|
-
}
|
|
878
|
-
if (!_optionalChain([options, 'access', _24 => _24.schemaAst, 'optionalAccess', _25 => _25.extensions, 'access', _26 => _26.sources])) {
|
|
879
|
-
throw new Error(`Preset "graphql-modules" requires to use GraphQL SDL`);
|
|
880
|
-
}
|
|
881
|
-
const extensions = _optionalChain([options, 'access', _27 => _27.schemaAst, 'optionalAccess', _28 => _28.extensions]);
|
|
882
|
-
const sources = _nullishCoalesce(_optionalChain([extensions, 'optionalAccess', _29 => _29.extendedSources]), () => ( []));
|
|
883
|
-
const sourcesByModuleMap = groupSourcesByModule(sources, baseOutputDir);
|
|
884
|
-
const modules = Object.keys(sourcesByModuleMap);
|
|
885
|
-
const baseVisitor = new (0, _visitorplugincommon.BaseVisitor)(options.config, {});
|
|
886
|
-
const { fieldTypes, fieldArguments } = collectObjectFieldTypesAndArguments(options.schemaAst);
|
|
887
|
-
const baseOutput = {
|
|
888
|
-
filename: _utilpath.resolve.call(void 0, cwd, baseOutputDir, baseTypesPath),
|
|
889
|
-
schema: options.schema,
|
|
890
|
-
documents: options.documents,
|
|
891
|
-
plugins: [
|
|
892
|
-
...options.plugins,
|
|
893
|
-
{
|
|
894
|
-
"modules-exported-scalars": {}
|
|
895
|
-
},
|
|
896
|
-
{
|
|
897
|
-
"modules-exported-picks": {}
|
|
898
|
-
}
|
|
899
|
-
],
|
|
900
|
-
pluginMap: {
|
|
901
|
-
...options.pluginMap,
|
|
902
|
-
"modules-exported-scalars": {
|
|
903
|
-
plugin: (schema) => {
|
|
904
|
-
const typeMap = schema.getTypeMap();
|
|
905
|
-
return Object.keys(typeMap).map((t) => {
|
|
906
|
-
if (t && typeMap[t] && _graphql.isScalarType.call(void 0, typeMap[t]) && !isGraphQLPrimitive(t)) {
|
|
907
|
-
const convertedName = baseVisitor.convertName(t);
|
|
908
|
-
return `export type ${convertedName} = Scalars["${t}"];`;
|
|
909
|
-
}
|
|
910
|
-
return null;
|
|
911
|
-
}).filter(Boolean).join("\n");
|
|
912
|
-
}
|
|
913
|
-
},
|
|
914
|
-
"modules-exported-picks": {
|
|
915
|
-
plugin: (schema) => {
|
|
916
|
-
const typePicks = [];
|
|
917
|
-
const unionPicks = [];
|
|
918
|
-
const unionResults = [];
|
|
919
|
-
const interfacePicks = [];
|
|
920
|
-
const interfaceResults = [];
|
|
921
|
-
const unionTypesMap = {};
|
|
922
|
-
const interfacesTypesMap = {};
|
|
923
|
-
for (const moduleName of modules) {
|
|
924
|
-
const sources2 = sourcesByModuleMap[moduleName];
|
|
925
|
-
const documents = sources2.map((source) => source.document);
|
|
926
|
-
const moduleDocument = _graphql.concatAST.call(void 0, documents);
|
|
927
|
-
const types = moduleDocument.definitions.filter(
|
|
928
|
-
(def) => def.kind === "ObjectTypeDefinition"
|
|
929
|
-
);
|
|
930
|
-
const unionsDefinition2 = moduleDocument.definitions.filter(
|
|
931
|
-
(def) => def.kind === "UnionTypeDefinition"
|
|
932
|
-
);
|
|
933
|
-
const unionsExtensions = moduleDocument.definitions.filter(
|
|
934
|
-
(def) => def.kind === "UnionTypeExtension"
|
|
935
|
-
);
|
|
936
|
-
const unions = [...unionsDefinition2, ...unionsExtensions];
|
|
937
|
-
for (const type of types) {
|
|
938
|
-
const name = type.name.value;
|
|
939
|
-
for (const interfaceNode of _nullishCoalesce(type.interfaces, () => ( []))) {
|
|
940
|
-
const interfaceName = interfaceNode.name.value;
|
|
941
|
-
if (interfacesTypesMap[interfaceName] == null) {
|
|
942
|
-
interfacesTypesMap[interfaceName] = [];
|
|
943
|
-
}
|
|
944
|
-
_optionalChain([interfacesTypesMap, 'access', _30 => _30[interfaceName], 'optionalAccess', _31 => _31.push, 'call', _32 => _32(name)]);
|
|
945
|
-
}
|
|
946
|
-
if (name === "Query" || name === "Mutation" || name === "Subscription") {
|
|
947
|
-
continue;
|
|
948
|
-
}
|
|
949
|
-
const fields = _nullishCoalesce(type.fields, () => ( []));
|
|
950
|
-
if (fields.length === 0) {
|
|
951
|
-
continue;
|
|
952
|
-
}
|
|
953
|
-
const fieldNames = fields.map((f) => `"${f.name.value}"`).join(" | ");
|
|
954
|
-
typePicks.push(` ${name}: ${fieldNames};`);
|
|
955
|
-
}
|
|
956
|
-
for (const union of unions) {
|
|
957
|
-
const types2 = _nullishCoalesce(_optionalChain([union, 'access', _33 => _33.types, 'optionalAccess', _34 => _34.map, 'call', _35 => _35((t) => t.name.value)]), () => ( []));
|
|
958
|
-
if (unionTypesMap[union.name.value] == null) {
|
|
959
|
-
unionTypesMap[union.name.value] = [];
|
|
960
|
-
}
|
|
961
|
-
_optionalChain([unionTypesMap, 'access', _36 => _36[union.name.value], 'optionalAccess', _37 => _37.push, 'call', _38 => _38(...types2)]);
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
for (const unionName in unionTypesMap) {
|
|
965
|
-
const types = unionTypesMap[unionName];
|
|
966
|
-
if (types == null || types.length === 0) {
|
|
967
|
-
unionPicks.push(` ${unionName}: never;`);
|
|
968
|
-
unionResults.push(` ${unionName}: null;`);
|
|
969
|
-
continue;
|
|
970
|
-
}
|
|
971
|
-
const typePicks2 = types.map((t) => {
|
|
972
|
-
const normalizedTypeName = baseVisitor.convertName(t, {
|
|
973
|
-
useTypesSuffix: true,
|
|
974
|
-
useTypesPrefix: true
|
|
975
|
-
});
|
|
976
|
-
return `Pick<${normalizedTypeName}, DefinedFieldsWithoutExtensions["${t}"] | "__typename">`;
|
|
977
|
-
});
|
|
978
|
-
const resultPicks = types.map((t) => `"${t}"`);
|
|
979
|
-
unionPicks.push(` ${unionName}: ${typePicks2.join(" | ")};`);
|
|
980
|
-
unionResults.push(` ${unionName}: ${resultPicks.join(" | ")} | null;`);
|
|
981
|
-
}
|
|
982
|
-
for (const interfaceName in interfacesTypesMap) {
|
|
983
|
-
const types = interfacesTypesMap[interfaceName];
|
|
984
|
-
if (types == null || types.length === 0) {
|
|
985
|
-
interfacePicks.push(` ${interfaceName}: never;`);
|
|
986
|
-
interfaceResults.push(` ${interfaceName}: null;`);
|
|
987
|
-
continue;
|
|
988
|
-
}
|
|
989
|
-
const typePicks2 = types.map((t) => {
|
|
990
|
-
const normalizedTypeName = baseVisitor.convertName(t, {
|
|
991
|
-
useTypesSuffix: true,
|
|
992
|
-
useTypesPrefix: true
|
|
993
|
-
});
|
|
994
|
-
return `Pick<${normalizedTypeName}, DefinedFieldsWithoutExtensions["${t}"] | "__typename">`;
|
|
995
|
-
});
|
|
996
|
-
const resultPicks = types.map((t) => `"${t}"`);
|
|
997
|
-
interfacePicks.push(` ${interfaceName}: ${typePicks2.join(" | ")};`);
|
|
998
|
-
interfaceResults.push(` ${interfaceName}: ${resultPicks.join(" | ")} | null;`);
|
|
999
|
-
}
|
|
1000
|
-
const fieldsDefinition = `export type DefinedFieldsWithoutExtensions = {
|
|
1001
|
-
${typePicks.join("\n")}
|
|
1002
|
-
};`;
|
|
1003
|
-
const unionsDefinition = `export type DefinedUnionsWithoutExtensions = {
|
|
1004
|
-
${unionPicks.join("\n")}
|
|
1005
|
-
};`;
|
|
1006
|
-
const unionsResultsDefinition = `export type DefinedUnionsResults = {
|
|
1007
|
-
${unionResults.join("\n")}
|
|
1008
|
-
};`;
|
|
1009
|
-
const interfacesDefinition = `export type DefinedInterfacesWithoutExtensions = {
|
|
1010
|
-
${interfacePicks.join("\n")}
|
|
1011
|
-
};`;
|
|
1012
|
-
const interfacesResultsDefinition = `export type DefinedInterfacesResults = {
|
|
1013
|
-
${interfaceResults.join(
|
|
1014
|
-
"\n"
|
|
1015
|
-
)}
|
|
1016
|
-
};`;
|
|
1017
|
-
const result = [
|
|
1018
|
-
fieldsDefinition,
|
|
1019
|
-
unionsDefinition,
|
|
1020
|
-
unionsResultsDefinition,
|
|
1021
|
-
interfacesDefinition,
|
|
1022
|
-
interfacesResultsDefinition
|
|
1023
|
-
].join("\n\n");
|
|
1024
|
-
return `
|
|
1025
|
-
${result}
|
|
1026
|
-
`;
|
|
1027
|
-
}
|
|
1028
|
-
}
|
|
1029
|
-
},
|
|
1030
|
-
config: {
|
|
1031
|
-
...options.config,
|
|
1032
|
-
enumsAsTypes: true
|
|
1033
|
-
},
|
|
1034
|
-
schemaAst: options.schemaAst
|
|
1035
|
-
};
|
|
1036
|
-
const baseTypesFilename = _utilpath.basename.call(void 0, baseTypesPath).replace(/\.(js|ts|d.ts)$/, "") + (importExtension || "");
|
|
1037
|
-
const baseTypesDir = stripFilename(baseOutput.filename);
|
|
1038
|
-
const outputs = modules.map((moduleName) => {
|
|
1039
|
-
const filename = _utilpath.resolve.call(void 0, cwd, baseOutputDir, moduleName, options.presetConfig.filename);
|
|
1040
|
-
const dirpath = stripFilename(filename);
|
|
1041
|
-
const relativePath = _utilpath.relative.call(void 0, dirpath, baseTypesDir);
|
|
1042
|
-
const importPath = options.presetConfig.importBaseTypesFrom || normalize(_utilpath.join.call(void 0, relativePath, baseTypesFilename));
|
|
1043
|
-
const sources2 = sourcesByModuleMap[moduleName];
|
|
1044
|
-
const documents = sources2.map((source) => source.document);
|
|
1045
|
-
const moduleDocument = _graphql.concatAST.call(void 0, documents);
|
|
1046
|
-
const shouldDeclare = filename.endsWith(".d.ts");
|
|
1047
|
-
return {
|
|
1048
|
-
filename,
|
|
1049
|
-
schema: options.schema,
|
|
1050
|
-
documents: [],
|
|
1051
|
-
plugins: [
|
|
1052
|
-
...options.plugins.filter((p) => typeof p === "object" && !!p.add),
|
|
1053
|
-
{
|
|
1054
|
-
"graphql-modules-plugin": {}
|
|
1055
|
-
}
|
|
1056
|
-
],
|
|
1057
|
-
pluginMap: {
|
|
1058
|
-
...options.pluginMap,
|
|
1059
|
-
"graphql-modules-plugin": {
|
|
1060
|
-
plugin: (schema) => buildModule(moduleName, moduleDocument, {
|
|
1061
|
-
importNamespace: importTypesNamespace,
|
|
1062
|
-
importPath,
|
|
1063
|
-
encapsulate: encapsulateModuleTypes || "namespace",
|
|
1064
|
-
requireRootResolvers,
|
|
1065
|
-
shouldDeclare,
|
|
1066
|
-
schema,
|
|
1067
|
-
baseVisitor,
|
|
1068
|
-
useGraphQLModules: false,
|
|
1069
|
-
fieldTypes,
|
|
1070
|
-
fieldArguments,
|
|
1071
|
-
extensionsPath,
|
|
1072
|
-
rootTypes: [
|
|
1073
|
-
_optionalChain([schema, 'access', _39 => _39.getQueryType, 'call', _40 => _40(), 'optionalAccess', _41 => _41.name]) || "",
|
|
1074
|
-
_optionalChain([schema, 'access', _42 => _42.getMutationType, 'call', _43 => _43(), 'optionalAccess', _44 => _44.name]) || "",
|
|
1075
|
-
_optionalChain([schema, 'access', _45 => _45.getSubscriptionType, 'call', _46 => _46(), 'optionalAccess', _47 => _47.name]) || ""
|
|
1076
|
-
].filter(Boolean)
|
|
1077
|
-
})
|
|
1078
|
-
}
|
|
1079
|
-
},
|
|
1080
|
-
config: options.config,
|
|
1081
|
-
schemaAst: options.schemaAst
|
|
1082
|
-
};
|
|
1083
|
-
});
|
|
1084
|
-
return [baseOutput].concat(outputs);
|
|
1085
|
-
}
|
|
1086
|
-
};
|
|
1087
|
-
|
|
1088
|
-
// lib/codegen.ts
|
|
1089
|
-
async function generate(options) {
|
|
1090
|
-
const rootConfig = {
|
|
1091
|
-
schemas: _pluginhelpers.normalizeInstanceOrArray.call(void 0, options.schemas),
|
|
1092
|
-
modulesDir: options.modulesDir,
|
|
1093
|
-
baseTypesPath: options.baseTypesPath,
|
|
1094
|
-
contextType: options.contextType,
|
|
1095
|
-
moduleDefinitionName: options.moduleDefinitionName,
|
|
1096
|
-
scalars: options.scalars,
|
|
1097
|
-
plugins: _pluginhelpers.normalizeConfig.call(void 0, ["typescript", "context"]),
|
|
1098
|
-
pluginMap: {
|
|
1099
|
-
typescript: typescriptPlugin,
|
|
1100
|
-
context: context_exports
|
|
1101
|
-
}
|
|
1102
|
-
};
|
|
1103
|
-
const cache = createCache();
|
|
1104
|
-
const schemaPointerMap = {};
|
|
1105
|
-
for (const ptr of rootConfig.schemas) {
|
|
1106
|
-
if (typeof ptr === "string") {
|
|
1107
|
-
schemaPointerMap[ptr] = {};
|
|
1108
|
-
} else if (typeof ptr === "object") {
|
|
1109
|
-
Object.assign(schemaPointerMap, ptr);
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
const hash2 = JSON.stringify(schemaPointerMap);
|
|
1113
|
-
const result = await cache("schema", hash2, async () => {
|
|
1114
|
-
return loadSchema(schemaPointerMap, options.cwd, options.loaders);
|
|
1115
|
-
});
|
|
1116
|
-
const outputs = await preset.buildGeneratesSection({
|
|
1117
|
-
baseOutputDir: options.modulesDir,
|
|
1118
|
-
presetConfig: {
|
|
1119
|
-
baseTypesPath: rootConfig.baseTypesPath,
|
|
1120
|
-
filename: rootConfig.moduleDefinitionName,
|
|
1121
|
-
encapsulateModuleTypes: "none",
|
|
1122
|
-
extensionsPath: options.extensions,
|
|
1123
|
-
importExtension: options.importExtension
|
|
1124
|
-
},
|
|
1125
|
-
schema: result.outputSchema,
|
|
1126
|
-
schemaAst: result.outputSchemaAst,
|
|
1127
|
-
documents: [],
|
|
1128
|
-
pluginMap: rootConfig.pluginMap,
|
|
1129
|
-
plugins: rootConfig.plugins,
|
|
1130
|
-
config: {
|
|
1131
|
-
inputMaybeValue: "T | undefined",
|
|
1132
|
-
contextType: rootConfig.contextType,
|
|
1133
|
-
useTypeImports: true,
|
|
1134
|
-
scalars: buildScalarMap(
|
|
1135
|
-
rootConfig.scalars,
|
|
1136
|
-
options.cwd,
|
|
1137
|
-
_utilpath.join.call(void 0, options.modulesDir, options.baseTypesPath)
|
|
1138
|
-
)
|
|
1139
|
-
}
|
|
1140
|
-
});
|
|
1141
|
-
const promises = outputs.map(async (output) => {
|
|
1142
|
-
const result2 = await _core.codegen.call(void 0, {
|
|
1143
|
-
...output,
|
|
1144
|
-
cache
|
|
1145
|
-
});
|
|
1146
|
-
return {
|
|
1147
|
-
filename: output.filename,
|
|
1148
|
-
content: result2
|
|
1149
|
-
};
|
|
1150
|
-
});
|
|
1151
|
-
return Promise.all(promises);
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
// index.ts
|
|
1155
|
-
function graphqlPlugin() {
|
|
1156
|
-
return _generatorsdk.createPluginV1.call(void 0, {
|
|
1157
|
-
name: "graphql",
|
|
1158
|
-
actionName: "GraphQL modules",
|
|
1159
|
-
watch: (options, watcher, reload) => {
|
|
1160
|
-
const handleChange = (file) => {
|
|
1161
|
-
if (_generatorsdk.isMatch.call(void 0, file.relativePath, options.schemas)) {
|
|
1162
|
-
reload(file);
|
|
1163
|
-
}
|
|
1164
|
-
};
|
|
1165
|
-
watcher.on("update", handleChange);
|
|
1166
|
-
watcher.on("delete", handleChange);
|
|
1167
|
-
},
|
|
1168
|
-
generate: async (ctx, next) => {
|
|
1169
|
-
const items = await generate(ctx.generatorOptions);
|
|
1170
|
-
for (const item of items) {
|
|
1171
|
-
ctx.fileManager.createAndAdd(item.filename, item.content, "graphql");
|
|
1172
|
-
}
|
|
1173
|
-
return next();
|
|
1174
|
-
}
|
|
1175
|
-
});
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
exports.graphqlPlugin = graphqlPlugin;
|
|
1180
|
-
//# sourceMappingURL=index.cjs.map
|