Skip to content

Commit

Permalink
[cli] Add watchPattern to generates config (#9151)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddeee888 committed Mar 19, 2023
1 parent 42199b7 commit b7dacb2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .changeset/brave-papayas-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
'@graphql-codegen/plugin-helpers': minor
'@graphql-codegen/cli': minor
---

Add `watchPattern` config option for `generates` sections.

By default, `watch` mode automatically watches all GraphQL schema and document files. This means when a change is detected, Codegen CLI is run.

A user may want to run Codegen CLI when non-schema and non-document files are changed. Each `generates` section now has a `watchPattern` option to allow more file patterns to be added to the list of patterns to watch.

In the example below, mappers are exported from `schema.mappers.ts` files. We want to re-run Codegen if the content of `*.mappers.ts` files change because they change the generated types file. To solve this, we can add mapper file patterns to watch using the glob pattern used for schema and document files.

```ts
// codegen.ts
const config: CodegenConfig = {
schema: 'src/schema/**/*.graphql',
generates: {
'src/schema/types.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
mappers: {
User: './user/schema.mappers#UserMapper',
Book: './book/schema.mappers#BookMapper',
},
}
watchPattern: 'src/schema/**/*.mappers.ts', // Watches mapper files in `watch` mode. Use an array for multiple patterns e.g. `['src/*.pattern1.ts','src/*.pattern2.ts']`
},
},
};
```

Then, run Codegen CLI in `watch` mode:

```shell
yarn graphql-codegen --watch
```

Now, updating `*.mappers.ts` files re-runs Codegen! 🎉

Note: `watchPattern` is only used in `watch` mode i.e. running CLI with `--watch` flag.
1 change: 1 addition & 0 deletions packages/graphql-codegen-cli/src/utils/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const createWatcher = (
.forEach(conf => {
schemas.push(...normalizeInstanceOrArray<Types.Schema>(conf.schema));
documents.push(...normalizeInstanceOrArray<Types.OperationDocument>(conf.documents));
files.push(...normalizeInstanceOrArray(conf.watchPattern));
});

if (documents) {
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/plugins-helpers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ export namespace Types {
* @description DocumentTransform changes documents before executing plugins.
*/
documentTransforms?: OutputDocumentTransform[];
/**
* @description: Additional file pattern to watch when using watch mode
*/
watchPattern?: string | string[];
}

/* Output Builder Preset */
Expand Down

0 comments on commit b7dacb2

Please sign in to comment.