question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow sorting in enums to be disabled

See original GitHub issue

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:

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
gtschiedercommented, Apr 6, 2022

@ha-akelius, try specifying on the sort:false on the root level, like so:

schema: "schema.graphql"
config:
      sort: false
generates:
(...)

That worked for me, whereas specifying at the output level did not.

4reactions
dewevecommented, Aug 5, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Don't sort inside an enum field - Stack Overflow
I have an enum which references itself B(A), in that case Eclipse sort members breaks the declaration order and the program will not...
Read more >
grouping and sorting appears to be disabled for Enum column
As you can see, in the right grid the Enum column allows grouping and sorting - this is a default binding to a...
Read more >
172801 – [sort members] Allow sort fields/sort enum to ... - Bugs
This patch (a break-out from bug 133277) supplies the ability to do a 'sort members' on a class (either through the clean-up or...
Read more >
Managing Sorted Lists: Enum Administration
If you do not want to delete an enum value because you might need it again, you can disable it. To do so,...
Read more >
Solved: How to make "Enum Control" disabled during run-time ...
I would suggest using the Disabled property node. You can then disable the enum at the start of the program and then enable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found