-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow sorting in enums to be disabled #6935
Comments
We apply sorting not only enum keys but also fields, o ject definitions etc. Currently there is no option to disable sorting for enum values specifically but instead you can disable all kind of sorting with |
@ardatan thank you! Is that behaviour documented anywhere? I tried specifying I still think it would be nice to have an option to disable sorting for enums. It's easier to browse through the code when it's sorted alphabetically, but it would be great if there was an option that for maintaining the index positioning of the enum values. |
@andbjer One could argue whether enum values are actually meant to be used like that. The ECMA specification does not clearly state that each js engine has to implement From a client perspective, an alternative approach could be to have a field on your Query root type that exposes a list of type Query {
daysOfWeek: [DayOfWeek!]!
} I really think this is simply too uncertain to support it as a separate config option. |
I saw this doc and my expectation is schema: http://localhost:3000/graphql
generates:
schema.graphql:
plugins:
- schema-ast
config:
commentDescriptions: true
sort: false also, it says default is false
|
I checked this code, sort config is not used in the |
@smmoosavi Thank you for the report! We have updated the documentation now. |
@ardatan |
https://www.graphql-code-generator.com/plugins/schema-ast Default updated to true. You can manually set it to false to disable sorting |
@n1ru4l I was not aware that this could differ from browser to browser. Then I guess the solution you propose is probably the best option. |
it didn't work for me :( could you please provide a complete codegen.yml this is my codegen.yaml, I don't know what I am missing
|
@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. 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. |
Is this supposed to still work/work for code-based configuration? In my GraphQL codegen config file I have: const codegenConfig = {
...
config: {
sort: false
}
...
} which has no effect on the enum sorting. |
@coopbri import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'schema.graphql',
config: {
sort: false,
},
...
} |
Thanks for confirming @ha-akelius! I have the same config so I must have another conflict. That helped me confirm something else is causing sorting to stay enabled for me, I'll do some more testing today. |
I believe ECMA specifies the order https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#description
|
Unfortunately, it is probably out of control of and enum values are being sorted there (which is clearly wrong, enum values should not be reordered as it conveys change of their internal integer values): At the beginning of that Meanwhile, I used patch-package to patch graphql for my needs. This fixes the issue: @@ -0,0 +1,12 @@
diff --git a/node_modules/graphql/utilities/lexicographicSortSchema.js b/node_modules/graphql/utilities/lexicographicSortSchema.js
index ea884b6..49ab981 100644
--- a/node_modules/graphql/utilities/lexicographicSortSchema.js
+++ b/node_modules/graphql/utilities/lexicographicSortSchema.js
@@ -97,6 +97,7 @@ function lexicographicSortSchema(schema) {
function sortNamedType(type) {
if (
(0, _definition.isScalarType)(type) ||
+ (0, _definition.isEnumType)(type) ||
(0, _introspection.isIntrospectionType)(type)
) {
return type; |
Its also worked for me!!! |
None of the options worked for me, and considering this reply here, should the issue be reopened? |
I ran into this same issue, and I'm having a problem where disabling the sorting of the whole generated schema causes the generated types to order themselves randomly, in a way that can change between each run of codegen. We commit our generated code to our repo and have a CI step that fails if codegen changes anything, which is broken on newer versions due to this randomness. |
It's possible to disable the sorting with a patch on the graphql package, which has solved by issue, but it would be nice for the option to be fully supported. |
Describe the bug
My schema contains an enum for
DayOfWeek
:When GraphQL Code Generator creates a typescript type for this enum, it will sort the values alphabetically like below:
This is an issue if you would like to retrieve the index value of the enum:
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
codegen.yml
config file: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
The text was updated successfully, but these errors were encountered: