graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

feat(preset/client): allow use of enumsAsConst config option

Open Stephen2 opened this issue 2 years ago • 3 comments

🚨 IMPORTANT: Please do not create a Pull Request without creating an issue first.

Any change needs to be discussed before proceeding. Failure to do so may result in the rejection of the pull request.

Description

Allows 'enumsAsConst' option to be used with client preset. Extremely simple and safe. Based on requests here: https://github.com/dotansimha/graphql-code-generator/issues/8562

Related # (issue)

https://github.com/dotansimha/graphql-code-generator/issues/8562

Type of change

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

How Has This Been Tested?

Tested on my local project, with a complex GraphQL schema, generating 2 types (masked & unmasked):

graphql.config.ts excerpt:

            generates: {
                'src/gql/': {
                    preset: 'client',
                    plugins: [],
                    config: {
                        // To generate string unions, instead of Enum, so backend <> frontend doesn't need conversion
                        enumsAsConst: true
                    },
                    presetConfig: {
                        fragmentMasking: {
                            unmaskFunctionName: 'getFragmentData'
                        }
                    }
                },

                // Backend server just returns the full shape of objects, and always has to return __typename or Apollo gets lost
                'src/gql/__backend__/': {
                    preset: 'client',
                    plugins: ['typescript-msw'],
                    config: {
                        nonOptionalTypename: true,

                        namingConvention: {
                            typeNames: './backend-gql-naming-fn'
                        },

                        // To generate string unions, instead of Enum, so backend <> frontend doesn't need conversion
                        enumsAsConst: true
                    },
                    presetConfig: {
                        fragmentMasking: false
                    }
                }
            }

Test Environment:

  • OS: MacOS 13.5 (22G74)
  • @graphql-codegen/...:
        "@graphql-codegen/cli": "4.0.1",
        "@graphql-codegen/client-preset": "4.0.1",
        "@graphql-codegen/typescript-msw": "^1.1.6",
  • NodeJS:
❯ node -v && npm -v
v16.20.1
8.19.4

Checklist:

  • [x] I have followed the CONTRIBUTING doc and the style guidelines of this project
  • [x] I have performed a self-review of my own code
  • [ ] I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [x] New and existing unit tests pass locally with my changes
  • [ ] Any dependent changes have been merged and published in downstream modules

Stephen2 avatar Jul 28 '23 21:07 Stephen2

🦋 Changeset detected

Latest commit: 70eb250278551cdae060cad75bf800f01a6adf80

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

This PR includes changesets to release 1 package
Name Type
@graphql-codegen/client-preset 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

changeset-bot[bot] avatar Jul 28 '23 21:07 changeset-bot[bot]

Force pushed, to keep up to date. Any maintainer willing to engage with this PR? 🙏

Stephen2 avatar Dec 06 '23 17:12 Stephen2

@dylan-oleria Can this be merged please? We have functionality dependant on enumsAsConst, so can't use client preset currently.

mvestergaard avatar May 28 '24 14:05 mvestergaard