dotansimha / dotansimha/graphql-code-generator
ESM plugin should be exportable with `export default`
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
### Is your feature request related to a problem? Please describe.
I want to write a plugin as ESM. But graphql-code-generator cannot load ESM plugin.
- https://stackblitz.com/edit/stackblitz-starters-r6erwf?file=codegen.ts&view=editor
```json
// package.json
{
"name": "playground-graphql-codegen-typescript",
"type": "module",
"private": true,
"scripts": {
"start": "npm run gen && npm run lint",
"gen": "graphql-codegen-esm",
"lint": "tsc"
},
"devDependencies": {
"@graphql-codegen/cli": "^5.0.2",
"typescript": "^5.4.2"
},
"dependencies": {
"graphql": "^16.8.1"
}
}
```
```ts
// codegen.ts
import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: './schema.graphql',
emitLegacyCommonJSImports: false,
generates: {
'__generated__/test.txt': {
plugins: ['./my-plugin.js'],
},
},
};
export default config;
```
```ts
// my-plugin.js
/** @type {import('@graphql-codegen/plugin-helpers').CodegenPlugin} */
const plugin = {
plugin(schema, documents, config) {
return 'Hi!';
},
};
export default plugin;
```
```console
$ npm start
> start
> npm run gen && npm run lint
> gen
> graphql-codegen-esm
✔ Parse Configuration
⚠ Generate outputs
❯ Generate to __generated__/test.txt
✔ Load GraphQL schemas
✔ Load GraphQL documents
✖ Invalid Custom Plugin "./my-plugin.js"
Plugin ./my-plugin.js does not export a valid JS object with "plugin" function.
Make sure your custom plugin is written in the following form:
module.exports = {
plugin: (schema, documents, config) => {
return 'my-custom-plugin-content';
},
};
```
### Describe the solution you'd like
Allow graphql-code-generator to load ESM plugin like CJS plugin.
### Describe alternatives you've considered
_No response_
### Is your feature request related to a problem? Please describe.
_No response_
### Workaround
It seems to work if I export the plugin function with named export. However, I think it is better to use the default export.
- https://stackblitz.com/edit/stackblitz-starters-ywtkdr?file=my-plugin.js&view=editor
```js
// my-plugin.js
/** @type {import('@graphql-codegen/plugin-helpers').PluginFunction} */
const plugin = (schema, documents, config) => {
return 'Hi!';
};
export { plugin };
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.