Skip to content

Add overwrite.updateExistingFiles and overwrite.removeStaleFiles - #10921

Merged
eddeee888 merged 4 commits into
masterfrom
cli-add-overwrite-options
Aug 22, 2026
Merged

Add overwrite.updateExistingFiles and overwrite.removeStaleFiles#10921
eddeee888 merged 4 commits into
masterfrom
cli-add-overwrite-options

Conversation

@eddeee888

@eddeee888 eddeee888 commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

Description

Whilst working on Server Preset next version, I noticed that resolver files may get removed in very specific conditions:

  • Run in watch mode
  • Server Preset returns a new file to Codegen to create (as part of buildGeneratesSection). At this point, Codegen tracks this file.
  • Watch mode is triggered
  • For any reason, Server Preset stops returning the said file as part of buildGeneratesSection. This is a deliberate move because sending files triggers filesystem write, which would be very slow for big codebases.
  • The file is seen as stale by Codegen
  • Codegen's overwrite: true config is used to remove stale files, so the resolver file is removed.

Problem: overwrite: true is also used to update exiting files. Server Preset needs to be able to update existing files to add resolvers/comments

For this reason, I'm extending the existing overwrite config to be more granular:

  • overwrite.updateExistingFiles: Whether updating existing files is allowed
  • overwrite.removeStaleFiles: Whether Codegen should remove stale files (files that are tracked in one run, but not the next) during watch mode

Type of change

  • New feature (non-breaking change which adds functionality)

TODO

  • Add watcher test
  • Update docs

@github-actions

Copy link
Copy Markdown
Contributor

💻 Website Preview

The latest changes are available as preview in: https://pr-10921.graphql-code-generator.pages.dev

@changeset-bot

changeset-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 88cc642

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@graphql-codegen/plugin-helpers Minor
@graphql-codegen/cli Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-codegen/cli 7.3.0-alpha-20260819142751-88cc6428fb4182dc8f12a1da685a0fc5280477d9 npm ↗︎ unpkg ↗︎
@graphql-codegen/plugin-helpers 7.2.0-alpha-20260819142751-88cc6428fb4182dc8f12a1da685a0fc5280477d9 npm ↗︎ unpkg ↗︎

@eddeee888
eddeee888 marked this pull request as ready for review August 19, 2026 14:26
Comment on lines +6 to +8
import * as watcherModule from '../src/utils/watcher.js';

const createWatcherSpy = vi.spyOn(watcherModule, 'createWatcher');

@eddeee888 eddeee888 Aug 19, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New testing approach uses generate which is the end-to-end test behaviour.
However, because generate calls createWatcher, we cannot get access to the stopWatching to stop the test.

Adding a spy is a trick that allows us to get the stopWatching function from the return type.

const runningWatcher = generate(context);
await waitForNextEvent();

const { stopWatching } = createWatcherSpy.mock.results.at(-1)!.value as ReturnType<

@eddeee888 eddeee888 Aug 19, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, createWatcherSpy.mock.results.at(-1)!.value means getting the last result returned by createWatch

@eddeee888
eddeee888 merged commit 58cdb31 into master Aug 22, 2026
11 of 13 checks passed
@eddeee888
eddeee888 deleted the cli-add-overwrite-options branch August 22, 2026 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant