@graphql-codegen/visitor-plugin-common 6.2.4 → 6.2.5-alpha-20260402144834-a9c46613cbf89fefee54fbcb7b8264006c2345db
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/cjs/base-documents-visitor.js +3 -1
- package/cjs/base-resolvers-visitor.js +66 -22
- package/cjs/base-types-visitor.js +21 -7
- package/cjs/base-visitor.js +1 -1
- package/cjs/client-side-base-visitor.js +16 -8
- package/cjs/enum-values.js +2 -1
- package/cjs/mappers.js +9 -3
- package/cjs/naming.js +3 -2
- package/cjs/selection-set-processor/pre-resolve-types.js +1 -1
- package/cjs/selection-set-to-object.js +70 -25
- package/cjs/utils.js +19 -5
- package/cjs/variables-to-object.js +8 -4
- package/esm/base-documents-visitor.js +4 -2
- package/esm/base-resolvers-visitor.js +67 -23
- package/esm/base-types-visitor.js +21 -7
- package/esm/base-visitor.js +1 -1
- package/esm/client-side-base-visitor.js +16 -8
- package/esm/enum-values.js +2 -1
- package/esm/mappers.js +9 -3
- package/esm/naming.js +3 -2
- package/esm/selection-set-processor/pre-resolve-types.js +1 -1
- package/esm/selection-set-to-object.js +70 -25
- package/esm/utils.js +19 -5
- package/esm/variables-to-object.js +8 -4
- package/package.json +4 -4
- package/typings/base-documents-visitor.d.cts +1 -1
- package/typings/base-documents-visitor.d.ts +1 -1
- package/typings/base-resolvers-visitor.d.cts +1 -1
- package/typings/base-resolvers-visitor.d.ts +1 -1
- package/typings/client-side-base-visitor.d.cts +1 -1
- package/typings/client-side-base-visitor.d.ts +1 -1
- package/typings/optimize-operations.d.cts +1 -1
- package/typings/optimize-operations.d.ts +1 -1
- package/typings/types.d.cts +1 -1
- package/typings/types.d.ts +1 -1
- package/typings/utils.d.cts +2 -2
- package/typings/utils.d.ts +2 -2
|
@@ -40,7 +40,9 @@ class BaseDocumentsVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
40
40
|
globalNamespace: !!rawConfig.globalNamespace,
|
|
41
41
|
operationResultSuffix: (0, utils_js_1.getConfigValue)(rawConfig.operationResultSuffix, ''),
|
|
42
42
|
scalars: (0, utils_js_1.buildScalarsFromConfig)(_schema, rawConfig, defaultScalars),
|
|
43
|
-
customDirectives: (0, utils_js_1.getConfigValue)(rawConfig.customDirectives, {
|
|
43
|
+
customDirectives: (0, utils_js_1.getConfigValue)(rawConfig.customDirectives, {
|
|
44
|
+
apolloUnmask: false,
|
|
45
|
+
}),
|
|
44
46
|
...(additionalConfig || {}),
|
|
45
47
|
});
|
|
46
48
|
this._schema = _schema;
|
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseResolversVisitor = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
6
|
-
const utils_1 = require("@graphql-tools/utils");
|
|
7
5
|
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
8
6
|
const graphql_1 = require("graphql");
|
|
7
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
8
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
9
|
+
const avoid_optionals_js_1 = require("./avoid-optionals.js");
|
|
9
10
|
const base_visitor_js_1 = require("./base-visitor.js");
|
|
10
11
|
const enum_values_js_1 = require("./enum-values.js");
|
|
11
12
|
const mappers_js_1 = require("./mappers.js");
|
|
12
13
|
const scalars_js_1 = require("./scalars.js");
|
|
13
14
|
const utils_js_1 = require("./utils.js");
|
|
14
15
|
const variables_to_object_js_1 = require("./variables-to-object.js");
|
|
15
|
-
const avoid_optionals_js_1 = require("./avoid-optionals.js");
|
|
16
16
|
class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
17
17
|
_schema;
|
|
18
18
|
_declarationBlockConfig = {};
|
|
@@ -151,7 +151,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
151
151
|
}
|
|
152
152
|
convertName(node, options, applyNamespacedImport = false) {
|
|
153
153
|
const sourceType = super.convertName(node, options);
|
|
154
|
-
return `${applyNamespacedImport && this.config.namespacedImportName
|
|
154
|
+
return `${applyNamespacedImport && this.config.namespacedImportName
|
|
155
|
+
? this.config.namespacedImportName + '.'
|
|
156
|
+
: ''}${sourceType}`;
|
|
155
157
|
}
|
|
156
158
|
// Kamil: this one is heeeeavvyyyy
|
|
157
159
|
createResolversFields({ applyWrapper, clearWrapper, getTypeToUse, currentType, shouldInclude, onNotMappedObjectType, }) {
|
|
@@ -176,7 +178,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
176
178
|
const isScalar = this.config.scalars[typeName];
|
|
177
179
|
const hasDefaultMapper = !!this.config.defaultMapper?.type;
|
|
178
180
|
// Check for mappers first, even for root types, to allow overriding rootValueType
|
|
179
|
-
if (isMapped &&
|
|
181
|
+
if (isMapped &&
|
|
182
|
+
this.config.mappers[typeName].type &&
|
|
183
|
+
!hasPlaceholder(this.config.mappers[typeName].type)) {
|
|
180
184
|
this.markMapperAsUsed(typeName);
|
|
181
185
|
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
|
|
182
186
|
return prev;
|
|
@@ -228,13 +232,16 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
228
232
|
shouldInclude,
|
|
229
233
|
});
|
|
230
234
|
// If relevantFields, puts ResolverTypeWrapper on top of an entire type
|
|
231
|
-
let internalType = relevantFields.length > 0
|
|
235
|
+
let internalType = relevantFields.length > 0
|
|
236
|
+
? this.replaceFieldsInType(prev[typeName], relevantFields)
|
|
237
|
+
: prev[typeName];
|
|
232
238
|
if (isMapped) {
|
|
233
239
|
// replace the placeholder with the actual type
|
|
234
240
|
if (hasPlaceholder(internalType)) {
|
|
235
241
|
internalType = replacePlaceholder(internalType, typeName);
|
|
236
242
|
}
|
|
237
|
-
if (this.config.mappers[typeName].type &&
|
|
243
|
+
if (this.config.mappers[typeName].type &&
|
|
244
|
+
hasPlaceholder(this.config.mappers[typeName].type)) {
|
|
238
245
|
internalType = replacePlaceholder(this.config.mappers[typeName].type, internalType);
|
|
239
246
|
}
|
|
240
247
|
}
|
|
@@ -264,19 +271,25 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
264
271
|
}
|
|
265
272
|
replaceFieldsInType(typeName, relevantFields) {
|
|
266
273
|
this._globalDeclarations.add(utils_js_1.OMIT_TYPE);
|
|
267
|
-
return `Omit<${typeName}, ${relevantFields
|
|
274
|
+
return `Omit<${typeName}, ${relevantFields
|
|
275
|
+
.map(f => `'${f.fieldName}'`)
|
|
276
|
+
.join(' | ')}> & { ${relevantFields
|
|
268
277
|
.map(f => `${f.fieldName}${f.addOptionalSign ? '?' : ''}: ${f.replaceWithType}`)
|
|
269
278
|
.join(', ')} }`;
|
|
270
279
|
}
|
|
271
280
|
applyMaybe(str) {
|
|
272
|
-
const namespacedImportPrefix = this.config.namespacedImportName
|
|
281
|
+
const namespacedImportPrefix = this.config.namespacedImportName
|
|
282
|
+
? this.config.namespacedImportName + '.'
|
|
283
|
+
: '';
|
|
273
284
|
return `${namespacedImportPrefix}Maybe<${str}>`;
|
|
274
285
|
}
|
|
275
286
|
applyResolverTypeWrapper(str) {
|
|
276
287
|
return `ResolverTypeWrapper<${this.clearResolverTypeWrapper(str)}>`;
|
|
277
288
|
}
|
|
278
289
|
clearMaybe(str) {
|
|
279
|
-
const namespacedImportPrefix = this.config.namespacedImportName
|
|
290
|
+
const namespacedImportPrefix = this.config.namespacedImportName
|
|
291
|
+
? this.config.namespacedImportName + '.'
|
|
292
|
+
: '';
|
|
280
293
|
if (str.startsWith(`${namespacedImportPrefix}Maybe<`)) {
|
|
281
294
|
const maybeRe = new RegExp(`${namespacedImportPrefix.replace('.', '\\.')}Maybe<(.*?)>$`);
|
|
282
295
|
return str.replace(maybeRe, '$1');
|
|
@@ -355,7 +368,10 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
355
368
|
}
|
|
356
369
|
// 2c. If type is mapped with placeholder, use the "type with maybe Omit" as {T}
|
|
357
370
|
if (isTypeMapped && hasPlaceholder(isTypeMapped.type)) {
|
|
358
|
-
return {
|
|
371
|
+
return {
|
|
372
|
+
typename: type.name,
|
|
373
|
+
typeValue: replacePlaceholder(isTypeMapped.type, typeValue),
|
|
374
|
+
};
|
|
359
375
|
}
|
|
360
376
|
// 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
|
|
361
377
|
const hasDefaultMapper = !!this.config.defaultMapper?.type;
|
|
@@ -370,7 +386,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
370
386
|
return { typename: type.name, typeValue };
|
|
371
387
|
})
|
|
372
388
|
.map(({ typename, typeValue }) => {
|
|
373
|
-
const nonOptionalTypenameModifier = isTypenameNonOptional
|
|
389
|
+
const nonOptionalTypenameModifier = isTypenameNonOptional
|
|
390
|
+
? ` & { __typename: '${typename}' }`
|
|
391
|
+
: '';
|
|
374
392
|
return `( ${typeValue}${nonOptionalTypenameModifier} )`; // Must wrap every type in explicit "( )" to separate them
|
|
375
393
|
});
|
|
376
394
|
const result = members.length === 0
|
|
@@ -390,7 +408,10 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
390
408
|
if (items.length === 3) {
|
|
391
409
|
const [path, source, contextTypeName] = items;
|
|
392
410
|
const sourceStr = isScoped ? `\\#${source}` : source;
|
|
393
|
-
return {
|
|
411
|
+
return {
|
|
412
|
+
...prev,
|
|
413
|
+
[path]: (0, mappers_js_1.parseMapper)(`${sourceStr}#${contextTypeName}`),
|
|
414
|
+
};
|
|
394
415
|
}
|
|
395
416
|
const [path, contextType] = items;
|
|
396
417
|
return { ...prev, [path]: (0, mappers_js_1.parseMapper)(contextType) };
|
|
@@ -406,7 +427,10 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
406
427
|
if (items.length === 3) {
|
|
407
428
|
const [path, source, contextTypeName] = items;
|
|
408
429
|
const sourceStr = isScoped ? `\\#${source}` : source;
|
|
409
|
-
return {
|
|
430
|
+
return {
|
|
431
|
+
...prev,
|
|
432
|
+
[path]: (0, mappers_js_1.parseMapper)(`${sourceStr}#${contextTypeName}`),
|
|
433
|
+
};
|
|
410
434
|
}
|
|
411
435
|
const [path, contextType] = items;
|
|
412
436
|
return { ...prev, [path]: (0, mappers_js_1.parseMapper)(contextType) };
|
|
@@ -513,7 +537,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
513
537
|
return Array.from(this._globalDeclarations);
|
|
514
538
|
}
|
|
515
539
|
isMapperImported(groupedMappers, identifier, source) {
|
|
516
|
-
const exists = groupedMappers[source]
|
|
540
|
+
const exists = groupedMappers[source]
|
|
541
|
+
? !!groupedMappers[source].find(m => m.identifier === identifier)
|
|
542
|
+
: false;
|
|
517
543
|
const existsFromEnums = !!Object.keys(this.config.enumValues)
|
|
518
544
|
.map(key => this.config.enumValues[key])
|
|
519
545
|
.find(o => o.sourceFile === source && o.typeIdentifier === identifier);
|
|
@@ -528,7 +554,10 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
528
554
|
}
|
|
529
555
|
};
|
|
530
556
|
for (const { mapper } of Object.keys(this.config.mappers)
|
|
531
|
-
.map(gqlTypeName => ({
|
|
557
|
+
.map(gqlTypeName => ({
|
|
558
|
+
gqlType: gqlTypeName,
|
|
559
|
+
mapper: this.config.mappers[gqlTypeName],
|
|
560
|
+
}))
|
|
532
561
|
.filter(({ mapper }) => mapper.isExternal)) {
|
|
533
562
|
const externalMapper = mapper;
|
|
534
563
|
const identifier = (0, utils_js_1.stripMapperTypeInterpolation)(externalMapper.import);
|
|
@@ -676,7 +705,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
676
705
|
const parentType = this.schema.getType(parentName);
|
|
677
706
|
const meta = {};
|
|
678
707
|
const typeName = node.name.value;
|
|
679
|
-
const fieldsToGenerate = this._federation.findFieldNodesToGenerate({
|
|
708
|
+
const fieldsToGenerate = this._federation.findFieldNodesToGenerate({
|
|
709
|
+
node: parentNode,
|
|
710
|
+
});
|
|
680
711
|
const shouldGenerateField = fieldsToGenerate.some(field => field.name.value === typeName) ||
|
|
681
712
|
this._federation.isResolveReferenceField(node);
|
|
682
713
|
if (!shouldGenerateField) {
|
|
@@ -737,7 +768,12 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
737
768
|
name: typeName,
|
|
738
769
|
modifier: avoidResolverOptionals ? '' : '?',
|
|
739
770
|
type: resolverType,
|
|
740
|
-
genericTypes: [
|
|
771
|
+
genericTypes: [
|
|
772
|
+
mappedTypeKey,
|
|
773
|
+
this.getParentTypeForSignature(node),
|
|
774
|
+
contextType,
|
|
775
|
+
argsType,
|
|
776
|
+
].filter(f => f),
|
|
741
777
|
};
|
|
742
778
|
if (this._federation.isResolveReferenceField(node)) {
|
|
743
779
|
if (!this._federation.getMeta()[parentType.name].hasResolveReference) {
|
|
@@ -815,7 +851,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
815
851
|
}
|
|
816
852
|
ObjectTypeDefinition(node) {
|
|
817
853
|
const typeName = node.name.value;
|
|
818
|
-
const fieldsToGenerate = this._federation.findFieldNodesToGenerate({
|
|
854
|
+
const fieldsToGenerate = this._federation.findFieldNodesToGenerate({
|
|
855
|
+
node,
|
|
856
|
+
});
|
|
819
857
|
if (fieldsToGenerate.length === 0) {
|
|
820
858
|
return null;
|
|
821
859
|
}
|
|
@@ -967,7 +1005,9 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
967
1005
|
if (!this.config.mappers[rawTypeName] && !this.config.enumValues[rawTypeName]) {
|
|
968
1006
|
return null;
|
|
969
1007
|
}
|
|
970
|
-
const name = this.convertName(node, {
|
|
1008
|
+
const name = this.convertName(node, {
|
|
1009
|
+
suffix: this.config.resolverTypeSuffix,
|
|
1010
|
+
});
|
|
971
1011
|
this._collectedResolvers[rawTypeName] = {
|
|
972
1012
|
typename: name,
|
|
973
1013
|
baseGeneratedTypename: name,
|
|
@@ -1101,7 +1141,9 @@ function checkIfObjectTypeHasAbstractTypesRecursively(baseType, result) {
|
|
|
1101
1141
|
result.checkedTypesWithNestedAbstractTypes[baseType.name].checkStatus === 'no')) {
|
|
1102
1142
|
return result.checkedTypesWithNestedAbstractTypes[baseType.name].checkStatus === 'yes';
|
|
1103
1143
|
}
|
|
1104
|
-
result.checkedTypesWithNestedAbstractTypes[baseType.name] ||= {
|
|
1144
|
+
result.checkedTypesWithNestedAbstractTypes[baseType.name] ||= {
|
|
1145
|
+
checkStatus: 'checking',
|
|
1146
|
+
};
|
|
1105
1147
|
let atLeastOneFieldWithAbstractType = false;
|
|
1106
1148
|
const fields = baseType.getFields();
|
|
1107
1149
|
for (const field of Object.values(fields)) {
|
|
@@ -1120,7 +1162,9 @@ function checkIfObjectTypeHasAbstractTypesRecursively(baseType, result) {
|
|
|
1120
1162
|
continue;
|
|
1121
1163
|
}
|
|
1122
1164
|
else {
|
|
1123
|
-
result.checkedTypesWithNestedAbstractTypes[fieldBaseType.name] = {
|
|
1165
|
+
result.checkedTypesWithNestedAbstractTypes[fieldBaseType.name] = {
|
|
1166
|
+
checkStatus: 'checking',
|
|
1167
|
+
};
|
|
1124
1168
|
}
|
|
1125
1169
|
// If the field is an abstract type, then both the field type and parent type are abstract types
|
|
1126
1170
|
if ((0, graphql_1.isInterfaceType)(fieldBaseType) || (0, graphql_1.isUnionType)(fieldBaseType)) {
|
|
@@ -84,7 +84,9 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
84
84
|
const inputScalarValue = this.config.scalars[scalarName].input.type;
|
|
85
85
|
const outputScalarValue = this.config.scalars[scalarName].output.type;
|
|
86
86
|
const scalarType = this._schema.getType(scalarName);
|
|
87
|
-
const comment = scalarType?.astNode && scalarType.description
|
|
87
|
+
const comment = scalarType?.astNode && scalarType.description
|
|
88
|
+
? (0, utils_js_1.transformComment)(scalarType.description, 1)
|
|
89
|
+
: '';
|
|
88
90
|
const { scalar } = this._parsedConfig.declarationKind;
|
|
89
91
|
return (comment +
|
|
90
92
|
(0, utils_js_1.indent)(`${scalarName}: { input: ${inputScalarValue}${this.getPunctuation(scalar)} output: ${outputScalarValue}${this.getPunctuation(scalar)} }`));
|
|
@@ -104,7 +106,9 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
104
106
|
const allDirectives = [];
|
|
105
107
|
for (const [directiveName, parsedMapper] of directiveEntries) {
|
|
106
108
|
const directiveType = this._schema.getDirective(directiveName);
|
|
107
|
-
const comment = directiveType?.astNode && directiveType.description
|
|
109
|
+
const comment = directiveType?.astNode && directiveType.description
|
|
110
|
+
? (0, utils_js_1.transformComment)(directiveType.description, 1)
|
|
111
|
+
: '';
|
|
108
112
|
const { directive } = this._parsedConfig.declarationKind;
|
|
109
113
|
allDirectives.push(comment + (0, utils_js_1.indent)(`${directiveName}: ${parsedMapper.type}${this.getPunctuation(directive)}`));
|
|
110
114
|
}
|
|
@@ -176,7 +180,7 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
176
180
|
return '';
|
|
177
181
|
const originalNode = parent[key];
|
|
178
182
|
const possibleTypes = originalNode.types
|
|
179
|
-
.map(t =>
|
|
183
|
+
.map(t => this.scalars[t.name.value] ? this._getScalar(t.name.value, 'output') : this.convertName(t))
|
|
180
184
|
.join(' | ');
|
|
181
185
|
return new utils_js_1.DeclarationBlock(this._declarationBlockConfig)
|
|
182
186
|
.export()
|
|
@@ -203,7 +207,9 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
203
207
|
: []),
|
|
204
208
|
...node.fields,
|
|
205
209
|
];
|
|
206
|
-
const interfacesNames = originalNode.interfaces
|
|
210
|
+
const interfacesNames = originalNode.interfaces
|
|
211
|
+
? originalNode.interfaces.map(i => this.convertName(i))
|
|
212
|
+
: [];
|
|
207
213
|
const declarationBlock = new utils_js_1.DeclarationBlock(this._declarationBlockConfig)
|
|
208
214
|
.export()
|
|
209
215
|
.asKind(type)
|
|
@@ -228,7 +234,10 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
228
234
|
if (this.config.onlyOperationTypes || this.config.onlyEnums)
|
|
229
235
|
return '';
|
|
230
236
|
const originalNode = parent[key];
|
|
231
|
-
return [
|
|
237
|
+
return [
|
|
238
|
+
this.getObjectTypeDeclarationBlock(node, originalNode).string,
|
|
239
|
+
this.buildArgumentsBlock(originalNode),
|
|
240
|
+
]
|
|
232
241
|
.filter(f => f)
|
|
233
242
|
.join('\n\n');
|
|
234
243
|
}
|
|
@@ -244,7 +253,10 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
244
253
|
if (this.config.onlyOperationTypes || this.config.onlyEnums)
|
|
245
254
|
return '';
|
|
246
255
|
const originalNode = parent[key];
|
|
247
|
-
return [
|
|
256
|
+
return [
|
|
257
|
+
this.getInterfaceTypeDeclarationBlock(node, originalNode).string,
|
|
258
|
+
this.buildArgumentsBlock(originalNode),
|
|
259
|
+
]
|
|
248
260
|
.filter(f => f)
|
|
249
261
|
.join('\n\n');
|
|
250
262
|
}
|
|
@@ -282,7 +294,9 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
282
294
|
const mappedValue = this.config.enumValues[enumName];
|
|
283
295
|
if (mappedValue.sourceFile) {
|
|
284
296
|
if (mappedValue.isDefault) {
|
|
285
|
-
return [
|
|
297
|
+
return [
|
|
298
|
+
this._buildTypeImport(mappedValue.typeIdentifier, mappedValue.sourceFile, true),
|
|
299
|
+
];
|
|
286
300
|
}
|
|
287
301
|
return this.handleEnumValueMapper(mappedValue.typeIdentifier, mappedValue.importIdentifier, mappedValue.sourceIdentifier, mappedValue.sourceFile);
|
|
288
302
|
}
|
package/cjs/base-visitor.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseVisitor = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
6
|
-
const naming_js_1 = require("./naming.js");
|
|
7
6
|
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
7
|
+
const naming_js_1 = require("./naming.js");
|
|
8
8
|
class BaseVisitor {
|
|
9
9
|
_parsedConfig;
|
|
10
10
|
_declarationBlockConfig = {};
|
|
@@ -3,16 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ClientSideBaseVisitor = exports.DocumentMode = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const path_1 = require("path");
|
|
6
|
-
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
7
|
-
const optimize_1 = require("@graphql-tools/optimize");
|
|
8
6
|
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
9
7
|
const change_case_all_1 = require("change-case-all");
|
|
10
8
|
const dependency_graph_1 = require("dependency-graph");
|
|
11
9
|
const graphql_1 = require("graphql");
|
|
12
10
|
const graphql_tag_1 = tslib_1.__importDefault(require("graphql-tag"));
|
|
11
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
12
|
+
const optimize_1 = require("@graphql-tools/optimize");
|
|
13
13
|
const base_visitor_js_1 = require("./base-visitor.js");
|
|
14
|
-
const utils_js_1 = require("./utils.js");
|
|
15
14
|
const imports_js_1 = require("./imports.js");
|
|
15
|
+
const utils_js_1 = require("./utils.js");
|
|
16
16
|
graphql_tag_1.default.enableExperimentalFragmentVariables();
|
|
17
17
|
var DocumentMode;
|
|
18
18
|
(function (DocumentMode) {
|
|
@@ -92,7 +92,8 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
92
92
|
}
|
|
93
93
|
_includeFragments(fragments) {
|
|
94
94
|
if (fragments && fragments.length > 0) {
|
|
95
|
-
if (this.config.documentMode === DocumentMode.documentNode ||
|
|
95
|
+
if (this.config.documentMode === DocumentMode.documentNode ||
|
|
96
|
+
this.config.documentMode === DocumentMode.string) {
|
|
96
97
|
return Array.from(this._fragments.values())
|
|
97
98
|
.filter(f => fragments.includes(this.getFragmentVariableName(f.name)))
|
|
98
99
|
.map(fragment => (0, graphql_1.print)(fragment.node))
|
|
@@ -109,7 +110,8 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
109
110
|
return documentStr;
|
|
110
111
|
}
|
|
111
112
|
_gql(node) {
|
|
112
|
-
const includeNestedFragments = this.config.documentMode === DocumentMode.documentNode ||
|
|
113
|
+
const includeNestedFragments = this.config.documentMode === DocumentMode.documentNode ||
|
|
114
|
+
this.config.documentMode === DocumentMode.string;
|
|
113
115
|
const fragmentNames = this._extractFragments(node, includeNestedFragments);
|
|
114
116
|
const fragments = this._transformFragments(fragmentNames);
|
|
115
117
|
const doc = this._prepareDocument(`
|
|
@@ -160,7 +162,10 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
160
162
|
if (this.config.documentMode === DocumentMode.string) {
|
|
161
163
|
if (node.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
|
|
162
164
|
const meta = this._getGraphQLCodegenMetadata(node, (0, graphql_tag_1.default)([doc]).definitions);
|
|
163
|
-
return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify({
|
|
165
|
+
return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify({
|
|
166
|
+
fragmentName: node.name.value,
|
|
167
|
+
...meta,
|
|
168
|
+
})})`;
|
|
164
169
|
}
|
|
165
170
|
if (this._onExecutableDocumentNode && node.kind === graphql_1.Kind.OPERATION_DEFINITION) {
|
|
166
171
|
const meta = this._getGraphQLCodegenMetadata(node, (0, graphql_tag_1.default)([doc]).definitions);
|
|
@@ -332,7 +337,8 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
332
337
|
}
|
|
333
338
|
case DocumentMode.external: {
|
|
334
339
|
if (this._collectedOperations.length > 0) {
|
|
335
|
-
if (this.config.importDocumentNodeExternallyFrom === 'near-operation-file' &&
|
|
340
|
+
if (this.config.importDocumentNodeExternallyFrom === 'near-operation-file' &&
|
|
341
|
+
this._documents.length === 1) {
|
|
336
342
|
let documentPath = `./${this.clearExtension((0, path_1.basename)(this._documents[0].location))}`;
|
|
337
343
|
documentPath += (0, plugin_helpers_1.normalizeImportExtension)({
|
|
338
344
|
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
@@ -353,7 +359,9 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
353
359
|
default:
|
|
354
360
|
break;
|
|
355
361
|
}
|
|
356
|
-
const excludeFragments = options.excludeFragments ||
|
|
362
|
+
const excludeFragments = options.excludeFragments ||
|
|
363
|
+
this.config.globalNamespace ||
|
|
364
|
+
this.config.documentMode !== DocumentMode.graphQLTag;
|
|
357
365
|
if (!excludeFragments) {
|
|
358
366
|
const importExtension = (0, plugin_helpers_1.normalizeImportExtension)({
|
|
359
367
|
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
package/cjs/enum-values.js
CHANGED
|
@@ -17,7 +17,8 @@ function parseEnumValues({ schema, mapOrStr = {}, ignoreEnumValuesFromSchema, })
|
|
|
17
17
|
if (value !== name) {
|
|
18
18
|
mapOrStr[enumTypeName] ||= {};
|
|
19
19
|
if (typeof mapOrStr[enumTypeName] !== 'string' && !mapOrStr[enumTypeName][name]) {
|
|
20
|
-
mapOrStr[enumTypeName][name] =
|
|
20
|
+
mapOrStr[enumTypeName][name] =
|
|
21
|
+
typeof value === 'string' ? escapeString(value) : value;
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
24
|
}
|
package/cjs/mappers.js
CHANGED
|
@@ -58,7 +58,9 @@ function isLegacyMode(mapper) {
|
|
|
58
58
|
}
|
|
59
59
|
function parseMapper(mapper, gqlTypeName = null, suffix) {
|
|
60
60
|
if (isExternalMapper(mapper)) {
|
|
61
|
-
const { isNamespace, isDefault, hasAlias, items } = isLegacyMode(mapper)
|
|
61
|
+
const { isNamespace, isDefault, hasAlias, items } = isLegacyMode(mapper)
|
|
62
|
+
? prepareLegacy(mapper)
|
|
63
|
+
: prepare(mapper);
|
|
62
64
|
const mapperKind = isNamespace
|
|
63
65
|
? MapperKind.Namespace
|
|
64
66
|
: isDefault
|
|
@@ -169,12 +171,16 @@ function buildMapperImport(source, types, useTypeImports) {
|
|
|
169
171
|
namedTypes = [{ identifier: `default as ${defaultType.identifier}` }, ...namedTypes];
|
|
170
172
|
}
|
|
171
173
|
// { Foo, Bar as BarModel }
|
|
172
|
-
const namedImports = namedTypes.length
|
|
174
|
+
const namedImports = namedTypes.length
|
|
175
|
+
? `{ ${namedTypes.map(t => t.identifier).join(', ')} }`
|
|
176
|
+
: '';
|
|
173
177
|
// { default as Baz, Foo, Bar as BarModel }
|
|
174
178
|
return `import type ${[namedImports].filter(Boolean).join(', ')} from '${source}';`;
|
|
175
179
|
}
|
|
176
180
|
// { Foo, Bar as BarModel }
|
|
177
|
-
const namedImports = namedTypes.length
|
|
181
|
+
const namedImports = namedTypes.length
|
|
182
|
+
? `{ ${namedTypes.map(t => t.identifier).join(', ')} }`
|
|
183
|
+
: '';
|
|
178
184
|
// Baz
|
|
179
185
|
const defaultImport = defaultType ? defaultType.identifier : '';
|
|
180
186
|
// Baz, { Foo, Bar as BarModel }
|
package/cjs/naming.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.convertFactory = convertFactory;
|
|
4
|
-
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
5
4
|
const change_case_all_1 = require("change-case-all");
|
|
5
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
6
6
|
const utils_js_1 = require("./utils.js");
|
|
7
7
|
function getKind(node) {
|
|
8
8
|
if (typeof node === 'string') {
|
|
@@ -79,7 +79,8 @@ function convertFactory(config) {
|
|
|
79
79
|
if (typeof config.namingConvention === 'object') {
|
|
80
80
|
if (!config.namingConvention[type]) {
|
|
81
81
|
return (str, opts = {}) => {
|
|
82
|
-
const transformUnderscore = config.namingConvention.transformUnderscore ||
|
|
82
|
+
const transformUnderscore = config.namingConvention.transformUnderscore ||
|
|
83
|
+
opts?.transformUnderscore;
|
|
83
84
|
return (0, utils_js_1.convertNameParts)(str, change_case_all_1.pascalCase, (0, utils_js_1.getConfigValue)(transformUnderscore, false));
|
|
84
85
|
};
|
|
85
86
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PreResolveTypesProcessor = void 0;
|
|
4
|
-
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
5
4
|
const graphql_1 = require("graphql");
|
|
5
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
6
6
|
const base_js_1 = require("./base.js");
|
|
7
7
|
class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
|
|
8
8
|
transformTypenameField(type, name) {
|