eddeee888 / graphql-code-generator-plugins

List of GraphQL Code Generator plugins that complements the official plugins.
MIT License
44 stars 10 forks source link

[FEAT] Add option to delete stale files #232

Open lensbart opened 4 months ago

lensbart commented 4 months ago

Is your feature request related to a problem? Please describe. When you’re making a larger change, sometimes many files can be renamed or deleted and created at once. In such a situation, it could be helpful if this plugin would delete all files it doesn’t recognise in the output folders (that is, all files excluding .graphql files, mapper files, and files that would be generated if missing).

Describe the solution you'd like This should probably not be the default behaviour, as it is a destructive action that will require the developer to keep unrelated files (for e.g. helper functions) out of the resolvers folders. So I’m thinking of a configuration option

Describe alternatives you've considered

Additional context Not an urgent request, just a friendly suggestion 😊 Thanks

eddeee888 commented 4 months ago

Thank you for the suggestion! I think there are a few cases we'd have to consider so we don't do destructive actions too aggressively:

lensbart commented 4 months ago

Good questions. Not sure if my suggestion is worth the potential hassle (especially considering the minor benefit).

If the user just renames types, would they expect the codegen to (a) delete the file or (b) marks the file as no longer in the Graph but also create a new file so user can manually migrate

I’d expect an incremental migration path to first have both resolvers in the schema, and then only the newer. Once a field gets removed from the schema, I think it can realistically be considered as having been migrated. There is always the git history to retrieve old logic.

If the user wants to extract logic from resolver files or add extra files into resolvers folder, do we need different config to avoid deleting these? e.g.

Good question. The more I think of it, the more I’m hesitant about the above suggestion. Even if opt-in via a configuration option, this will introduce a convention in the codebase where files might get deleted unexpectedly. One approach could be to prepend a comment to the generated files, and only auto-delete files that start with that comment.

eddeee888 commented 3 months ago

All great thoughts here @lensbart! It seems like the emerging theme is "somehow detect and track a file generated by this preset". Another option is we track the resolver files with something like .resolvers.ts e.g. User.resolvers.ts, Query/deleteUser.resolvers.ts

This way, it has a few benefits:

This is also a breaking change to existing codebases so we need a migration path e.g.

Also, this is not the only option, just another one to add to your great suggestions above 🙂 So I'm keen to have more ideas before picking one