dotansimha / dotansimha/graphql-code-generator
Allow passing JavaScript code directly to `plugins` in `codegen.ts`
- 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.
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:
```typescript
const config: CodegenConfig = {
schema: [
'./**/*.graphql',
],
generates: {
'./example.ts': {
plugins: [
{
'./example-plugin.ts': {
name: 'John',
},
// OR
'@myworkspace/example-plugin': {
name: 'John',
},
},
],
},
},
}
```
Desired:
```typescript
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.
```typescript
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_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.