Skip to content

Commit

Permalink
Add watch polling configuration (#4823)
Browse files Browse the repository at this point in the history
* Add watch polling configuration

- Change usePolling default from `true` to `false`
- add `watchPolling` config file option to configure polling

* Update watch config structure

* Include changeset message
  • Loading branch information
mintchkin authored Sep 30, 2020
1 parent 1422ab1 commit ceb9fe0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-bikes-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': minor
---

Changes watch mode to not use polling by default and adds configurable override
9 changes: 4 additions & 5 deletions packages/graphql-codegen-cli/src/utils/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function emitWatching() {
export const createWatcher = (
initalContext: CodegenContext,
onNext: (result: Types.FileOutput[]) => Promise<Types.FileOutput[]>
) => {
): Promise<void> => {
debugLog(`[Watcher] Starting watcher...`);
let config: Types.Config & { configFilePath?: string } = initalContext.getConfig();
const files: string[] = [initalContext.filepath].filter(a => a);
Expand Down Expand Up @@ -94,9 +94,8 @@ export const createWatcher = (
followSymlinks: true,
cwd: process.cwd(),
disableGlobbing: false,
usePolling: true,
interval: 100,
binaryInterval: 300,
usePolling: config.watchConfig?.usePolling,
interval: config.watchConfig?.interval,
depth: 99,
awaitWriteFinish: true,
ignorePermissionErrors: false,
Expand Down Expand Up @@ -141,7 +140,7 @@ export const createWatcher = (
};

// the promise never resolves to keep process running
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
executeCodegen(initalContext)
.then(onNext, () => Promise.resolve())
.then(runWatcher)
Expand Down
11 changes: 11 additions & 0 deletions packages/utils/plugins-helpers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ export namespace Types {
* For more details: https://graphql-code-generator.com/docs/getting-started/development-workflow#watch-mode
*/
watch?: boolean | string | string[];
/**
* @description Allows overriding the behavior of watch to use stat polling over native file watching support.
*
* Config fields have the same defaults and sematics as the identically named ones for chokidar.
*
* For more details: https://graphql-code-generator.com/docs/getting-started/development-workflow#watch-mode
*/
watchConfig?: {
usePolling: boolean;
interval?: number;
};
/**
* @description A flag to suppress printing errors when they occur.
*/
Expand Down
11 changes: 11 additions & 0 deletions website/docs/getting-started/development-workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ If you wish, you can specify a custom list of files to watch, by adding a glob e
Use this when you are loading your schema or documents from a single code file, that depends on other files internally, because codegen can't tell that you using those files automatically.
:::

By default, watch mode uses the system's native support to listen for file change events. This can be configured in the settings file to use a stat polling method instead in unusual cases where system support is unavailable.

```yml
watch: true
# Passed directly through to chokidar's file watch configuration
watchConfig:
usePolling: true
interval: 1000
```
### Monorepo and Yarn Workspaces
If you are using a monorepo structure, with tools such as [Yarn Workspaces](https://yarnpkg.com/lang/en/docs/workspaces/) or [Lerna](https://github.com/lerna/lerna), we recommend to install the codegen in the root of your monorepo.
Expand Down

0 comments on commit ceb9fe0

Please sign in to comment.