1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.isOneOfInputObjectType = exports.wrapTypeNodeWithModifiers = exports.removeDescription = exports.wrapTypeWithModifiers = exports.hasConditionalDirectives = exports.getPossibleTypes = exports.separateSelectionSet = exports.getFieldNodeNameValue = exports.mergeSelectionSets = exports.REQUIRE_FIELDS_TYPE = exports.OMIT_TYPE = exports.stripMapperTypeInterpolation = exports.getRootTypeNames = exports.buildScalars = exports.buildScalarsFromConfig = exports.convertNameParts = exports.getBaseTypeNode = exports.DeclarationBlock = exports.transformComment = exports.indentMultiline = exports.indent = exports.breakLine = exports.wrapWithSingleQuotes = exports.block = exports.quoteIfNeeded = exports.getConfigValue = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const scalars_js_1 = require("./scalars.js");
|
6 | const mappers_js_1 = require("./mappers.js");
|
7 | const getConfigValue = (value, defaultValue) => {
|
8 | if (value === null || value === undefined) {
|
9 | return defaultValue;
|
10 | }
|
11 | return value;
|
12 | };
|
13 | exports.getConfigValue = getConfigValue;
|
14 | function quoteIfNeeded(array, joinWith = ' & ') {
|
15 | if (array.length === 0) {
|
16 | return '';
|
17 | }
|
18 | if (array.length === 1) {
|
19 | return array[0];
|
20 | }
|
21 | return `(${array.join(joinWith)})`;
|
22 | }
|
23 | exports.quoteIfNeeded = quoteIfNeeded;
|
24 | function block(array) {
|
25 | return array && array.length !== 0 ? '{\n' + array.join('\n') + '\n}' : '';
|
26 | }
|
27 | exports.block = block;
|
28 | function wrapWithSingleQuotes(value, skipNumericCheck = false) {
|
29 | if (skipNumericCheck) {
|
30 | if (typeof value === 'number') {
|
31 | return `${value}`;
|
32 | }
|
33 | return `'${value}'`;
|
34 | }
|
35 | if (typeof value === 'number' ||
|
36 | (typeof value === 'string' && !isNaN(parseInt(value)) && parseFloat(value).toString() === value)) {
|
37 | return `${value}`;
|
38 | }
|
39 | return `'${value}'`;
|
40 | }
|
41 | exports.wrapWithSingleQuotes = wrapWithSingleQuotes;
|
42 | function breakLine(str) {
|
43 | return str + '\n';
|
44 | }
|
45 | exports.breakLine = breakLine;
|
46 | function indent(str, count = 1) {
|
47 | return new Array(count).fill(' ').join('') + str;
|
48 | }
|
49 | exports.indent = indent;
|
50 | function indentMultiline(str, count = 1) {
|
51 | const indentation = new Array(count).fill(' ').join('');
|
52 | const replaceWith = '\n' + indentation;
|
53 | return indentation + str.replace(/\n/g, replaceWith);
|
54 | }
|
55 | exports.indentMultiline = indentMultiline;
|
56 | function transformComment(comment, indentLevel = 0, disabled = false) {
|
57 | if (!comment || comment === '' || disabled) {
|
58 | return '';
|
59 | }
|
60 | if (isStringValueNode(comment)) {
|
61 | comment = comment.value;
|
62 | }
|
63 | comment = comment.split('*/').join('*\\/');
|
64 | let lines = comment.split('\n');
|
65 | if (lines.length === 1) {
|
66 | return indent(`/** ${lines[0]} */\n`, indentLevel);
|
67 | }
|
68 | lines = ['/**', ...lines.map(line => ` * ${line}`), ' */\n'];
|
69 | return stripTrailingSpaces(lines.map(line => indent(line, indentLevel)).join('\n'));
|
70 | }
|
71 | exports.transformComment = transformComment;
|
72 | class DeclarationBlock {
|
73 | constructor(_config) {
|
74 | this._config = _config;
|
75 | this._decorator = null;
|
76 | this._export = false;
|
77 | this._name = null;
|
78 | this._kind = null;
|
79 | this._methodName = null;
|
80 | this._content = null;
|
81 | this._block = null;
|
82 | this._nameGenerics = null;
|
83 | this._comment = null;
|
84 | this._ignoreBlockWrapper = false;
|
85 | this._config = {
|
86 | blockWrapper: '',
|
87 | blockTransformer: block => block,
|
88 | enumNameValueSeparator: ':',
|
89 | ...this._config,
|
90 | };
|
91 | }
|
92 | withDecorator(decorator) {
|
93 | this._decorator = decorator;
|
94 | return this;
|
95 | }
|
96 | export(exp = true) {
|
97 | if (!this._config.ignoreExport) {
|
98 | this._export = exp;
|
99 | }
|
100 | return this;
|
101 | }
|
102 | asKind(kind) {
|
103 | this._kind = kind;
|
104 | return this;
|
105 | }
|
106 | withComment(comment, disabled = false) {
|
107 | const nonEmptyComment = isStringValueNode(comment) ? !!comment.value : !!comment;
|
108 | if (nonEmptyComment && !disabled) {
|
109 | this._comment = transformComment(comment, 0);
|
110 | }
|
111 | return this;
|
112 | }
|
113 | withMethodCall(methodName, ignoreBlockWrapper = false) {
|
114 | this._methodName = methodName;
|
115 | this._ignoreBlockWrapper = ignoreBlockWrapper;
|
116 | return this;
|
117 | }
|
118 | withBlock(block) {
|
119 | this._block = block;
|
120 | return this;
|
121 | }
|
122 | withContent(content) {
|
123 | this._content = content;
|
124 | return this;
|
125 | }
|
126 | withName(name, generics = null) {
|
127 | this._name = name;
|
128 | this._nameGenerics = generics;
|
129 | return this;
|
130 | }
|
131 | get string() {
|
132 | let result = '';
|
133 | if (this._decorator) {
|
134 | result += this._decorator + '\n';
|
135 | }
|
136 | if (this._export) {
|
137 | result += 'export ';
|
138 | }
|
139 | if (this._kind) {
|
140 | let extra = '';
|
141 | let name = '';
|
142 | if (['type', 'const', 'var', 'let'].includes(this._kind)) {
|
143 | extra = '= ';
|
144 | }
|
145 | if (this._name) {
|
146 | name = this._name + (this._nameGenerics || '') + ' ';
|
147 | }
|
148 | result += this._kind + ' ' + name + extra;
|
149 | }
|
150 | if (this._block) {
|
151 | if (this._content) {
|
152 | result += this._content;
|
153 | }
|
154 | const blockWrapper = this._ignoreBlockWrapper ? '' : this._config.blockWrapper;
|
155 | const before = '{' + blockWrapper;
|
156 | const after = blockWrapper + '}';
|
157 | const block = [before, this._block, after].filter(val => !!val).join('\n');
|
158 | if (this._methodName) {
|
159 | result += `${this._methodName}(${this._config.blockTransformer(block)})`;
|
160 | }
|
161 | else {
|
162 | result += this._config.blockTransformer(block);
|
163 | }
|
164 | }
|
165 | else if (this._content) {
|
166 | result += this._content;
|
167 | }
|
168 | else if (this._kind) {
|
169 | result += this._config.blockTransformer('{}');
|
170 | }
|
171 | return stripTrailingSpaces((this._comment ? this._comment : '') +
|
172 | result +
|
173 | (this._kind === 'interface' || this._kind === 'enum' || this._kind === 'namespace' || this._kind === 'function'
|
174 | ? ''
|
175 | : ';') +
|
176 | '\n');
|
177 | }
|
178 | }
|
179 | exports.DeclarationBlock = DeclarationBlock;
|
180 | function getBaseTypeNode(typeNode) {
|
181 | if (typeNode.kind === graphql_1.Kind.LIST_TYPE || typeNode.kind === graphql_1.Kind.NON_NULL_TYPE) {
|
182 | return getBaseTypeNode(typeNode.type);
|
183 | }
|
184 | return typeNode;
|
185 | }
|
186 | exports.getBaseTypeNode = getBaseTypeNode;
|
187 | function convertNameParts(str, func, removeUnderscore = false) {
|
188 | if (removeUnderscore) {
|
189 | return func(str);
|
190 | }
|
191 | return str
|
192 | .split('_')
|
193 | .map(s => func(s))
|
194 | .join('_');
|
195 | }
|
196 | exports.convertNameParts = convertNameParts;
|
197 | function buildScalarsFromConfig(schema, config, defaultScalarsMapping = scalars_js_1.DEFAULT_SCALARS, defaultScalarType = 'any') {
|
198 | return buildScalars(schema, config.scalars, defaultScalarsMapping, config.strictScalars ? null : config.defaultScalarType || defaultScalarType);
|
199 | }
|
200 | exports.buildScalarsFromConfig = buildScalarsFromConfig;
|
201 | function buildScalars(schema, scalarsMapping, defaultScalarsMapping = scalars_js_1.DEFAULT_SCALARS, defaultScalarType = 'any') {
|
202 | const result = {};
|
203 | Object.keys(defaultScalarsMapping).forEach(name => {
|
204 | result[name] = (0, mappers_js_1.parseMapper)(defaultScalarsMapping[name]);
|
205 | });
|
206 | if (schema) {
|
207 | const typeMap = schema.getTypeMap();
|
208 | Object.keys(typeMap)
|
209 | .map(typeName => typeMap[typeName])
|
210 | .filter(type => (0, graphql_1.isScalarType)(type))
|
211 | .map((scalarType) => {
|
212 | var _a;
|
213 | const { name } = scalarType;
|
214 | if (typeof scalarsMapping === 'string') {
|
215 | const value = (0, mappers_js_1.parseMapper)(scalarsMapping + '#' + name, name);
|
216 | result[name] = value;
|
217 | }
|
218 | else if (scalarsMapping && typeof scalarsMapping[name] === 'string') {
|
219 | const value = (0, mappers_js_1.parseMapper)(scalarsMapping[name], name);
|
220 | result[name] = value;
|
221 | }
|
222 | else if (scalarsMapping && scalarsMapping[name]) {
|
223 | result[name] = {
|
224 | isExternal: false,
|
225 | type: JSON.stringify(scalarsMapping[name]),
|
226 | };
|
227 | }
|
228 | else if ((_a = scalarType.extensions) === null || _a === void 0 ? void 0 : _a.codegenScalarType) {
|
229 | result[name] = {
|
230 | isExternal: false,
|
231 | type: scalarType.extensions.codegenScalarType,
|
232 | };
|
233 | }
|
234 | else if (!defaultScalarsMapping[name]) {
|
235 | if (defaultScalarType === null) {
|
236 | throw new Error(`Unknown scalar type ${name}. Please override it using the "scalars" configuration field!`);
|
237 | }
|
238 | result[name] = {
|
239 | isExternal: false,
|
240 | type: defaultScalarType,
|
241 | };
|
242 | }
|
243 | });
|
244 | }
|
245 | else if (scalarsMapping) {
|
246 | if (typeof scalarsMapping === 'string') {
|
247 | throw new Error('Cannot use string scalars mapping when building without a schema');
|
248 | }
|
249 | Object.keys(scalarsMapping).forEach(name => {
|
250 | if (typeof scalarsMapping[name] === 'string') {
|
251 | const value = (0, mappers_js_1.parseMapper)(scalarsMapping[name], name);
|
252 | result[name] = value;
|
253 | }
|
254 | else {
|
255 | result[name] = {
|
256 | isExternal: false,
|
257 | type: JSON.stringify(scalarsMapping[name]),
|
258 | };
|
259 | }
|
260 | });
|
261 | }
|
262 | return result;
|
263 | }
|
264 | exports.buildScalars = buildScalars;
|
265 | function isStringValueNode(node) {
|
266 | return node && typeof node === 'object' && node.kind === graphql_1.Kind.STRING;
|
267 | }
|
268 |
|
269 | function getRootTypeNames(schema) {
|
270 | return [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
|
271 | .filter(t => t)
|
272 | .map(t => t.name);
|
273 | }
|
274 | exports.getRootTypeNames = getRootTypeNames;
|
275 | function stripMapperTypeInterpolation(identifier) {
|
276 | return identifier.trim().replace(/<{.*}>/, '');
|
277 | }
|
278 | exports.stripMapperTypeInterpolation = stripMapperTypeInterpolation;
|
279 | exports.OMIT_TYPE = 'export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;';
|
280 | exports.REQUIRE_FIELDS_TYPE = `export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };`;
|
281 |
|
282 |
|
283 |
|
284 | function mergeSelectionSets(selectionSet1, selectionSet2) {
|
285 | const newSelections = [...selectionSet1.selections];
|
286 | for (let selection2 of selectionSet2.selections) {
|
287 | if (selection2.kind === 'FragmentSpread' || selection2.kind === 'InlineFragment') {
|
288 | newSelections.push(selection2);
|
289 | continue;
|
290 | }
|
291 | if (selection2.kind !== 'Field') {
|
292 | throw new TypeError('Invalid state.');
|
293 | }
|
294 | const match = newSelections.find(selection1 => selection1.kind === 'Field' &&
|
295 | (0, exports.getFieldNodeNameValue)(selection1) === (0, exports.getFieldNodeNameValue)(selection2));
|
296 | if (match &&
|
297 |
|
298 | match.kind === 'Field' &&
|
299 | match.selectionSet &&
|
300 | selection2.selectionSet) {
|
301 | selection2 = {
|
302 | ...selection2,
|
303 | selectionSet: mergeSelectionSets(match.selectionSet, selection2.selectionSet),
|
304 | };
|
305 | }
|
306 | newSelections.push(selection2);
|
307 | }
|
308 | return {
|
309 | kind: graphql_1.Kind.SELECTION_SET,
|
310 | selections: newSelections,
|
311 | };
|
312 | }
|
313 | exports.mergeSelectionSets = mergeSelectionSets;
|
314 | const getFieldNodeNameValue = (node) => {
|
315 | return (node.alias || node.name).value;
|
316 | };
|
317 | exports.getFieldNodeNameValue = getFieldNodeNameValue;
|
318 | function separateSelectionSet(selections) {
|
319 | return {
|
320 | fields: selections.filter(s => s.kind === graphql_1.Kind.FIELD),
|
321 | inlines: selections.filter(s => s.kind === graphql_1.Kind.INLINE_FRAGMENT),
|
322 | spreads: selections.filter(s => s.kind === graphql_1.Kind.FRAGMENT_SPREAD),
|
323 | };
|
324 | }
|
325 | exports.separateSelectionSet = separateSelectionSet;
|
326 | function getPossibleTypes(schema, type) {
|
327 | if ((0, graphql_1.isListType)(type) || (0, graphql_1.isNonNullType)(type)) {
|
328 | return getPossibleTypes(schema, type.ofType);
|
329 | }
|
330 | if ((0, graphql_1.isObjectType)(type)) {
|
331 | return [type];
|
332 | }
|
333 | if ((0, graphql_1.isAbstractType)(type)) {
|
334 | return schema.getPossibleTypes(type);
|
335 | }
|
336 | return [];
|
337 | }
|
338 | exports.getPossibleTypes = getPossibleTypes;
|
339 | function hasConditionalDirectives(field) {
|
340 | var _a;
|
341 | const CONDITIONAL_DIRECTIVES = ['skip', 'include'];
|
342 | return (_a = field.directives) === null || _a === void 0 ? void 0 : _a.some(directive => CONDITIONAL_DIRECTIVES.includes(directive.name.value));
|
343 | }
|
344 | exports.hasConditionalDirectives = hasConditionalDirectives;
|
345 | function wrapTypeWithModifiers(baseType, type, options) {
|
346 | let currentType = type;
|
347 | const modifiers = [];
|
348 | while (currentType) {
|
349 | if ((0, graphql_1.isNonNullType)(currentType)) {
|
350 | currentType = currentType.ofType;
|
351 | }
|
352 | else {
|
353 | modifiers.push(options.wrapOptional);
|
354 | }
|
355 | if ((0, graphql_1.isListType)(currentType)) {
|
356 | modifiers.push(options.wrapArray);
|
357 | currentType = currentType.ofType;
|
358 | }
|
359 | else {
|
360 | break;
|
361 | }
|
362 | }
|
363 | return modifiers.reduceRight((result, modifier) => modifier(result), baseType);
|
364 | }
|
365 | exports.wrapTypeWithModifiers = wrapTypeWithModifiers;
|
366 | function removeDescription(nodes) {
|
367 | return nodes.map(node => ({ ...node, description: undefined }));
|
368 | }
|
369 | exports.removeDescription = removeDescription;
|
370 | function wrapTypeNodeWithModifiers(baseType, typeNode) {
|
371 | switch (typeNode.kind) {
|
372 | case graphql_1.Kind.NAMED_TYPE: {
|
373 | return `Maybe<${baseType}>`;
|
374 | }
|
375 | case graphql_1.Kind.NON_NULL_TYPE: {
|
376 | const innerType = wrapTypeNodeWithModifiers(baseType, typeNode.type);
|
377 | return clearOptional(innerType);
|
378 | }
|
379 | case graphql_1.Kind.LIST_TYPE: {
|
380 | const innerType = wrapTypeNodeWithModifiers(baseType, typeNode.type);
|
381 | return `Maybe<Array<${innerType}>>`;
|
382 | }
|
383 | }
|
384 | }
|
385 | exports.wrapTypeNodeWithModifiers = wrapTypeNodeWithModifiers;
|
386 | function clearOptional(str) {
|
387 | const rgx = new RegExp(`^Maybe<(.*?)>$`, 'i');
|
388 | if (str.startsWith(`Maybe`)) {
|
389 | return str.replace(rgx, '$1');
|
390 | }
|
391 | return str;
|
392 | }
|
393 | function stripTrailingSpaces(str) {
|
394 | return str.replace(/ +\n/g, '\n');
|
395 | }
|
396 | const isOneOfTypeCache = new WeakMap();
|
397 | function isOneOfInputObjectType(namedType) {
|
398 | var _a, _b;
|
399 | if (!namedType) {
|
400 | return false;
|
401 | }
|
402 | let isOneOfType = isOneOfTypeCache.get(namedType);
|
403 | if (isOneOfType !== undefined) {
|
404 | return isOneOfType;
|
405 | }
|
406 | isOneOfType =
|
407 | (0, graphql_1.isInputObjectType)(namedType) &&
|
408 | (namedType.isOneOf ||
|
409 | ((_b = (_a = namedType.astNode) === null || _a === void 0 ? void 0 : _a.directives) === null || _b === void 0 ? void 0 : _b.some(d => d.name.value === 'oneOf')));
|
410 | isOneOfTypeCache.set(namedType, isOneOfType);
|
411 | return isOneOfType;
|
412 | }
|
413 | exports.isOneOfInputObjectType = isOneOfInputObjectType;
|