Skip to content

Allow sorting in enums to be disabled #6935

Closed
@andbjer

Description

@andbjer

Describe 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

  1. 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
}
  1. My GraphQL operations:
query user {
    user(id: 1) {
        id
        username
        email
        planStartDay
    }
}
  1. My codegen.yml config 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions