Skip to content

Commit

Permalink
Preserve .js extension when importDocumentNodeExternallyFrom and emit…
Browse files Browse the repository at this point in the history
…LegacyCommonJSImports is false (#8895)
  • Loading branch information
benkroeger committed Feb 27, 2023
1 parent 123834e commit b343626
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-carpets-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Preserve .js extension when importDocumentNodeExternallyFrom and emitLegacyCommonJSImports is false
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ export class ClientSideBaseVisitor<
private clearExtension(path: string): string {
const extension = extname(path);

if (!this.config.emitLegacyCommonJSImports && extension === '.js') {
return path;
}

if (EXTENSIONS_TO_REMOVE.includes(extension)) {
return path.replace(/\.[^/.]+$/, '');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,51 @@ describe('getImports', () => {
});
});
});

describe('when documentMode "external", importDocumentNodeExternallyFrom is relative path', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
a: A
}
type A {
foo: String
bar: String
}
`);

describe('when emitLegacyCommonJSImports is false', () => {
it('preserves `.js` on Operations import path', () => {
const fileName = 'fooBarQuery';
const importPath = `./src/queries/${fileName}.js`;

const document = parse(
`query fooBarQuery {
a {
foo
bar
}
}
`
);

const visitor = new ClientSideBaseVisitor(
schema,
[],
{
emitLegacyCommonJSImports: false,
importDocumentNodeExternallyFrom: importPath,
documentMode: DocumentMode.external,
},
{},
[{ document, location: importPath }]
);

visitor.OperationDefinition(document.definitions[0] as OperationDefinitionNode);

const imports = visitor.getImports();
expect(imports[0]).toBe(`import * as Operations from '${importPath}';`);
});
});
});
});

0 comments on commit b343626

Please sign in to comment.