dotansimha / dotansimha/graphql-code-generator

Inconsistent enum usages when enum key is not exact the custom value

Open
#7,450 1 comment 0 reactions 0 assignees View on GitHub
core stage/0-issue-prerequisites
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**Describe the bug**

We are building our graphql schemes with typescript for a nodejs backend.
A strange inconsistency occurs while generating enums where a enum key does not match its "custom" value.

If key and value property of an enum are the same the enum is correctly generated in "CamelCase" and used.

If the custom value differs from the key, the references to that enum in the generated types is "camelCase". With a leading lowercase character.

**To Reproduce**
Steps to reproduce the behavior:

1. My GraphQL schema:

```TS
export const stateType = new GraphQLEnumType({
description: 'Available and valid states',
name: 'stateType',
values: {
Approved: {
value: 'approved'
},
Canceled: {
value: 'canceled'
},
ChangesRequested: {
value: 'changesRequested'
},
Created: {
value: 'created'
},
Declined: {
value: 'declined'
},
Draft: {
value: 'draft'
},
Finished: {
value: 'finished'
},
Sent: {
value: 'sent'
}
}
})
```

2. My GraphQL operations:

Just add this enum to a field type and add it to your schema

3. My `codegen.yml` config file:

```yml
schema:
- "./scripts/code-generator-schema.ts"
overwrite: true
generates:
./src/frontend/graphql/generated-types.ts:
documents:
- "./src/frontend/**/*.graphql"
plugins:
- typescript
- typescript-operations
- typescript-apollo-angular
config:
avoidOptionals:
inputValue: false
object: false
field: true
exportFragmentSpreadSubTypes: true
arrayInputCoercion: false
```

Results in:
```TS
export enum StateType {
Approved = 'approved',
Canceled = 'canceled',
ChangesRequested = 'changesRequested',
Created = 'created',
Declined = 'declined',
Draft = 'draft',
Finished = 'finished',
Sent = 'sent'
}
```

But it is used in other types
```TS
{
stateType: stateType
}
```

If enum key + custom value are the same it is correctly referenced in the generation output:
```TS
{
stateType: StateType
}
```

**Expected behavior**

The enum should be correctly reference the generated enum types. There should not be any strange side effects on the enum usages when defining custom eunm values.

Additional there should not be a transformation of the keys of an enum. Every key is transformed that the first character is in uppercase - even if we define the key as lowercase.

**Environment:**

- OS: Ubuntu 20.04.3 LTS
- `@graphql-codegen/cli`: 2.4.0
- `@graphql-codegen/typescript`: 2.4.2
- `@graphql-codegen/typescript-apollo-angular`: 3.3.2
- `@graphql-codegen/typescript-operations`: 2.2.3
- NodeJS: 16.10.0

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.