Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESM plugin should be exportable with export default #9896

Open
mizdra opened this issue Mar 8, 2024 · 1 comment
Open

ESM plugin should be exportable with export default #9896

mizdra opened this issue Mar 8, 2024 · 1 comment

Comments

@mizdra
Copy link
Contributor

mizdra commented Mar 8, 2024

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.

// 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"
  }
}
// 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;
// my-plugin.js
/** @type {import('@graphql-codegen/plugin-helpers').CodegenPlugin} */
const plugin = {
  plugin(schema, documents, config) {
    return 'Hi!';
  },
};

export default plugin;
$ 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.

// my-plugin.js
/** @type {import('@graphql-codegen/plugin-helpers').PluginFunction} */
const plugin = (schema, documents, config) => {
  return 'Hi!';
};

export { plugin };
mizdra added a commit to mizdra/graphql-codegen-typescript-fabbrica that referenced this issue Mar 10, 2024
- Keep `@mizdra/graphql-codegen-typescript-fabbrica` as CJS because of dotansimha/graphql-code-generator#9896
- Convert `@mizdra/graphql-codegen-typescript-fabbrica` to ESM
@mizdra mizdra changed the title Support ESM plugin ESM plugin should be default exportable Mar 10, 2024
@mizdra mizdra changed the title ESM plugin should be default exportable ESM plugin should be exportable with export default Mar 10, 2024
@mizdra
Copy link
Contributor Author

mizdra commented Mar 10, 2024

Am I understanding this incorrectly? Is it correct behavior that export default is not available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant