Allow sorting in enums to be disabled
See original GitHub issueDescribe the bug
My schema contains an enum for DayOfWeek:
enum DayOfWeek {
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
}
When GraphQL Code Generator creates a typescript type for this enum, it will sort the values alphabetically like below:
export enum DayOfWeek {
Friday = 'FRIDAY',
Monday = 'MONDAY',
Saturday = 'SATURDAY',
Sunday = 'SUNDAY',
Thursday = 'THURSDAY',
Tuesday = 'TUESDAY',
Wednesday = 'WEDNESDAY'
}
This is an issue if you would like to retrieve the index value of the enum:
const day = Object.values(DayOfWeek).indexOf(obj.dayOfWeek);
If the object’s day of week is Sunday I would expect day to be 0, instead it is 3.
I’ve tried to look through all of the options for the typescript-plugin, but I have not found an option for disabling sorting on enums.
To Reproduce Steps to reproduce the behavior:
https://codesandbox.io/s/graphql-code-generator-enum-issue-7788u
- My GraphQL schema:
type Query {
user(id: ID!): User!
}
type User {
id: ID!
username: String!
email: String!
planStartDay: DayOfWeek
}
enum DayOfWeek {
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
}
- My GraphQL operations:
query user {
user(id: 1) {
id
username
email
planStartDay
}
}
- My
codegen.ymlconfig file:
schema: schema.graphql
documents: document.graphql
generates:
types.ts:
plugins:
- typescript
- typescript-operations
Expected behavior I would expect the enum values to keep the same index position as in the schema, or at least an option to disable the alphabetical sorting.
Environment:
- OS: macOS 11.6
- “@graphql-codegen/add”: “3.1.0”
- “@graphql-codegen/cli”: “2.2.1”
- “@graphql-codegen/typescript”: “2.2.4”
- “@graphql-codegen/typescript-operations”: “2.1.8”,
- NodeJS: v17.0.1
Additional context
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:12 (2 by maintainers)

Top Related StackOverflow Question
@ha-akelius, try specifying on the sort:false on the root level, like so:
That worked for me, whereas specifying at the output level did not.
Hello, we will rolleback to older version to not have the new sort method. We’ve tried to disabled the sort and it work however, changing one query (ex: add one field to a query) is modifying the all structure of the file.
The Query A is now at ligne 2000 instead of line 100, even if it was not modified. It becomes very difficult to work like that in a team because it produce merge conflict every time the graphql.tsx is generated.
I think we need the option to disable sort on enum. I understand that browsers does not return values in the same order, but we did not had the problem.
So either we have sortEnum option or the logic in our project has to be changed. For the moment will rolleback on the version of the lib.
PS : I know this is an open source project. If you indicates me where the logic is implemented we could considerate to developp this option.