@graphql-codegen/visitor-plugin-common 6.1.2 → 6.2.0
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-resolvers-visitor.js +7 -5
- package/cjs/base-visitor.js +7 -1
- package/cjs/client-side-base-visitor.js +14 -4
- package/cjs/imports.js +7 -1
- package/esm/base-resolvers-visitor.js +7 -5
- package/esm/base-visitor.js +7 -1
- package/esm/client-side-base-visitor.js +15 -5
- package/esm/imports.js +7 -1
- package/package.json +2 -2
- package/typings/base-visitor.d.cts +8 -1
- package/typings/base-visitor.d.ts +8 -1
- package/typings/imports.d.cts +2 -1
- package/typings/imports.d.ts +2 -1
|
@@ -175,15 +175,17 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
175
175
|
const isMapped = this.config.mappers[typeName];
|
|
176
176
|
const isScalar = this.config.scalars[typeName];
|
|
177
177
|
const hasDefaultMapper = !!this.config.defaultMapper?.type;
|
|
178
|
-
|
|
179
|
-
prev[typeName] = applyWrapper(this.config.rootValueType.type);
|
|
180
|
-
return prev;
|
|
181
|
-
}
|
|
178
|
+
// Check for mappers first, even for root types, to allow overriding rootValueType
|
|
182
179
|
if (isMapped && this.config.mappers[typeName].type && !hasPlaceholder(this.config.mappers[typeName].type)) {
|
|
183
180
|
this.markMapperAsUsed(typeName);
|
|
184
181
|
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
|
|
182
|
+
return prev;
|
|
183
|
+
}
|
|
184
|
+
if (isRootType) {
|
|
185
|
+
prev[typeName] = applyWrapper(this.config.rootValueType.type);
|
|
186
|
+
return prev;
|
|
185
187
|
}
|
|
186
|
-
|
|
188
|
+
if ((0, graphql_1.isEnumType)(schemaType) && this.config.enumValues[typeName]) {
|
|
187
189
|
const isExternalFile = !!this.config.enumValues[typeName].sourceFile;
|
|
188
190
|
prev[typeName] = isExternalFile
|
|
189
191
|
? this.convertName(this.config.enumValues[typeName].typeIdentifier, {
|
package/cjs/base-visitor.js
CHANGED
|
@@ -4,11 +4,16 @@ exports.BaseVisitor = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
6
6
|
const naming_js_1 = require("./naming.js");
|
|
7
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
7
8
|
class BaseVisitor {
|
|
8
9
|
_parsedConfig;
|
|
9
10
|
_declarationBlockConfig = {};
|
|
10
11
|
scalars;
|
|
11
12
|
constructor(rawConfig, additionalConfig) {
|
|
13
|
+
const importExtension = (0, plugin_helpers_1.normalizeImportExtension)({
|
|
14
|
+
emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports,
|
|
15
|
+
importExtension: rawConfig.importExtension,
|
|
16
|
+
});
|
|
12
17
|
this._parsedConfig = {
|
|
13
18
|
convert: (0, naming_js_1.convertFactory)(rawConfig),
|
|
14
19
|
typesPrefix: rawConfig.typesPrefix || '',
|
|
@@ -20,7 +25,8 @@ class BaseVisitor {
|
|
|
20
25
|
useTypeImports: !!rawConfig.useTypeImports,
|
|
21
26
|
allowEnumStringTypes: !!rawConfig.allowEnumStringTypes,
|
|
22
27
|
inlineFragmentTypes: rawConfig.inlineFragmentTypes ?? 'inline',
|
|
23
|
-
emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports
|
|
28
|
+
emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports ?? true,
|
|
29
|
+
importExtension,
|
|
24
30
|
extractAllFieldsToTypes: rawConfig.extractAllFieldsToTypes ?? false,
|
|
25
31
|
printFieldsOnNewLines: rawConfig.printFieldsOnNewLines ?? false,
|
|
26
32
|
includeExternalFragments: rawConfig.includeExternalFragments ?? false,
|
|
@@ -296,7 +296,11 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
296
296
|
}
|
|
297
297
|
clearExtension(path) {
|
|
298
298
|
const extension = (0, path_1.extname)(path);
|
|
299
|
-
|
|
299
|
+
const importExtension = (0, plugin_helpers_1.normalizeImportExtension)({
|
|
300
|
+
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
301
|
+
importExtension: this.config.importExtension,
|
|
302
|
+
});
|
|
303
|
+
if (extension === importExtension) {
|
|
300
304
|
return path;
|
|
301
305
|
}
|
|
302
306
|
if (EXTENSIONS_TO_REMOVE.includes(extension)) {
|
|
@@ -330,9 +334,10 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
330
334
|
if (this._collectedOperations.length > 0) {
|
|
331
335
|
if (this.config.importDocumentNodeExternallyFrom === 'near-operation-file' && this._documents.length === 1) {
|
|
332
336
|
let documentPath = `./${this.clearExtension((0, path_1.basename)(this._documents[0].location))}`;
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
337
|
+
documentPath += (0, plugin_helpers_1.normalizeImportExtension)({
|
|
338
|
+
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
339
|
+
importExtension: this.config.importExtension,
|
|
340
|
+
});
|
|
336
341
|
this._imports.add(`import * as Operations from '${documentPath}';`);
|
|
337
342
|
}
|
|
338
343
|
else {
|
|
@@ -350,6 +355,10 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
350
355
|
}
|
|
351
356
|
const excludeFragments = options.excludeFragments || this.config.globalNamespace || this.config.documentMode !== DocumentMode.graphQLTag;
|
|
352
357
|
if (!excludeFragments) {
|
|
358
|
+
const importExtension = (0, plugin_helpers_1.normalizeImportExtension)({
|
|
359
|
+
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
360
|
+
importExtension: this.config.importExtension,
|
|
361
|
+
});
|
|
353
362
|
const deduplicatedImports = Object.values((0, utils_js_1.groupBy)(this.config.fragmentImports, fi => fi.importSource.path))
|
|
354
363
|
.map((fragmentImports) => ({
|
|
355
364
|
...fragmentImports[0],
|
|
@@ -358,6 +367,7 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
358
367
|
identifiers: (0, utils_js_1.unique)((0, utils_js_1.flatten)(fragmentImports.map(fi => fi.importSource.identifiers)), identifier => identifier.name),
|
|
359
368
|
},
|
|
360
369
|
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
370
|
+
importExtension,
|
|
361
371
|
}))
|
|
362
372
|
.filter(fragmentImport => fragmentImport.outputPath !== fragmentImport.importSource.path);
|
|
363
373
|
for (const fragmentImport of deduplicatedImports) {
|
package/cjs/imports.js
CHANGED
|
@@ -9,6 +9,7 @@ exports.fixLocalFilePath = fixLocalFilePath;
|
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
10
|
const path_1 = require("path");
|
|
11
11
|
const parse_filepath_1 = tslib_1.__importDefault(require("parse-filepath"));
|
|
12
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
12
13
|
function generateFragmentImportStatement(statement, kind) {
|
|
13
14
|
const { importSource: fragmentImportSource, ...rest } = statement;
|
|
14
15
|
const { identifiers, path, namespace } = fragmentImportSource;
|
|
@@ -31,7 +32,12 @@ function generateImportStatement(statement) {
|
|
|
31
32
|
const importNames = importSource.identifiers?.length
|
|
32
33
|
? `{ ${Array.from(new Set(importSource.identifiers)).join(', ')} }`
|
|
33
34
|
: '*';
|
|
34
|
-
const importExtension = importPath.startsWith('/') || importPath.startsWith('.')
|
|
35
|
+
const importExtension = importPath.startsWith('/') || importPath.startsWith('.')
|
|
36
|
+
? (0, plugin_helpers_1.normalizeImportExtension)({
|
|
37
|
+
emitLegacyCommonJSImports: statement.emitLegacyCommonJSImports,
|
|
38
|
+
importExtension: statement.importExtension,
|
|
39
|
+
})
|
|
40
|
+
: '';
|
|
35
41
|
const importAlias = importSource.namespace ? ` as ${importSource.namespace}` : '';
|
|
36
42
|
const importStatement = typesImport ? 'import type' : 'import';
|
|
37
43
|
return `${importStatement} ${importNames}${importAlias} from '${importPath}${importExtension}';${importAlias ? '\n' : ''}`;
|
|
@@ -171,15 +171,17 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
171
171
|
const isMapped = this.config.mappers[typeName];
|
|
172
172
|
const isScalar = this.config.scalars[typeName];
|
|
173
173
|
const hasDefaultMapper = !!this.config.defaultMapper?.type;
|
|
174
|
-
|
|
175
|
-
prev[typeName] = applyWrapper(this.config.rootValueType.type);
|
|
176
|
-
return prev;
|
|
177
|
-
}
|
|
174
|
+
// Check for mappers first, even for root types, to allow overriding rootValueType
|
|
178
175
|
if (isMapped && this.config.mappers[typeName].type && !hasPlaceholder(this.config.mappers[typeName].type)) {
|
|
179
176
|
this.markMapperAsUsed(typeName);
|
|
180
177
|
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
|
|
178
|
+
return prev;
|
|
179
|
+
}
|
|
180
|
+
if (isRootType) {
|
|
181
|
+
prev[typeName] = applyWrapper(this.config.rootValueType.type);
|
|
182
|
+
return prev;
|
|
181
183
|
}
|
|
182
|
-
|
|
184
|
+
if (isEnumType(schemaType) && this.config.enumValues[typeName]) {
|
|
183
185
|
const isExternalFile = !!this.config.enumValues[typeName].sourceFile;
|
|
184
186
|
prev[typeName] = isExternalFile
|
|
185
187
|
? this.convertName(this.config.enumValues[typeName].typeIdentifier, {
|
package/esm/base-visitor.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import autoBind from 'auto-bind';
|
|
2
2
|
import { convertFactory } from './naming.js';
|
|
3
|
+
import { normalizeImportExtension } from '@graphql-codegen/plugin-helpers';
|
|
3
4
|
export class BaseVisitor {
|
|
4
5
|
_parsedConfig;
|
|
5
6
|
_declarationBlockConfig = {};
|
|
6
7
|
scalars;
|
|
7
8
|
constructor(rawConfig, additionalConfig) {
|
|
9
|
+
const importExtension = normalizeImportExtension({
|
|
10
|
+
emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports,
|
|
11
|
+
importExtension: rawConfig.importExtension,
|
|
12
|
+
});
|
|
8
13
|
this._parsedConfig = {
|
|
9
14
|
convert: convertFactory(rawConfig),
|
|
10
15
|
typesPrefix: rawConfig.typesPrefix || '',
|
|
@@ -16,7 +21,8 @@ export class BaseVisitor {
|
|
|
16
21
|
useTypeImports: !!rawConfig.useTypeImports,
|
|
17
22
|
allowEnumStringTypes: !!rawConfig.allowEnumStringTypes,
|
|
18
23
|
inlineFragmentTypes: rawConfig.inlineFragmentTypes ?? 'inline',
|
|
19
|
-
emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports
|
|
24
|
+
emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports ?? true,
|
|
25
|
+
importExtension,
|
|
20
26
|
extractAllFieldsToTypes: rawConfig.extractAllFieldsToTypes ?? false,
|
|
21
27
|
printFieldsOnNewLines: rawConfig.printFieldsOnNewLines ?? false,
|
|
22
28
|
includeExternalFragments: rawConfig.includeExternalFragments ?? false,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { basename, extname } from 'path';
|
|
2
|
-
import { oldVisit } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import { normalizeImportExtension, oldVisit } from '@graphql-codegen/plugin-helpers';
|
|
3
3
|
import { optimizeDocumentNode } from '@graphql-tools/optimize';
|
|
4
4
|
import autoBind from 'auto-bind';
|
|
5
5
|
import { pascalCase } from 'change-case-all';
|
|
@@ -292,7 +292,11 @@ export class ClientSideBaseVisitor extends BaseVisitor {
|
|
|
292
292
|
}
|
|
293
293
|
clearExtension(path) {
|
|
294
294
|
const extension = extname(path);
|
|
295
|
-
|
|
295
|
+
const importExtension = normalizeImportExtension({
|
|
296
|
+
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
297
|
+
importExtension: this.config.importExtension,
|
|
298
|
+
});
|
|
299
|
+
if (extension === importExtension) {
|
|
296
300
|
return path;
|
|
297
301
|
}
|
|
298
302
|
if (EXTENSIONS_TO_REMOVE.includes(extension)) {
|
|
@@ -326,9 +330,10 @@ export class ClientSideBaseVisitor extends BaseVisitor {
|
|
|
326
330
|
if (this._collectedOperations.length > 0) {
|
|
327
331
|
if (this.config.importDocumentNodeExternallyFrom === 'near-operation-file' && this._documents.length === 1) {
|
|
328
332
|
let documentPath = `./${this.clearExtension(basename(this._documents[0].location))}`;
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
333
|
+
documentPath += normalizeImportExtension({
|
|
334
|
+
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
335
|
+
importExtension: this.config.importExtension,
|
|
336
|
+
});
|
|
332
337
|
this._imports.add(`import * as Operations from '${documentPath}';`);
|
|
333
338
|
}
|
|
334
339
|
else {
|
|
@@ -346,6 +351,10 @@ export class ClientSideBaseVisitor extends BaseVisitor {
|
|
|
346
351
|
}
|
|
347
352
|
const excludeFragments = options.excludeFragments || this.config.globalNamespace || this.config.documentMode !== DocumentMode.graphQLTag;
|
|
348
353
|
if (!excludeFragments) {
|
|
354
|
+
const importExtension = normalizeImportExtension({
|
|
355
|
+
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
356
|
+
importExtension: this.config.importExtension,
|
|
357
|
+
});
|
|
349
358
|
const deduplicatedImports = Object.values(groupBy(this.config.fragmentImports, fi => fi.importSource.path))
|
|
350
359
|
.map((fragmentImports) => ({
|
|
351
360
|
...fragmentImports[0],
|
|
@@ -354,6 +363,7 @@ export class ClientSideBaseVisitor extends BaseVisitor {
|
|
|
354
363
|
identifiers: unique(flatten(fragmentImports.map(fi => fi.importSource.identifiers)), identifier => identifier.name),
|
|
355
364
|
},
|
|
356
365
|
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
|
|
366
|
+
importExtension,
|
|
357
367
|
}))
|
|
358
368
|
.filter(fragmentImport => fragmentImport.outputPath !== fragmentImport.importSource.path);
|
|
359
369
|
for (const fragmentImport of deduplicatedImports) {
|
package/esm/imports.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { dirname, isAbsolute, join, relative, resolve } from 'path';
|
|
2
2
|
import parse from 'parse-filepath';
|
|
3
|
+
import { normalizeImportExtension } from '@graphql-codegen/plugin-helpers';
|
|
3
4
|
export function generateFragmentImportStatement(statement, kind) {
|
|
4
5
|
const { importSource: fragmentImportSource, ...rest } = statement;
|
|
5
6
|
const { identifiers, path, namespace } = fragmentImportSource;
|
|
@@ -22,7 +23,12 @@ export function generateImportStatement(statement) {
|
|
|
22
23
|
const importNames = importSource.identifiers?.length
|
|
23
24
|
? `{ ${Array.from(new Set(importSource.identifiers)).join(', ')} }`
|
|
24
25
|
: '*';
|
|
25
|
-
const importExtension = importPath.startsWith('/') || importPath.startsWith('.')
|
|
26
|
+
const importExtension = importPath.startsWith('/') || importPath.startsWith('.')
|
|
27
|
+
? normalizeImportExtension({
|
|
28
|
+
emitLegacyCommonJSImports: statement.emitLegacyCommonJSImports,
|
|
29
|
+
importExtension: statement.importExtension,
|
|
30
|
+
})
|
|
31
|
+
: '';
|
|
26
32
|
const importAlias = importSource.namespace ? ` as ${importSource.namespace}` : '';
|
|
27
33
|
const importStatement = typesImport ? 'import type' : 'import';
|
|
28
34
|
return `${importStatement} ${importNames}${importAlias} from '${importPath}${importExtension}';${importAlias ? '\n' : ''}`;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/visitor-plugin-common",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@graphql-tools/optimize": "^2.0.0",
|
|
9
|
-
"@graphql-codegen/plugin-helpers": "^6.
|
|
9
|
+
"@graphql-codegen/plugin-helpers": "^6.1.0",
|
|
10
10
|
"@graphql-tools/relay-operation-optimizer": "^7.0.0",
|
|
11
11
|
"@graphql-tools/utils": "^10.0.0",
|
|
12
12
|
"auto-bind": "~4.0.0",
|
|
@@ -21,7 +21,8 @@ export interface ParsedConfig {
|
|
|
21
21
|
useTypeImports: boolean;
|
|
22
22
|
allowEnumStringTypes: boolean;
|
|
23
23
|
inlineFragmentTypes: InlineFragmentTypeOptions;
|
|
24
|
-
emitLegacyCommonJSImports
|
|
24
|
+
emitLegacyCommonJSImports?: boolean;
|
|
25
|
+
importExtension: '' | `.${string}`;
|
|
25
26
|
printFieldsOnNewLines: boolean;
|
|
26
27
|
includeExternalFragments: boolean;
|
|
27
28
|
}
|
|
@@ -343,11 +344,17 @@ export interface RawConfig {
|
|
|
343
344
|
*/
|
|
344
345
|
inlineFragmentTypes?: InlineFragmentTypeOptions;
|
|
345
346
|
/**
|
|
347
|
+
* @deprecated Please use `importExtension` instead.
|
|
346
348
|
* @default true
|
|
347
349
|
* @description Emit legacy common js imports.
|
|
348
350
|
* Default it will be `true` this way it ensure that generated code works with [non-compliant bundlers](https://github.com/dotansimha/graphql-code-generator/issues/8065).
|
|
349
351
|
*/
|
|
350
352
|
emitLegacyCommonJSImports?: boolean;
|
|
353
|
+
/**
|
|
354
|
+
* @description Append this extension to all imports.
|
|
355
|
+
* Useful for ESM environments that require file extensions in import statements.
|
|
356
|
+
*/
|
|
357
|
+
importExtension?: '' | `.${string}`;
|
|
351
358
|
/**
|
|
352
359
|
* @default false
|
|
353
360
|
* @description Extract all field types to their own types, instead of inlining them.
|
|
@@ -21,7 +21,8 @@ export interface ParsedConfig {
|
|
|
21
21
|
useTypeImports: boolean;
|
|
22
22
|
allowEnumStringTypes: boolean;
|
|
23
23
|
inlineFragmentTypes: InlineFragmentTypeOptions;
|
|
24
|
-
emitLegacyCommonJSImports
|
|
24
|
+
emitLegacyCommonJSImports?: boolean;
|
|
25
|
+
importExtension: '' | `.${string}`;
|
|
25
26
|
printFieldsOnNewLines: boolean;
|
|
26
27
|
includeExternalFragments: boolean;
|
|
27
28
|
}
|
|
@@ -343,11 +344,17 @@ export interface RawConfig {
|
|
|
343
344
|
*/
|
|
344
345
|
inlineFragmentTypes?: InlineFragmentTypeOptions;
|
|
345
346
|
/**
|
|
347
|
+
* @deprecated Please use `importExtension` instead.
|
|
346
348
|
* @default true
|
|
347
349
|
* @description Emit legacy common js imports.
|
|
348
350
|
* Default it will be `true` this way it ensure that generated code works with [non-compliant bundlers](https://github.com/dotansimha/graphql-code-generator/issues/8065).
|
|
349
351
|
*/
|
|
350
352
|
emitLegacyCommonJSImports?: boolean;
|
|
353
|
+
/**
|
|
354
|
+
* @description Append this extension to all imports.
|
|
355
|
+
* Useful for ESM environments that require file extensions in import statements.
|
|
356
|
+
*/
|
|
357
|
+
importExtension?: '' | `.${string}`;
|
|
351
358
|
/**
|
|
352
359
|
* @default false
|
|
353
360
|
* @description Extract all field types to their own types, instead of inlining them.
|
package/typings/imports.d.cts
CHANGED
|
@@ -4,7 +4,8 @@ export type ImportDeclaration<T = string> = {
|
|
|
4
4
|
baseOutputDir: string;
|
|
5
5
|
baseDir: string;
|
|
6
6
|
typesImport: boolean;
|
|
7
|
-
emitLegacyCommonJSImports
|
|
7
|
+
emitLegacyCommonJSImports?: boolean;
|
|
8
|
+
importExtension: '' | `.${string}`;
|
|
8
9
|
};
|
|
9
10
|
export type ImportSource<T = string> = {
|
|
10
11
|
/**
|
package/typings/imports.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ export type ImportDeclaration<T = string> = {
|
|
|
4
4
|
baseOutputDir: string;
|
|
5
5
|
baseDir: string;
|
|
6
6
|
typesImport: boolean;
|
|
7
|
-
emitLegacyCommonJSImports
|
|
7
|
+
emitLegacyCommonJSImports?: boolean;
|
|
8
|
+
importExtension: '' | `.${string}`;
|
|
8
9
|
};
|
|
9
10
|
export type ImportSource<T = string> = {
|
|
10
11
|
/**
|