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

Allow passing JavaScript code directly to plugins in codegen.ts #10228

Open
kensukesaito opened this issue Jan 4, 2025 · 0 comments
Open

Allow passing JavaScript code directly to plugins in codegen.ts #10228

kensukesaito opened this issue Jan 4, 2025 · 0 comments

Comments

@kensukesaito
Copy link

Is your feature request related to a problem? Please describe.

No.

Describe the solution you'd like

Currently, the plugins field only accepts npm packages, workspace packages, or external files via relative paths. However, I’d like to request support for passing JavaScript code directly, as shown below:

Current:

const config: CodegenConfig = {
  schema: [
    './**/*.graphql',
  ],
  generates: {
    './example.ts': {
      plugins: [
        {
          './example-plugin.ts': {
            name: 'John',
          },
          // OR
          '@myworkspace/example-plugin': {
            name: 'John',
          },
        },
      ],
    },
  },
}

Desired:

const examplePlugin = (config) => {
  plugin: async (schema, documents, config, info) => {
    return `Hello, ${config.name}`
  },
  validate: (schema, documents, config, outputFile, allPlugins, pluginContext) => {
    throw new Error('Something went wrong...')
  },
}

const config: CodegenConfig = {
  schema: [
    './**/*.graphql',
  ],
  generates: {
    './example.ts': {
      plugins: [
        examplePlugin({
          name: 'John',
        }),
      ],
    },
  },
}

For reference, this is possible in esbuild.config.ts, allowing small plugins to be written directly within the configuration file, which is very convenient.

import { type Plugin, build } from 'esbuild'

const examplePlugin = (config): Plugin => {
  return {
    name: 'examplePlugin',
    setup: (build) => {
      console.log(`Hello, ${config.name}`)
    }
  }
}

build({
  plugins: [
    examplePlugin({
      name: 'John',
    }),
  ],
})

Describe alternatives you've considered

No response

Any additional important details?

No response

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