dotansimha / dotansimha/graphql-code-generator
codegen/cli `generate` with `watch` bug
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
### Which packages are impacted by your issue?
@graphql-codegen/cli
### Describe the bug
When using `watch` in the `generate` call from `@graphql-codegen/cli` you will get an error:
```
node:internal/errors:478
ErrorCaptureStackTrace(err);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "to" argument must be of type string. Received undefined
at new NodeError (node:internal/errors:387:5)
at validateString (node:internal/validators:162:11)
at relative (node:path:1192:5)
at makeGlobalPatternSet (/home/tomk/git/ah/ah-checkout/web/node_modules/@graphql-codegen/cli/cjs/utils/patterns.js:103:33)
at createWatcher (/home/tomk/git/ah/ah-checkout/web/node_modules/@graphql-codegen/cli/cjs/utils/watcher.js:28:69)
at generate (/home/tomk/git/ah/ah-checkout/web/node_modules/@graphql-codegen/cli/cjs/generate-and-save.js:100:47) {
code: 'ERR_INVALID_ARG_TYPE'
}
error Command failed with exit code 1.
```
This is triggered from here:
https://github.com/dotansimha/graphql-code-generator/blob/2139f1db5704aa34a0cd89342736bf67ec556da3/packages/graphql-codegen-cli/src/utils/patterns.ts#L122
`filePath` is not always defined (in fact it is not possible to set it at all using the plain object input of `generate`), see here:
https://github.com/dotansimha/graphql-code-generator/blob/2139f1db5704aa34a0cd89342736bf67ec556da3/packages/graphql-codegen-cli/src/config.ts#L365
Typescript in strict mode would have complained here, since the result of the RHS is then in fact `string | undefined`.
The constructor is called only with the `config` object, so `filepath` will always be `undefined`, triggering the error.
https://github.com/dotansimha/graphql-code-generator/blob/2139f1db5704aa34a0cd89342736bf67ec556da3/packages/graphql-codegen-cli/src/config.ts#L448
Why not use `strict: true` for this project? That would have caught the bug here.
Workaround for the current version:
Call `generate` with an instance of `CodegenContext`, instead of using the plain object.
### Your Example Website or App
private repo
### Steps to Reproduce the Bug or Issue
See description
```
import { generate } from '@graphql-codegen/cli';
generate({
// ...config
watch: true
});
```
### Expected behavior
No crash
### Screenshots or Videos
_No response_
### Platform
- OS: Linux (irrelevant
- NodeJS: 16-18 (irrelevant)
- `graphql` version: 16 (irrelevant)
- `@graphql-codegen/*` version(s):
- `@graphql-codegen/cli` `4.0.1`
### Codegen Config File
```typescript
import { generate } from '@graphql-codegen/cli';
generate({
// ...config
watch: true
});
```
### Additional context
Workaround:
```typescript
import { generate, CodegenContext } from '@graphql-codegen/cli';
generate(new CodegenContext({
config: {
// ...config
watch: true
},
filepath: "i-must-be-set"
}));
```
I'm not sure what that extra watcher does, but this is actually working for me, triggering rebuild when any of the `documents` change.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.