dotansimha / dotansimha/graphql-code-generator

Option to strip common prefix from enum variants

Open
#2,549 0 comments 2 reactions 0 assignees View on GitHub
core kind/enhancement plugins
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**Is your feature request related to a problem? Please describe.**

Due to graphql not support namespaces, and enum values beving in the global namespace, we have implemented our own convention to avoid naming collisions.
Our enums look like this:

```graphql
enum ExternalLoginProvider {
EXTERNAL_LOGIN_PROVIDER_FACEBOOK
EXTERNAL_LOGIN_PROVIDER_GOOGLE
}

enum RegistrationSource {
REGISTRATION_SOURCE_EXISTING_USER
REGISTRATION_SOURCE_NEW_USER_VIA_CREDENTIALS
REGISTRATION_SOURCE_NEW_USER_VIA_FACEBOOK
REGISTRATION_SOURCE_NEW_USER_VIA_GOOGLE
REGISTRATION_SOURCE_NEW_USER_VIA_INVITATION
}
```

With the `namingConvention: change-case#pascalCase` setting, this gets turned into:
```ts
export enum ExternalLoginProvider {
ExternalLoginProviderFacebook = 'EXTERNAL_LOGIN_PROVIDER_FACEBOOK',
ExternalLoginProviderGoogle = 'EXTERNAL_LOGIN_PROVIDER_GOOGLE'
}
export enum RegistrationSource {
RegistrationSourceExistingUser = 'REGISTRATION_SOURCE_EXISTING_USER',
RegistrationSourceNewUserViaCredentials = 'REGISTRATION_SOURCE_NEW_USER_VIA_CREDENTIALS',
RegistrationSourceNewUserViaFacebook = 'REGISTRATION_SOURCE_NEW_USER_VIA_FACEBOOK',
RegistrationSourceNewUserViaGoogle = 'REGISTRATION_SOURCE_NEW_USER_VIA_GOOGLE',
RegistrationSourceNewUserViaInvitation = 'REGISTRATION_SOURCE_NEW_USER_VIA_INVITATION'
}
```

**Describe the solution you'd like**

It would be nice if the redundant prefix could be dropped, so the resulting enum definition becomes:
```ts
export enum ExternalLoginProvider {
Facebook = 'EXTERNAL_LOGIN_PROVIDER_FACEBOOK',
Google = 'EXTERNAL_LOGIN_PROVIDER_GOOGLE'
}
export enum RegistrationSource {
ExistingUser = 'REGISTRATION_SOURCE_EXISTING_USER',
NewUserViaCredentials = 'REGISTRATION_SOURCE_NEW_USER_VIA_CREDENTIALS',
NewUserViaFacebook = 'REGISTRATION_SOURCE_NEW_USER_VIA_FACEBOOK',
NewUserViaGoogle = 'REGISTRATION_SOURCE_NEW_USER_VIA_GOOGLE',
NewUserViaInvitation = 'REGISTRATION_SOURCE_NEW_USER_VIA_INVITATION'
}
```

That would actually match our convention of normal enum naming (apart from the string value itself…)

**Describe alternatives you've considered**

I could just provide my own enum definitions via the `enumValues` config. But actually having this done completely via codegen would be awesome :-)

**Additional context**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.