dotansimha / dotansimha/graphql-code-generator
Watch generates different files from no watch (graphql-modules)
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
**Describe the bug**
Thank you for keeping this important project going!
So for some reason, if `--watch 'some/file/glob'` is specified for our monorepo, then the server schema generated from the `graphql-modules` preset can potentially generate using more files than it should be using.
In our monorepo, we have an older `schemas` folder with many `.graphql` files defining our schema, and a newer implementation using [graphql-modules](https://www.graphql-code-generator.com/docs/presets/graphql-modules). We generate two schemas, one _only_ from the graphql modules files, and one combining the older schema and the modules schema.
If `--watch` specifies all possible files (including the older schema), then the merging happens even in the output file that shouldn't contain the full schema.
My guess is that graphql-codegen or the modules preset is adding all possible files into an internal list, and reusing that list during generation instead of re-calculating for each `generates` key in the codegen file. So `--watch` basically augments `schema`, instead of _only_ watching the specified files for changes and keeping `schema` the same.
For a workaround I'm going to try watching the glob manually with chokidar or watchman, and re-running `graphql-codegen` from scratch each time.
**To Reproduce**
Don't have time for a full reproduction project right now, sorry :(
(Abbreviated) `codegen.yml`:
```yaml
schema:
- ./modules/**/*.module.ts:
noRequire: true
config:
onlyResolveTypeForInterfaces: true
useTypeImports: true
maybeValue: 'T | null | undefined'
omitOperationSuffix: true
preResolveTypes: false
skipTypeNameForRoot: true
namingConvention:
enumValues: keep
scalars:
# many scalar definitions
mappers:
# many mapper definitions
generates:
# Generate server types and resolver types - modules
../src/modules/:
preset: graphql-modules
presetConfig:
baseTypesPath: ../generated/server.ts
filename: generated/module-types.ts
encapsulateModuleTypes: none
plugins:
- typescript
- typescript-resolvers
# Generate server and resolver types - modules + legacy
../src/generated/server-legacy.ts:
schema: ./schema/**/*.graphql # added to "schema" at root
plugins:
- typescript
- typescript-resolvers
```
**Expected behavior**
`--watch` should watch the specified files, but the actual files used in each `generates` should not change.
**Environment:**
- OS: Mac OS
```json
{
"@graphql-codegen/cli": "^2.2.0",
"@graphql-codegen/graphql-modules-preset": "^2.1.4",
"@graphql-codegen/schema-ast": "^2.2.0",
"@graphql-codegen/typed-document-node": "^2.1.6",
"@graphql-codegen/typescript": "^2.2.2",
"@graphql-codegen/typescript-operations": "^2.1.8",
"@graphql-codegen/typescript-resolvers": "^2.3.0"
}
```
- NodeJS: 14.15.1
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.