@baeta/plugin-graphql 0.0.0-57-20230619210342-5073d33
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 +185 -0
- package/dist/index.cjs +891 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +891 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,891 @@
|
|
|
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
|
+
|
|
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 _apolloengineloader = require('@graphql-tools/apollo-engine-loader');
|
|
34
|
+
var _codefileloader = require('@graphql-tools/code-file-loader');
|
|
35
|
+
var _gitloader = require('@graphql-tools/git-loader');
|
|
36
|
+
var _githubloader = require('@graphql-tools/github-loader');
|
|
37
|
+
var _graphqlfileloader = require('@graphql-tools/graphql-file-loader');
|
|
38
|
+
var _jsonfileloader = require('@graphql-tools/json-file-loader');
|
|
39
|
+
var _load = require('@graphql-tools/load');
|
|
40
|
+
var _prismaloader = require('@graphql-tools/prisma-loader');
|
|
41
|
+
var _urlloader = require('@graphql-tools/url-loader');
|
|
42
|
+
|
|
43
|
+
// utils/hash.ts
|
|
44
|
+
|
|
45
|
+
var _graphql = require('graphql');
|
|
46
|
+
var _crypto = require('crypto');
|
|
47
|
+
function hashContent(content) {
|
|
48
|
+
return _crypto.createHash.call(void 0, "sha256").update(content).digest("hex");
|
|
49
|
+
}
|
|
50
|
+
function hashSchema(schema) {
|
|
51
|
+
return hashContent(_graphql.print.call(void 0, _pluginhelpers.getCachedDocumentNodeFromSchema.call(void 0, schema)));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// utils/load.ts
|
|
55
|
+
async function loadSchema(schemaPointerMap, cwd) {
|
|
56
|
+
const outputSchemaAst = await _load.loadSchema.call(void 0, schemaPointerMap, {
|
|
57
|
+
loaders: [
|
|
58
|
+
new (0, _codefileloader.CodeFileLoader)(),
|
|
59
|
+
new (0, _gitloader.GitLoader)(),
|
|
60
|
+
new (0, _githubloader.GithubLoader)(),
|
|
61
|
+
new (0, _graphqlfileloader.GraphQLFileLoader)(),
|
|
62
|
+
new (0, _jsonfileloader.JsonFileLoader)(),
|
|
63
|
+
new (0, _urlloader.UrlLoader)(),
|
|
64
|
+
new (0, _apolloengineloader.ApolloEngineLoader)(),
|
|
65
|
+
new (0, _prismaloader.PrismaLoader)()
|
|
66
|
+
],
|
|
67
|
+
cwd,
|
|
68
|
+
includeSources: true
|
|
69
|
+
});
|
|
70
|
+
if (!outputSchemaAst.extensions) {
|
|
71
|
+
outputSchemaAst.extensions = {};
|
|
72
|
+
}
|
|
73
|
+
outputSchemaAst.extensions["hash"] = hashSchema(outputSchemaAst);
|
|
74
|
+
return {
|
|
75
|
+
outputSchemaAst,
|
|
76
|
+
outputSchema: _pluginhelpers.getCachedDocumentNodeFromSchema.call(void 0, outputSchemaAst)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// lib/context/index.ts
|
|
81
|
+
var context_exports = {};
|
|
82
|
+
__export(context_exports, {
|
|
83
|
+
default: () => context_default,
|
|
84
|
+
plugin: () => plugin
|
|
85
|
+
});
|
|
86
|
+
var _visitorplugincommon = require('@graphql-codegen/visitor-plugin-common');
|
|
87
|
+
var plugin = async (schema, documents, config) => {
|
|
88
|
+
const prepend = [];
|
|
89
|
+
const mapper = _visitorplugincommon.parseMapper.call(void 0, config.contextType || "any");
|
|
90
|
+
if (mapper.isExternal && mapper.source) {
|
|
91
|
+
const identifier = mapper.default ? "ContextType" : `${mapper.import} as ContextType`;
|
|
92
|
+
const result = _visitorplugincommon.buildMapperImport.call(void 0,
|
|
93
|
+
mapper.source,
|
|
94
|
+
[
|
|
95
|
+
{
|
|
96
|
+
identifier,
|
|
97
|
+
asDefault: mapper.default
|
|
98
|
+
}
|
|
99
|
+
],
|
|
100
|
+
true
|
|
101
|
+
);
|
|
102
|
+
if (result) {
|
|
103
|
+
prepend.push(result);
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
prepend.push(`type ContextType = ${mapper.type}`);
|
|
107
|
+
}
|
|
108
|
+
prepend.push("export type { ContextType }");
|
|
109
|
+
return {
|
|
110
|
+
content: "",
|
|
111
|
+
prepend
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
var context_default = { plugin };
|
|
115
|
+
|
|
116
|
+
// lib/modules/index.ts
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
var _path = require('path');
|
|
120
|
+
|
|
121
|
+
// lib/modules/builder.ts
|
|
122
|
+
var _changecaseall = require('change-case-all');
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
// lib/modules/utils.ts
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
var _parsefilepath = require('parse-filepath'); var _parsefilepath2 = _interopRequireDefault(_parsefilepath);
|
|
136
|
+
var sep = "/";
|
|
137
|
+
function collectUsedTypes(doc) {
|
|
138
|
+
const used = [];
|
|
139
|
+
doc.definitions.forEach(findRelated);
|
|
140
|
+
function markAsUsed(type) {
|
|
141
|
+
pushUnique(used, type);
|
|
142
|
+
}
|
|
143
|
+
function findRelated(node) {
|
|
144
|
+
if (node.kind === _graphql.Kind.OBJECT_TYPE_DEFINITION || node.kind === _graphql.Kind.OBJECT_TYPE_EXTENSION) {
|
|
145
|
+
markAsUsed(node.name.value);
|
|
146
|
+
if (node.fields) {
|
|
147
|
+
node.fields.forEach(findRelated);
|
|
148
|
+
}
|
|
149
|
+
if (node.interfaces) {
|
|
150
|
+
node.interfaces.forEach(findRelated);
|
|
151
|
+
}
|
|
152
|
+
} else if (node.kind === _graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION || node.kind === _graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION) {
|
|
153
|
+
markAsUsed(node.name.value);
|
|
154
|
+
if (node.fields) {
|
|
155
|
+
node.fields.forEach(findRelated);
|
|
156
|
+
}
|
|
157
|
+
} else if (node.kind === _graphql.Kind.INTERFACE_TYPE_DEFINITION || node.kind === _graphql.Kind.INTERFACE_TYPE_EXTENSION) {
|
|
158
|
+
markAsUsed(node.name.value);
|
|
159
|
+
if (node.fields) {
|
|
160
|
+
node.fields.forEach(findRelated);
|
|
161
|
+
}
|
|
162
|
+
if (node.interfaces) {
|
|
163
|
+
node.interfaces.forEach(findRelated);
|
|
164
|
+
}
|
|
165
|
+
} else if (node.kind === _graphql.Kind.UNION_TYPE_DEFINITION || node.kind === _graphql.Kind.UNION_TYPE_EXTENSION) {
|
|
166
|
+
markAsUsed(node.name.value);
|
|
167
|
+
if (node.types) {
|
|
168
|
+
node.types.forEach(findRelated);
|
|
169
|
+
}
|
|
170
|
+
} else if (node.kind === _graphql.Kind.ENUM_TYPE_DEFINITION || node.kind === _graphql.Kind.ENUM_TYPE_EXTENSION) {
|
|
171
|
+
markAsUsed(node.name.value);
|
|
172
|
+
} else if (node.kind === _graphql.Kind.SCALAR_TYPE_DEFINITION || node.kind === _graphql.Kind.SCALAR_TYPE_EXTENSION) {
|
|
173
|
+
if (!isGraphQLPrimitive(node.name.value)) {
|
|
174
|
+
markAsUsed(node.name.value);
|
|
175
|
+
}
|
|
176
|
+
} else if (node.kind === _graphql.Kind.INPUT_VALUE_DEFINITION) {
|
|
177
|
+
findRelated(resolveTypeNode(node.type));
|
|
178
|
+
} else if (node.kind === _graphql.Kind.FIELD_DEFINITION) {
|
|
179
|
+
findRelated(resolveTypeNode(node.type));
|
|
180
|
+
if (node.arguments) {
|
|
181
|
+
node.arguments.forEach(findRelated);
|
|
182
|
+
}
|
|
183
|
+
} else if (node.kind === _graphql.Kind.NAMED_TYPE && // Named type
|
|
184
|
+
!isGraphQLPrimitive(node.name.value)) {
|
|
185
|
+
markAsUsed(node.name.value);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return used;
|
|
189
|
+
}
|
|
190
|
+
function collectObjectFieldType(node, fieldDefinition, fieldsMap, fieldTypes) {
|
|
191
|
+
const objectName = node.name.value;
|
|
192
|
+
const fieldName = fieldDefinition.name.value;
|
|
193
|
+
const field = fieldsMap[fieldDefinition.name.value];
|
|
194
|
+
const baseName = _pluginhelpers.getBaseType.call(void 0, field.type).name;
|
|
195
|
+
const isDefaultScalar = _visitorplugincommon.DEFAULT_SCALARS[baseName] != null;
|
|
196
|
+
const name = isDefaultScalar ? `Types.Scalars["${baseName}"]` : baseName;
|
|
197
|
+
const type = _visitorplugincommon.wrapTypeWithModifiers.call(void 0, name, field.type, {
|
|
198
|
+
wrapOptional: (str) => {
|
|
199
|
+
return `Types.Maybe<${str}>`;
|
|
200
|
+
},
|
|
201
|
+
wrapArray: (str) => {
|
|
202
|
+
return `Array<${str}>`;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
if (fieldTypes[objectName] == null) {
|
|
206
|
+
fieldTypes[objectName] = {};
|
|
207
|
+
}
|
|
208
|
+
fieldTypes[objectName][fieldName] = type;
|
|
209
|
+
}
|
|
210
|
+
function collectObjectFieldArguments(node, fieldDefinition, fieldArguments) {
|
|
211
|
+
const objectName = node.name.value;
|
|
212
|
+
const fieldName = fieldDefinition.name.value;
|
|
213
|
+
const hasArguments = fieldDefinition.arguments != null && fieldDefinition.arguments.length > 0;
|
|
214
|
+
if (fieldArguments[objectName] == null) {
|
|
215
|
+
fieldArguments[objectName] = {};
|
|
216
|
+
}
|
|
217
|
+
fieldArguments[objectName][fieldName] = hasArguments;
|
|
218
|
+
}
|
|
219
|
+
function collectObjectFieldTypesAndArguments(schema) {
|
|
220
|
+
const fieldTypes = {};
|
|
221
|
+
const fieldArguments = {};
|
|
222
|
+
if (!schema) {
|
|
223
|
+
return { fieldTypes, fieldArguments };
|
|
224
|
+
}
|
|
225
|
+
const schemaTypes = schema.getTypeMap();
|
|
226
|
+
for (const type of Object.values(schemaTypes)) {
|
|
227
|
+
if (_optionalChain([type, 'access', _ => _.astNode, 'optionalAccess', _2 => _2.kind]) !== _graphql.Kind.OBJECT_TYPE_DEFINITION) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const schemaType = schemaTypes[type.name];
|
|
231
|
+
const fieldsMap = schemaType.getFields();
|
|
232
|
+
for (const field of _nullishCoalesce(type.astNode.fields, () => ( []))) {
|
|
233
|
+
collectObjectFieldArguments(type.astNode, field, fieldArguments);
|
|
234
|
+
collectObjectFieldType(type.astNode, field, fieldsMap, fieldTypes);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return { fieldTypes, fieldArguments };
|
|
238
|
+
}
|
|
239
|
+
function resolveTypeNode(node) {
|
|
240
|
+
if (node.kind === _graphql.Kind.LIST_TYPE) {
|
|
241
|
+
return resolveTypeNode(node.type);
|
|
242
|
+
}
|
|
243
|
+
if (node.kind === _graphql.Kind.NON_NULL_TYPE) {
|
|
244
|
+
return resolveTypeNode(node.type);
|
|
245
|
+
}
|
|
246
|
+
return node;
|
|
247
|
+
}
|
|
248
|
+
function isGraphQLPrimitive(name) {
|
|
249
|
+
return ["String", "Boolean", "ID", "Float", "Int"].includes(name);
|
|
250
|
+
}
|
|
251
|
+
function unique(val, i, all) {
|
|
252
|
+
return i === all.indexOf(val);
|
|
253
|
+
}
|
|
254
|
+
function withQuotes(val) {
|
|
255
|
+
return `'${val}'`;
|
|
256
|
+
}
|
|
257
|
+
function indent(size) {
|
|
258
|
+
const space = new Array(size).fill(" ").join("");
|
|
259
|
+
function indentInner(val) {
|
|
260
|
+
return val.split("\n").map((line) => `${space}${line}`).join("\n");
|
|
261
|
+
}
|
|
262
|
+
return indentInner;
|
|
263
|
+
}
|
|
264
|
+
function buildBlock({ name, lines }) {
|
|
265
|
+
if (!lines.length) {
|
|
266
|
+
return "";
|
|
267
|
+
}
|
|
268
|
+
return [`${name} {`, ...lines.map(indent(2)), "};"].join("\n");
|
|
269
|
+
}
|
|
270
|
+
var getRelativePath = function(filepath, basePath) {
|
|
271
|
+
const normalizedFilepath = normalize(filepath);
|
|
272
|
+
const normalizedBasePath = ensureStartsWithSeparator(
|
|
273
|
+
normalize(ensureEndsWithSeparator(basePath))
|
|
274
|
+
);
|
|
275
|
+
const [, relativePath] = normalizedFilepath.split(normalizedBasePath);
|
|
276
|
+
return relativePath;
|
|
277
|
+
};
|
|
278
|
+
function groupSourcesByModule(sources, basePath) {
|
|
279
|
+
const grouped = {};
|
|
280
|
+
sources.forEach((source) => {
|
|
281
|
+
if (!source.location) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const relativePath = getRelativePath(source.location, basePath);
|
|
285
|
+
if (relativePath) {
|
|
286
|
+
const mod = extractModuleDirectory(source.location, basePath);
|
|
287
|
+
if (!grouped[mod]) {
|
|
288
|
+
grouped[mod] = [];
|
|
289
|
+
}
|
|
290
|
+
grouped[mod].push(source);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
return grouped;
|
|
294
|
+
}
|
|
295
|
+
function extractModuleDirectory(filepath, basePath) {
|
|
296
|
+
const relativePath = getRelativePath(filepath, basePath);
|
|
297
|
+
const [moduleDirectory] = relativePath.split(sep);
|
|
298
|
+
return moduleDirectory;
|
|
299
|
+
}
|
|
300
|
+
function stripFilename(path) {
|
|
301
|
+
const parsedPath = _parsefilepath2.default.call(void 0, path);
|
|
302
|
+
return normalize(parsedPath.dir);
|
|
303
|
+
}
|
|
304
|
+
function normalize(path) {
|
|
305
|
+
return path.replace(/\\/g, "/");
|
|
306
|
+
}
|
|
307
|
+
function ensureEndsWithSeparator(path) {
|
|
308
|
+
return path.endsWith(sep) ? path : path + sep;
|
|
309
|
+
}
|
|
310
|
+
function ensureStartsWithSeparator(path) {
|
|
311
|
+
return path.startsWith(".") ? path.replace(/^(..\/)|(.\/)/, "/") : path.startsWith("/") ? path : `/${path}`;
|
|
312
|
+
}
|
|
313
|
+
function pushUnique(list, item) {
|
|
314
|
+
if (!list.includes(item)) {
|
|
315
|
+
list.push(item);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function concatByKey(left, right, key) {
|
|
319
|
+
return [.../* @__PURE__ */ new Set([...left[key], ...right[key]])];
|
|
320
|
+
}
|
|
321
|
+
function uniqueByKey(left, right, key) {
|
|
322
|
+
return left[key].filter((item) => !right[key].includes(item));
|
|
323
|
+
}
|
|
324
|
+
function createObject(keys, valueFn) {
|
|
325
|
+
const obj = {};
|
|
326
|
+
keys.forEach((key) => {
|
|
327
|
+
obj[key] = valueFn(key);
|
|
328
|
+
});
|
|
329
|
+
return obj;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// lib/modules/builder.ts
|
|
333
|
+
var registryKeys = [
|
|
334
|
+
"objects",
|
|
335
|
+
"inputs",
|
|
336
|
+
"interfaces",
|
|
337
|
+
"scalars",
|
|
338
|
+
"unions",
|
|
339
|
+
"enums"
|
|
340
|
+
];
|
|
341
|
+
function buildModule(name, doc, {
|
|
342
|
+
importNamespace,
|
|
343
|
+
importPath,
|
|
344
|
+
encapsulate,
|
|
345
|
+
requireRootResolvers,
|
|
346
|
+
shouldDeclare,
|
|
347
|
+
rootTypes,
|
|
348
|
+
schema,
|
|
349
|
+
baseVisitor,
|
|
350
|
+
fieldTypes,
|
|
351
|
+
fieldArguments,
|
|
352
|
+
extensionsPath
|
|
353
|
+
}) {
|
|
354
|
+
const picks = createObject(
|
|
355
|
+
registryKeys,
|
|
356
|
+
() => ({})
|
|
357
|
+
);
|
|
358
|
+
const defined = createObject(registryKeys, () => []);
|
|
359
|
+
const extended = createObject(registryKeys, () => []);
|
|
360
|
+
const usedTypes = collectUsedTypes(doc);
|
|
361
|
+
_graphql.visit.call(void 0, doc, {
|
|
362
|
+
ObjectTypeDefinition(node) {
|
|
363
|
+
collectTypeDefinition(node);
|
|
364
|
+
},
|
|
365
|
+
ObjectTypeExtension(node) {
|
|
366
|
+
collectTypeExtension(node);
|
|
367
|
+
},
|
|
368
|
+
InputObjectTypeDefinition(node) {
|
|
369
|
+
collectTypeDefinition(node);
|
|
370
|
+
},
|
|
371
|
+
InputObjectTypeExtension(node) {
|
|
372
|
+
collectTypeExtension(node);
|
|
373
|
+
},
|
|
374
|
+
InterfaceTypeDefinition(node) {
|
|
375
|
+
collectTypeDefinition(node);
|
|
376
|
+
},
|
|
377
|
+
InterfaceTypeExtension(node) {
|
|
378
|
+
collectTypeExtension(node);
|
|
379
|
+
},
|
|
380
|
+
ScalarTypeDefinition(node) {
|
|
381
|
+
collectTypeDefinition(node);
|
|
382
|
+
},
|
|
383
|
+
UnionTypeDefinition(node) {
|
|
384
|
+
collectTypeDefinition(node);
|
|
385
|
+
},
|
|
386
|
+
UnionTypeExtension(node) {
|
|
387
|
+
collectTypeExtension(node);
|
|
388
|
+
},
|
|
389
|
+
EnumTypeDefinition(node) {
|
|
390
|
+
collectTypeDefinition(node);
|
|
391
|
+
},
|
|
392
|
+
EnumTypeExtension(node) {
|
|
393
|
+
collectTypeExtension(node);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
const visited = createObject(
|
|
397
|
+
registryKeys,
|
|
398
|
+
(key) => concatByKey(defined, extended, key)
|
|
399
|
+
);
|
|
400
|
+
const external = createObject(
|
|
401
|
+
registryKeys,
|
|
402
|
+
(key) => uniqueByKey(extended, defined, key)
|
|
403
|
+
);
|
|
404
|
+
const imports = [
|
|
405
|
+
`import * as ${importNamespace} from "${importPath}";`,
|
|
406
|
+
'import { DocumentNode } from "graphql";',
|
|
407
|
+
'import * as Baeta from "@baeta/core/sdk";'
|
|
408
|
+
];
|
|
409
|
+
if (extensionsPath) {
|
|
410
|
+
imports.push(`import baetaExtensions from "${extensionsPath}";`);
|
|
411
|
+
}
|
|
412
|
+
imports.push("\n");
|
|
413
|
+
let content = [
|
|
414
|
+
printDefinedFields(),
|
|
415
|
+
printDefinedEnumValues(),
|
|
416
|
+
printDefinedInputFields(),
|
|
417
|
+
printSchemaTypes(usedTypes),
|
|
418
|
+
printMetadata()
|
|
419
|
+
].filter(Boolean).join("\n\n");
|
|
420
|
+
const moduleNamespace = baseVisitor.convertName(name, {
|
|
421
|
+
suffix: "Module",
|
|
422
|
+
useTypesPrefix: false,
|
|
423
|
+
useTypesSuffix: false
|
|
424
|
+
});
|
|
425
|
+
if (encapsulate === "namespace") {
|
|
426
|
+
content = `${shouldDeclare ? "declare" : "export"} namespace ${baseVisitor.convertName(name, {
|
|
427
|
+
suffix: "Module",
|
|
428
|
+
useTypesPrefix: false,
|
|
429
|
+
useTypesSuffix: false
|
|
430
|
+
})} {
|
|
431
|
+
${shouldDeclare ? `${indent(2)(imports.join("\n"))}
|
|
432
|
+
` : ""}${indent(2)(content)}
|
|
433
|
+
}`;
|
|
434
|
+
}
|
|
435
|
+
return [...shouldDeclare ? [] : imports, content, printFactoryMethod()].filter(Boolean).join("\n");
|
|
436
|
+
function printMetadata() {
|
|
437
|
+
return `export namespace ModuleMetadata {
|
|
438
|
+
export const id = '${name}';
|
|
439
|
+
export const dirname = './${name}';
|
|
440
|
+
export const typedef = ${JSON.stringify(doc)} as unknown as DocumentNode;
|
|
441
|
+
${printBaetaManager()}
|
|
442
|
+
}`;
|
|
443
|
+
}
|
|
444
|
+
function printDefinedFields() {
|
|
445
|
+
return buildBlock({
|
|
446
|
+
name: "interface DefinedFields",
|
|
447
|
+
lines: [...visited.objects, ...visited.interfaces].map(
|
|
448
|
+
(typeName) => `${typeName}: ${printPicks(typeName, {
|
|
449
|
+
...picks.objects,
|
|
450
|
+
...picks.interfaces
|
|
451
|
+
})};`
|
|
452
|
+
)
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
function printFactoryMethod() {
|
|
456
|
+
const name2 = moduleNamespace.slice(0, moduleNamespace.length - 6);
|
|
457
|
+
const createModuleFn = `create${name2}Module`;
|
|
458
|
+
const getModuleFn = `get${name2}Module`;
|
|
459
|
+
const extensionsArg = extensionsPath ? ", baetaExtensions" : "";
|
|
460
|
+
return `
|
|
461
|
+
export const ${createModuleFn} = () => Baeta.createModuleManager(ModuleMetadata${extensionsArg});
|
|
462
|
+
export const ${getModuleFn} = Baeta.createSingletonModule(${createModuleFn});
|
|
463
|
+
`;
|
|
464
|
+
}
|
|
465
|
+
function printObjectFieldResolverBuilder(typeName, field) {
|
|
466
|
+
const parentType = getParentType(typeName);
|
|
467
|
+
const resultType = getResultType(typeName, field);
|
|
468
|
+
const argumentsType = getArgsType(typeName, field);
|
|
469
|
+
const contextType = getContextType();
|
|
470
|
+
return `${field}: module.createResolverBuilder<${resultType}, ${parentType}, ${contextType}, ${argumentsType}>("${typeName}", "${field}"),`;
|
|
471
|
+
}
|
|
472
|
+
function printObjectResolverBuilder(typeName, objects) {
|
|
473
|
+
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))]), () => ( []));
|
|
474
|
+
if (fields.length === 0) {
|
|
475
|
+
return "";
|
|
476
|
+
}
|
|
477
|
+
const parentType = getParentType(typeName);
|
|
478
|
+
const contextType = getContextType();
|
|
479
|
+
const addons = [`...module.createTypeMethods<${parentType}, ${contextType}>("${typeName}"),`];
|
|
480
|
+
const contentBody = [...addons, ...fields].map(indent(2)).join("\n");
|
|
481
|
+
const content2 = `{
|
|
482
|
+
${contentBody}
|
|
483
|
+
}`;
|
|
484
|
+
return `${typeName}: ${content2},`;
|
|
485
|
+
}
|
|
486
|
+
function printSubscriptionFieldBuilder(field) {
|
|
487
|
+
const parentType = getParentType("Subscription");
|
|
488
|
+
const resultType = getResultType("Subscription", field);
|
|
489
|
+
const argumentsType = getArgsType("Subscription", field);
|
|
490
|
+
const contextType = getContextType();
|
|
491
|
+
return `${field}: module.createSubscriptionBuilder<${resultType}, ${parentType}, ${contextType}, ${argumentsType}>("${field}"),`;
|
|
492
|
+
}
|
|
493
|
+
function printSubscriptionObjectBuilder() {
|
|
494
|
+
const subscriptions = _nullishCoalesce(_optionalChain([picks, 'access', _8 => _8.objects, 'access', _9 => _9["Subscription"], 'optionalAccess', _10 => _10.filter, 'call', _11 => _11(unique)]), () => ( []));
|
|
495
|
+
if (subscriptions.length === 0) {
|
|
496
|
+
return "";
|
|
497
|
+
}
|
|
498
|
+
const fields = subscriptions.map((subscription) => printSubscriptionFieldBuilder(subscription));
|
|
499
|
+
const parentType = getParentType("Subscription");
|
|
500
|
+
const contextType = getContextType();
|
|
501
|
+
const addons = [`...module.createSubscriptionMethods<${parentType}, ${contextType}>(),`];
|
|
502
|
+
const contentBody = [...addons, ...fields].map(indent(2)).join("\n");
|
|
503
|
+
const content2 = `{
|
|
504
|
+
${contentBody}
|
|
505
|
+
}`;
|
|
506
|
+
return `Subscription: ${content2},`;
|
|
507
|
+
}
|
|
508
|
+
function printScalarBuilder() {
|
|
509
|
+
const scalars = visited.scalars;
|
|
510
|
+
if (scalars.length === 0) {
|
|
511
|
+
return "";
|
|
512
|
+
}
|
|
513
|
+
const fields = scalars.map((scalar) => `${scalar}: module.createScalarBuilder("${scalar}"),`);
|
|
514
|
+
const content2 = fields.map(indent(2)).join("\n");
|
|
515
|
+
return `Scalar: {
|
|
516
|
+
${content2}
|
|
517
|
+
},`;
|
|
518
|
+
}
|
|
519
|
+
function printBaetaManager() {
|
|
520
|
+
const objects = visited.objects.filter((type) => type !== "Subscription").map((typeName) => printObjectResolverBuilder(typeName, picks.objects)).filter(Boolean);
|
|
521
|
+
const contextType = getContextType();
|
|
522
|
+
const addons = [`...module.createModuleMethods<${contextType}>(),`];
|
|
523
|
+
const bodyFields = [
|
|
524
|
+
...addons,
|
|
525
|
+
...objects,
|
|
526
|
+
printScalarBuilder(),
|
|
527
|
+
printSubscriptionObjectBuilder()
|
|
528
|
+
];
|
|
529
|
+
const body = bodyFields.filter(Boolean).map(indent(6)).join("\n");
|
|
530
|
+
const content2 = `{
|
|
531
|
+
${body}
|
|
532
|
+
}`;
|
|
533
|
+
return `
|
|
534
|
+
export function createManager(module: Baeta.ModuleBuilder) {
|
|
535
|
+
return ${content2};
|
|
536
|
+
}`;
|
|
537
|
+
}
|
|
538
|
+
function printDefinedEnumValues() {
|
|
539
|
+
return buildBlock({
|
|
540
|
+
name: "interface DefinedEnumValues",
|
|
541
|
+
lines: visited.enums.map((typeName) => `${typeName}: ${printPicks(typeName, picks.enums)};`)
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
function encapsulateTypeName(typeName) {
|
|
545
|
+
if (encapsulate === "prefix") {
|
|
546
|
+
return `${_changecaseall.pascalCase.call(void 0, name)}_${typeName}`;
|
|
547
|
+
}
|
|
548
|
+
return typeName;
|
|
549
|
+
}
|
|
550
|
+
function printDefinedInputFields() {
|
|
551
|
+
return buildBlock({
|
|
552
|
+
name: "interface DefinedInputFields",
|
|
553
|
+
lines: visited.inputs.map(
|
|
554
|
+
(typeName) => `${typeName}: ${printPicks(typeName, picks.inputs)};`
|
|
555
|
+
)
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
function printSchemaTypes(types) {
|
|
559
|
+
return types.filter((type) => !visited.scalars.includes(type)).map(printExportType).join("\n");
|
|
560
|
+
}
|
|
561
|
+
function printPicks(typeName, records) {
|
|
562
|
+
return records[typeName].filter(unique).map(withQuotes).join(" | ");
|
|
563
|
+
}
|
|
564
|
+
function printTypeBody(typeName) {
|
|
565
|
+
const coreType = `${importNamespace}.${baseVisitor.convertName(typeName, {
|
|
566
|
+
useTypesSuffix: true,
|
|
567
|
+
useTypesPrefix: true
|
|
568
|
+
})}`;
|
|
569
|
+
if (external.enums.includes(typeName) || external.objects.includes(typeName)) {
|
|
570
|
+
if (schema && _graphql.isScalarType.call(void 0, schema.getType(typeName))) {
|
|
571
|
+
return `${importNamespace}.Scalars['${typeName}']`;
|
|
572
|
+
}
|
|
573
|
+
return coreType;
|
|
574
|
+
}
|
|
575
|
+
if (defined.enums.includes(typeName) && picks.enums[typeName]) {
|
|
576
|
+
return `DefinedEnumValues['${typeName}']`;
|
|
577
|
+
}
|
|
578
|
+
if (defined.objects.includes(typeName) && picks.objects[typeName]) {
|
|
579
|
+
return `Pick<${coreType}, DefinedFields['${typeName}']>`;
|
|
580
|
+
}
|
|
581
|
+
if (defined.interfaces.includes(typeName) && picks.interfaces[typeName]) {
|
|
582
|
+
return `Pick<${coreType}, DefinedFields['${typeName}']>`;
|
|
583
|
+
}
|
|
584
|
+
if (defined.inputs.includes(typeName) && picks.inputs[typeName]) {
|
|
585
|
+
return `Pick<${coreType}, DefinedInputFields['${typeName}']>`;
|
|
586
|
+
}
|
|
587
|
+
return coreType;
|
|
588
|
+
}
|
|
589
|
+
function printExportType(typeName) {
|
|
590
|
+
return `export type ${encapsulateTypeName(typeName)} = ${printTypeBody(typeName)};`;
|
|
591
|
+
}
|
|
592
|
+
function collectFields(node, picksObj) {
|
|
593
|
+
const name2 = node.name.value;
|
|
594
|
+
if (node.fields) {
|
|
595
|
+
if (!picksObj[name2]) {
|
|
596
|
+
picksObj[name2] = [];
|
|
597
|
+
}
|
|
598
|
+
node.fields.forEach((field) => {
|
|
599
|
+
picksObj[name2].push(field.name.value);
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
function collectValuesFromEnum(node) {
|
|
604
|
+
const name2 = node.name.value;
|
|
605
|
+
if (node.values) {
|
|
606
|
+
if (!picks.enums[name2]) {
|
|
607
|
+
picks.enums[name2] = [];
|
|
608
|
+
}
|
|
609
|
+
node.values.forEach((field) => {
|
|
610
|
+
picks.enums[name2].push(field.name.value);
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
function collectTypeDefinition(node) {
|
|
615
|
+
const name2 = node.name.value;
|
|
616
|
+
switch (node.kind) {
|
|
617
|
+
case _graphql.Kind.OBJECT_TYPE_DEFINITION: {
|
|
618
|
+
defined.objects.push(name2);
|
|
619
|
+
collectFields(node, picks.objects);
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
case _graphql.Kind.ENUM_TYPE_DEFINITION: {
|
|
623
|
+
defined.enums.push(name2);
|
|
624
|
+
collectValuesFromEnum(node);
|
|
625
|
+
break;
|
|
626
|
+
}
|
|
627
|
+
case _graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION: {
|
|
628
|
+
defined.inputs.push(name2);
|
|
629
|
+
collectFields(node, picks.inputs);
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
case _graphql.Kind.SCALAR_TYPE_DEFINITION: {
|
|
633
|
+
defined.scalars.push(name2);
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
case _graphql.Kind.INTERFACE_TYPE_DEFINITION: {
|
|
637
|
+
defined.interfaces.push(name2);
|
|
638
|
+
collectFields(node, picks.interfaces);
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
case _graphql.Kind.UNION_TYPE_DEFINITION: {
|
|
642
|
+
defined.unions.push(name2);
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function collectTypeExtension(node) {
|
|
648
|
+
const name2 = node.name.value;
|
|
649
|
+
switch (node.kind) {
|
|
650
|
+
case _graphql.Kind.OBJECT_TYPE_EXTENSION: {
|
|
651
|
+
collectFields(node, picks.objects);
|
|
652
|
+
if (rootTypes.includes(name2)) {
|
|
653
|
+
pushUnique(defined.objects, name2);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
pushUnique(extended.objects, name2);
|
|
657
|
+
break;
|
|
658
|
+
}
|
|
659
|
+
case _graphql.Kind.ENUM_TYPE_EXTENSION: {
|
|
660
|
+
collectValuesFromEnum(node);
|
|
661
|
+
pushUnique(extended.enums, name2);
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
case _graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION: {
|
|
665
|
+
collectFields(node, picks.inputs);
|
|
666
|
+
pushUnique(extended.inputs, name2);
|
|
667
|
+
break;
|
|
668
|
+
}
|
|
669
|
+
case _graphql.Kind.INTERFACE_TYPE_EXTENSION: {
|
|
670
|
+
collectFields(node, picks.interfaces);
|
|
671
|
+
pushUnique(extended.interfaces, name2);
|
|
672
|
+
break;
|
|
673
|
+
}
|
|
674
|
+
case _graphql.Kind.UNION_TYPE_EXTENSION: {
|
|
675
|
+
pushUnique(extended.unions, name2);
|
|
676
|
+
break;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
function getParentType(type) {
|
|
681
|
+
if (["Query", "Mutation", "Subscription"].includes(type)) {
|
|
682
|
+
return "{}";
|
|
683
|
+
}
|
|
684
|
+
return type;
|
|
685
|
+
}
|
|
686
|
+
function getResultType(type, field) {
|
|
687
|
+
return _optionalChain([fieldTypes, 'optionalAccess', _12 => _12[type], 'optionalAccess', _13 => _13[field]]) || "{}";
|
|
688
|
+
}
|
|
689
|
+
function getArgsType(type, field) {
|
|
690
|
+
const hasArgs = _nullishCoalesce(_optionalChain([fieldArguments, 'optionalAccess', _14 => _14[type], 'optionalAccess', _15 => _15[field]]), () => ( false));
|
|
691
|
+
if (!hasArgs) {
|
|
692
|
+
return "{}";
|
|
693
|
+
}
|
|
694
|
+
const fieldUpper = field[0].toUpperCase() + field.slice(1);
|
|
695
|
+
return `Types.${type}${fieldUpper}Args`;
|
|
696
|
+
}
|
|
697
|
+
function getContextType() {
|
|
698
|
+
return "Types.ContextType";
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// lib/modules/index.ts
|
|
703
|
+
var preset = {
|
|
704
|
+
buildGeneratesSection: (options) => {
|
|
705
|
+
const { baseOutputDir } = options;
|
|
706
|
+
const { baseTypesPath, extensionsPath, encapsulateModuleTypes } = options.presetConfig;
|
|
707
|
+
const requireRootResolvers = _visitorplugincommon.getConfigValue.call(void 0, _optionalChain([options, 'optionalAccess', _16 => _16.presetConfig, 'access', _17 => _17.requireRootResolvers]), false);
|
|
708
|
+
const cwd = _path.resolve.call(void 0, options.presetConfig.cwd || process.cwd());
|
|
709
|
+
const importTypesNamespace = options.presetConfig.importTypesNamespace || "Types";
|
|
710
|
+
if (!baseTypesPath) {
|
|
711
|
+
throw new Error(
|
|
712
|
+
`Preset "graphql-modules" requires you to specify "baseTypesPath" configuration and point it to your base types file (generated by "typescript" plugin)!`
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
if (!_optionalChain([options, 'access', _18 => _18.schemaAst, 'optionalAccess', _19 => _19.extensions, 'access', _20 => _20.sources])) {
|
|
716
|
+
throw new Error(`Preset "graphql-modules" requires to use GraphQL SDL`);
|
|
717
|
+
}
|
|
718
|
+
const extensions = _optionalChain([options, 'access', _21 => _21.schemaAst, 'optionalAccess', _22 => _22.extensions]);
|
|
719
|
+
const sources = _nullishCoalesce(_optionalChain([extensions, 'optionalAccess', _23 => _23.extendedSources]), () => ( []));
|
|
720
|
+
const sourcesByModuleMap = groupSourcesByModule(sources, baseOutputDir);
|
|
721
|
+
const modules = Object.keys(sourcesByModuleMap);
|
|
722
|
+
const baseVisitor = new (0, _visitorplugincommon.BaseVisitor)(options.config, {});
|
|
723
|
+
const { fieldTypes, fieldArguments } = collectObjectFieldTypesAndArguments(options.schemaAst);
|
|
724
|
+
const baseOutput = {
|
|
725
|
+
filename: _path.resolve.call(void 0, cwd, baseOutputDir, baseTypesPath),
|
|
726
|
+
schema: options.schema,
|
|
727
|
+
documents: options.documents,
|
|
728
|
+
plugins: [
|
|
729
|
+
...options.plugins,
|
|
730
|
+
{
|
|
731
|
+
"modules-exported-scalars": {}
|
|
732
|
+
}
|
|
733
|
+
],
|
|
734
|
+
pluginMap: {
|
|
735
|
+
...options.pluginMap,
|
|
736
|
+
"modules-exported-scalars": {
|
|
737
|
+
plugin: (schema) => {
|
|
738
|
+
const typeMap = schema.getTypeMap();
|
|
739
|
+
return Object.keys(typeMap).map((t) => {
|
|
740
|
+
if (t && typeMap[t] && _graphql.isScalarType.call(void 0, typeMap[t]) && !isGraphQLPrimitive(t)) {
|
|
741
|
+
const convertedName = baseVisitor.convertName(t);
|
|
742
|
+
return `export type ${convertedName} = Scalars["${t}"];`;
|
|
743
|
+
}
|
|
744
|
+
return null;
|
|
745
|
+
}).filter(Boolean).join("\n");
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
},
|
|
749
|
+
config: {
|
|
750
|
+
...options.config,
|
|
751
|
+
enumsAsTypes: true
|
|
752
|
+
},
|
|
753
|
+
schemaAst: options.schemaAst
|
|
754
|
+
};
|
|
755
|
+
const baseTypesFilename = baseTypesPath.replace(/\.(js|ts|d.ts)$/, "");
|
|
756
|
+
const baseTypesDir = stripFilename(baseOutput.filename);
|
|
757
|
+
const outputs = modules.map((moduleName) => {
|
|
758
|
+
const filename = _path.resolve.call(void 0, cwd, baseOutputDir, moduleName, options.presetConfig.filename);
|
|
759
|
+
const dirpath = stripFilename(filename);
|
|
760
|
+
const relativePath = _path.relative.call(void 0, dirpath, baseTypesDir);
|
|
761
|
+
const importPath = options.presetConfig.importBaseTypesFrom || normalize(_path.join.call(void 0, relativePath, baseTypesFilename));
|
|
762
|
+
const sources2 = sourcesByModuleMap[moduleName];
|
|
763
|
+
const documents = sources2.map((source) => source.document);
|
|
764
|
+
const moduleDocument = _graphql.concatAST.call(void 0, documents);
|
|
765
|
+
const shouldDeclare = filename.endsWith(".d.ts");
|
|
766
|
+
return {
|
|
767
|
+
filename,
|
|
768
|
+
schema: options.schema,
|
|
769
|
+
documents: [],
|
|
770
|
+
plugins: [
|
|
771
|
+
...options.plugins.filter((p) => typeof p === "object" && !!p.add),
|
|
772
|
+
{
|
|
773
|
+
"graphql-modules-plugin": {}
|
|
774
|
+
}
|
|
775
|
+
],
|
|
776
|
+
pluginMap: {
|
|
777
|
+
...options.pluginMap,
|
|
778
|
+
"graphql-modules-plugin": {
|
|
779
|
+
plugin: (schema) => buildModule(moduleName, moduleDocument, {
|
|
780
|
+
importNamespace: importTypesNamespace,
|
|
781
|
+
importPath,
|
|
782
|
+
encapsulate: encapsulateModuleTypes || "namespace",
|
|
783
|
+
requireRootResolvers,
|
|
784
|
+
shouldDeclare,
|
|
785
|
+
schema,
|
|
786
|
+
baseVisitor,
|
|
787
|
+
useGraphQLModules: false,
|
|
788
|
+
fieldTypes,
|
|
789
|
+
fieldArguments,
|
|
790
|
+
extensionsPath,
|
|
791
|
+
rootTypes: [
|
|
792
|
+
_optionalChain([schema, 'access', _24 => _24.getQueryType, 'call', _25 => _25(), 'optionalAccess', _26 => _26.name]) || "",
|
|
793
|
+
_optionalChain([schema, 'access', _27 => _27.getMutationType, 'call', _28 => _28(), 'optionalAccess', _29 => _29.name]) || "",
|
|
794
|
+
_optionalChain([schema, 'access', _30 => _30.getSubscriptionType, 'call', _31 => _31(), 'optionalAccess', _32 => _32.name]) || ""
|
|
795
|
+
].filter(Boolean)
|
|
796
|
+
})
|
|
797
|
+
}
|
|
798
|
+
},
|
|
799
|
+
config: options.config,
|
|
800
|
+
schemaAst: options.schemaAst
|
|
801
|
+
};
|
|
802
|
+
});
|
|
803
|
+
return [baseOutput].concat(outputs);
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
// lib/codegen.ts
|
|
808
|
+
async function generate(options) {
|
|
809
|
+
const rootConfig = {
|
|
810
|
+
schemas: _pluginhelpers.normalizeInstanceOrArray.call(void 0, options.schemas),
|
|
811
|
+
modulesDir: options.modulesDir,
|
|
812
|
+
baseTypesPath: options.baseTypesPath,
|
|
813
|
+
contextType: options.contextType,
|
|
814
|
+
moduleDefinitionName: options.moduleDefinitionName,
|
|
815
|
+
scalars: options.scalars,
|
|
816
|
+
plugins: _pluginhelpers.normalizeConfig.call(void 0, ["typescript", "context"]),
|
|
817
|
+
pluginMap: {
|
|
818
|
+
typescript: typescriptPlugin,
|
|
819
|
+
context: context_exports
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
const cache = createCache();
|
|
823
|
+
const schemaPointerMap = {};
|
|
824
|
+
for (const ptr of rootConfig.schemas) {
|
|
825
|
+
if (typeof ptr === "string") {
|
|
826
|
+
schemaPointerMap[ptr] = {};
|
|
827
|
+
} else if (typeof ptr === "object") {
|
|
828
|
+
Object.assign(schemaPointerMap, ptr);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
const hash = JSON.stringify(schemaPointerMap);
|
|
832
|
+
const result = await cache("schema", hash, async () => {
|
|
833
|
+
return loadSchema(schemaPointerMap, options.cwd);
|
|
834
|
+
});
|
|
835
|
+
const outputs = await preset.buildGeneratesSection({
|
|
836
|
+
baseOutputDir: options.modulesDir,
|
|
837
|
+
presetConfig: {
|
|
838
|
+
baseTypesPath: rootConfig.baseTypesPath,
|
|
839
|
+
filename: rootConfig.moduleDefinitionName,
|
|
840
|
+
encapsulateModuleTypes: "none",
|
|
841
|
+
extensionsPath: options.extensions
|
|
842
|
+
},
|
|
843
|
+
schema: result.outputSchema,
|
|
844
|
+
schemaAst: result.outputSchemaAst,
|
|
845
|
+
documents: [],
|
|
846
|
+
pluginMap: rootConfig.pluginMap,
|
|
847
|
+
plugins: rootConfig.plugins,
|
|
848
|
+
config: {
|
|
849
|
+
inputMaybeValue: "T | undefined",
|
|
850
|
+
contextType: rootConfig.contextType,
|
|
851
|
+
useTypeImports: true,
|
|
852
|
+
scalars: {
|
|
853
|
+
...rootConfig.scalars
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
const promises = outputs.map(async (output) => {
|
|
858
|
+
const result2 = await _core.codegen.call(void 0, {
|
|
859
|
+
...output,
|
|
860
|
+
cache
|
|
861
|
+
});
|
|
862
|
+
return new (0, _generatorsdk.File)(output.filename, result2, "graphql");
|
|
863
|
+
});
|
|
864
|
+
return Promise.all(promises);
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// index.ts
|
|
868
|
+
function graphqlPlugin() {
|
|
869
|
+
return _generatorsdk.createPluginV1.call(void 0, {
|
|
870
|
+
name: "graphql",
|
|
871
|
+
actionName: "GraphQL modules",
|
|
872
|
+
watch: (options, watcher, reload) => {
|
|
873
|
+
const handleChange = (file) => {
|
|
874
|
+
if (_generatorsdk.isMatch.call(void 0, file.relativePath, options.schemas)) {
|
|
875
|
+
reload(file);
|
|
876
|
+
}
|
|
877
|
+
};
|
|
878
|
+
watcher.on("update", handleChange);
|
|
879
|
+
watcher.on("delete", handleChange);
|
|
880
|
+
},
|
|
881
|
+
generate: async (ctx, next) => {
|
|
882
|
+
const files = await generate(ctx.generatorOptions);
|
|
883
|
+
ctx.fileManager.add(...files);
|
|
884
|
+
return next();
|
|
885
|
+
}
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
exports.graphqlPlugin = graphqlPlugin;
|
|
891
|
+
//# sourceMappingURL=index.cjs.map
|