eddeee888 / graphql-code-generator-plugins

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

[FEAT] Pretty print typeDefs to reduce merge conflicts in VCS #268

Closed kdawgwilk closed 4 months ago

kdawgwilk commented 6 months ago

Is your feature request related to a problem? Please describe. Because the typeDefs.generated.ts file is a single line it always ends up causing merge conflicts when checked in.

Describe the solution you'd like I would like the JSON object to be pretty printed on multiple lines so that its less likely to cause merge conflicts

Describe alternatives you've considered Not checking in generated code. There are tradeoffs but I think those trade offs are another discussion and this would be useful either way.

Additional context N/A

eddeee888 commented 6 months ago

Hi @kdawgwilk 👋

I think this is a perfect use case for Codegen's life cycle hooks

A setting I normally use is to run prettier on generated files using afterAllFileWrite: ["prettier --write"].

Here's an example config:

import type { CodegenConfig } from "@graphql-codegen/cli";
import { defineConfig } from "@eddeee888/gcg-typescript-resolver-files";

const config: CodegenConfig = {
  schema: "**/schema.graphql",
  hooks: {
    afterAllFileWrite: ["prettier --write"], // This can be added to run prettier on all generated files
  },
  generates: {
    "src/schema": defineConfig(),
  },
};
export default config;

Here's an example prettified output: https://github.com/eddeee888/graphql-code-generator-plugins/blob/master/packages/typescript-resolver-files-e2e/src/test-modules/modules/typeDefs.generated.ts

Please let me know whether this solves your issue or not 🙂

kdawgwilk commented 6 months ago

It worked when i added it at the top level like you suggested but i had originally tried with it set only on one of my outputs e.g.

const config: CodegenConfig = {
  schema: 'functions/**/schema.graphql',
  ignoreNoDocuments: true, // for better experience with the watcher
  generates: {
    'functions/src/schema': {
      ...defineConfig({
        typesPluginsConfig: {
          contextType: './context.js#Context',
          enumsAsTypes: false,
        },
        emitLegacyCommonJSImports: false,
      }),
      hooks: {
        afterAllFileWrite: ['prettier --write'],
      },
    },
    'web/src/lib/gql/': {
      preset: 'client',
      documents: 'web/src/**/*.tsx',
    },
  },
  config: {
    scalars: {
      DateTime: 'string',
      Date: 'string',
      URL: 'string',
    },
  },
};

So there may still be a bug here but i am at least no longer blocked by this issue

kdawgwilk commented 6 months ago

Also i am noticing the loc.start and loc.end also get updated for everything below where i change the schema which unfortunately causes many conflicts still 😞

eddeee888 commented 6 months ago

I see. Could you help me understand if this is happening because multiple devs are making schema changes at the same time, causing conflicts?

If you are finding it getting in the way, maybe you can add it to .gitignore?

eddeee888 commented 4 months ago

FYI in v0.10.1, there's an update to remove loc.

I'll close this for now but if there's still issues, let me know 🙂