Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed emitLegacyCommonJSImports in getImports adding JS when truthy r… (
- Loading branch information
Showing
3 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-codegen/visitor-plugin-common': patch | ||
--- | ||
|
||
Fix issue where visitor-plugin-common emitted ESM imports for Operations when emitLegacyCommonJSImports is true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/plugins/other/visitor-plugin-common/tests/client-side-base-visitor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { buildSchema, OperationDefinitionNode, parse } from 'graphql'; | ||
import { ClientSideBaseVisitor, DocumentMode } from '../src/client-side-base-visitor.js'; | ||
|
||
describe('getImports', () => { | ||
describe('when documentMode "external", importDocumentNodeExternallyFrom is "near-operation-file"', () => { | ||
const schema = buildSchema(/* GraphQL */ ` | ||
type Query { | ||
a: A | ||
} | ||
type A { | ||
foo: String | ||
bar: String | ||
} | ||
`); | ||
|
||
describe('when emitLegacyCommonJSImports is true', () => { | ||
it('does not append `.js` to Operations import path', () => { | ||
const fileName = 'fooBarQuery'; | ||
const importPath = `src/queries/${fileName}`; | ||
|
||
const document = parse( | ||
`query fooBarQuery { | ||
a { | ||
foo | ||
bar | ||
} | ||
} | ||
` | ||
); | ||
|
||
const visitor = new ClientSideBaseVisitor( | ||
schema, | ||
[], | ||
{ | ||
emitLegacyCommonJSImports: true, | ||
importDocumentNodeExternallyFrom: 'near-operation-file', | ||
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 './${fileName}';`); | ||
}); | ||
}); | ||
|
||
describe('when emitLegacyCommonJSImports is false', () => { | ||
it('appends `.js` to Operations import path', () => { | ||
const fileName = 'fooBarQuery'; | ||
const importPath = `src/queries/${fileName}`; | ||
|
||
const document = parse( | ||
`query fooBarQuery { | ||
a { | ||
foo | ||
bar | ||
} | ||
} | ||
` | ||
); | ||
|
||
const visitor = new ClientSideBaseVisitor( | ||
schema, | ||
[], | ||
{ | ||
emitLegacyCommonJSImports: false, | ||
importDocumentNodeExternallyFrom: 'near-operation-file', | ||
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 './${fileName}.js';`); | ||
}); | ||
}); | ||
}); | ||
}); |