Describe the bug
Graphql Enums have keys and values. Seems graphql-codegen uses only keys for generaring enums in typescript.
See example below:
// Such GQL enum
new GraphQLEnumType({
name: 'FvFormFillerServiceControlTypes',
values: {
Text1: {
value: 'lala'
}
}
})
// will be converted into such typescript enum:
export enum GQLFvFormFillerServiceControlTypes {
Text1 = 'Text1',
}
// Expected typescript enum is:
export enum GQLFvFormFillerServiceControlTypes {
Text1 = 'lala',
}
in this case you will have to map your enum values in your codegen.yml file, field
generates:
schema.ts:
plugins:
- typescript
config:
enumValues:
FvFormFillerServiceControlTypes:
Text1: lala
This is obviously incorrect behavior. GraphQL has in its enum key and value. Typescript has in its enum key and value. It seems logically correct to map key to key and value to value. Codegen should generate code rather than make me insert manually such enums :)
I believe I can even create pull for that:)
I didn't read the code, but my guess would be that they work with the resulting generated .graphql file or just consider not using the defined values to cover for frontend users, who doesn't have information about internal enum mapping in the server and will be consuming the graphql only with the plain values.
Maybe there should be an option to chose between using the internal values in enum or not
Maybe there should be an option to chose between using the internal values in enum or not
As an non-breaking feature - definitely
@deser You can use enumValues for internal mapping.
https://graphql-code-generator.com/docs/plugins/typescript#enumvalues-enumvaluesmap
This was suggested above and I described why this doesn't match :)
Currently GraphQL Codegen uses definitions only in the visitor implementations of all plugins. It doesn't respect the actual GraphQLNamedTypes like GraphQLEnumType as in your example. It introspects the schema and parses again to make sure it has a valid astNode to make the visitors work correctly.
I pushed some changes on GraphQL Toolkit to keep actual GraphQLSchema object by fixing its astNode to make it work properly with the visitors instead of printing and reparsing as DocumentNode. (Visitors use DefinitionNode not NamedType objects)
https://github.com/ardatan/graphql-toolkit/commit/4d94736a02498d267724e0e461dddcb719e84247
I also did some changes on TypeScript visitor to use existing enum values if there is any defined in GraphQLEnumType.
https://github.com/dotansimha/graphql-code-generator/pull/3147
When all those come together, we will directly use your GraphQLSchema object with your GraphQLNamedTypes and also we will fix it automatically inside GraphQL Toolkit if there is any problem with this GraphQLSchema object.
It might be better to call it as an enhancement than a bug because every single type in typescript package is generated to be used as typings not actual code.
I'll let you know when the PR is ready to test :)
every single type in typescript package is generated to be used as typings not actual code.
this may be the tricky part, since enums will be compiled to actual objects which may be used for code
@deser Could you try with the following version?
1.9.2-alpha-1455536b.55+1455536b
@ardatan ,
Firstly - thanks a lot :)
Secondly - yes, I'll try 1.9.2-alpha-1455536b.55+1455536b. Will respond soon
Updated:
"@graphql-codegen/cli": "1.9.2-alpha-1455536b.55",
"@graphql-codegen/typescript": "1.9.2-alpha-1455536b.55",
Seeing errors:
Unable to download schema from remote: Unknown operation named "introspection".
Error: Unable to download schema from remote: Unknown operation named "introspection".
@deser I fixed the introspection query in URL loader but I don't think it is possible to get enum values from the schema's introspection because it is not part of schema definition/SDL. Could you try with the actual code file that exports schema?
Last time I tried I failed to do it. We define schema in old school way (literally constructing new GraphQLObjectType ) and writing script which requires that schema as a module and generates graphql types failed. Therefore the only way I found working is introspection schema :)
Let me try again maybe something has changed from the last time.
Should I install 1.9.2-alpha-1455536b.55 again?
You can try it with 1.9.2-alpha-74dff252.63+74dff252. I added tests for your case.
ok thx
Doesnt work with introspection schema. Will try to work with schema directly
@deser I fixed the introspection query in URL loader but I don't think it is possible to get enum values from the schema's introspection because it is not part of schema definition/SDL. Could you try with the actual code file that exports schema?
@deser It will never work with introspection and SDL because enum values are not part of the schema definition. You should try the exported schema as I said above.
This is the schema object;
https://github.com/dotansimha/graphql-code-generator/blob/9a84be6ea640ecfc5eb26817e9dca54a44751d33/dev-test/test-schema/schema-formats.js#L16
This is the configuration;
https://github.com/dotansimha/graphql-code-generator/blob/9a84be6ea640ecfc5eb26817e9dca54a44751d33/dev-test/codegen.yml#L63
This is the generated file;
https://github.com/dotansimha/graphql-code-generator/blob/master/dev-test/test-schema/typings.enum.ts
Sure, I'm trying that. Thank you
It works. Thanks a lot!
Found a possible issue:
config:
declarationKind: 'interface'
typesPrefix: GQL
skipTypename: true
addUnderscoreToArgsType: true
maybeValue: T
With this config it generates incorrect types:

Previously it was ok:

And adding enumPrefix: GQL doesn't help. Seems bug
I think it should be fixed in v1.10.0.
@deser let me know if it needs to be reopened and fixed again.
Ok thx a lot!
Most helpful comment
This is obviously incorrect behavior. GraphQL has in its enum key and value. Typescript has in its enum key and value. It seems logically correct to map key to key and value to value. Codegen should generate code rather than make me insert manually such enums :)
I believe I can even create pull for that:)