Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add watch polling configuration #4823

Merged
merged 3 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 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 @@ -88,15 +88,19 @@ export const createWatcher = (
}
});

let pollingInterval = 100;
if (typeof config.watchPolling === 'object') {
dotansimha marked this conversation as resolved.
Show resolved Hide resolved
pollingInterval = config.watchPolling.interval;
}

watcher = chokidar.watch(files, {
persistent: true,
ignoreInitial: true,
followSymlinks: true,
cwd: process.cwd(),
disableGlobbing: false,
usePolling: true,
interval: 100,
binaryInterval: 300,
usePolling: !!config.watchPolling,
interval: pollingInterval,
depth: 99,
awaitWriteFinish: true,
ignorePermissionErrors: false,
Expand Down Expand Up @@ -141,7 +145,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
12 changes: 12 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,18 @@ 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.
*
* You can specify a boolean to toggle polling on or off (default: false) or an interval option to configure polling.
*
* For more details: https://graphql-code-generator.com/docs/getting-started/development-workflow#watch-mode
*/
watchPolling?:
mintchkin marked this conversation as resolved.
Show resolved Hide resolved
| {
interval: number;
}
| boolean;
/**
* @description A flag to suppress printing errors when they occur.
*/
Expand Down
10 changes: 10 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,16 @@ 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
# Supports a boolean toggle, or additional options to configure polling
watchPolling:
interval: 1000 # polling interval in ms (default: 100 when polling is enabled)
```


### 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