Skip to content

Commit a981985

Browse files
authored
Fixed emitLegacyCommonJSImports in getImports adding JS when truthy r… (#8816)
1 parent 884d25c commit a981985

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

.changeset/unlucky-suits-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
---
4+
5+
Fix issue where visitor-plugin-common emitted ESM imports for Operations when emitLegacyCommonJSImports is true

packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ export class ClientSideBaseVisitor<
532532
if (this._collectedOperations.length > 0) {
533533
if (this.config.importDocumentNodeExternallyFrom === 'near-operation-file' && this._documents.length === 1) {
534534
let documentPath = `./${this.clearExtension(basename(this._documents[0].location))}`;
535-
if (this.config.emitLegacyCommonJSImports) {
535+
if (!this.config.emitLegacyCommonJSImports) {
536536
documentPath += '.js';
537537
}
538538

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { buildSchema, OperationDefinitionNode, parse } from 'graphql';
2+
import { ClientSideBaseVisitor, DocumentMode } from '../src/client-side-base-visitor.js';
3+
4+
describe('getImports', () => {
5+
describe('when documentMode "external", importDocumentNodeExternallyFrom is "near-operation-file"', () => {
6+
const schema = buildSchema(/* GraphQL */ `
7+
type Query {
8+
a: A
9+
}
10+
11+
type A {
12+
foo: String
13+
bar: String
14+
}
15+
`);
16+
17+
describe('when emitLegacyCommonJSImports is true', () => {
18+
it('does not append `.js` to Operations import path', () => {
19+
const fileName = 'fooBarQuery';
20+
const importPath = `src/queries/${fileName}`;
21+
22+
const document = parse(
23+
`query fooBarQuery {
24+
a {
25+
foo
26+
bar
27+
}
28+
}
29+
`
30+
);
31+
32+
const visitor = new ClientSideBaseVisitor(
33+
schema,
34+
[],
35+
{
36+
emitLegacyCommonJSImports: true,
37+
importDocumentNodeExternallyFrom: 'near-operation-file',
38+
documentMode: DocumentMode.external,
39+
},
40+
{},
41+
[{ document, location: importPath }]
42+
);
43+
44+
visitor.OperationDefinition(document.definitions[0] as OperationDefinitionNode);
45+
46+
const imports = visitor.getImports();
47+
expect(imports[0]).toBe(`import * as Operations from './${fileName}';`);
48+
});
49+
});
50+
51+
describe('when emitLegacyCommonJSImports is false', () => {
52+
it('appends `.js` to Operations import path', () => {
53+
const fileName = 'fooBarQuery';
54+
const importPath = `src/queries/${fileName}`;
55+
56+
const document = parse(
57+
`query fooBarQuery {
58+
a {
59+
foo
60+
bar
61+
}
62+
}
63+
`
64+
);
65+
66+
const visitor = new ClientSideBaseVisitor(
67+
schema,
68+
[],
69+
{
70+
emitLegacyCommonJSImports: false,
71+
importDocumentNodeExternallyFrom: 'near-operation-file',
72+
documentMode: DocumentMode.external,
73+
},
74+
{},
75+
[{ document, location: importPath }]
76+
);
77+
78+
visitor.OperationDefinition(document.definitions[0] as OperationDefinitionNode);
79+
80+
const imports = visitor.getImports();
81+
expect(imports[0]).toBe(`import * as Operations from './${fileName}.js';`);
82+
});
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)