Zaid-Ajaj / Snowflaqe

A dotnet CLI to generate type-safe GraphQL clients for F# and Fable with automatic deserialization, static query verification and type checking
MIT License
154 stars 25 forks source link

CompiledName attribute not working for union type members #62

Closed petrkoutnycz closed 2 years ago

petrkoutnycz commented 2 years ago

Generated union types currently contain members with different casing than the original enums. Problem is that CompiledName attribute does not work with members of union type. It works with values and functions.

Example

Following code shows that value of PPA generates a property Ppa which causes a problem during the runtime.

enum MyEnumType { Shopping PPA }

produces

type ProductLabelDomainType = | [<CompiledName "Shopping">] Shopping | [<CompiledName "PPA">] Ppa

Zaid-Ajaj commented 2 years ago

Problem is that CompiledName attribute does not work with members of union type. It works with values and functions.

The JSON serializers at runtime know about CompiledName and it should work fine.

Are you targeting F# on dotnet or Fable?

Is your generated type missing [<Fable.Core.StringEnum>]? It should be there.

I will need more information in order to debug your issue. Snowflaqe config, JSON returned from the GraphQL API, the runtime error you are getting and how the GraphQL schema looks like.

petrkoutnycz commented 2 years ago

Here's the repo: https://github.com/petrkoutnycz/snowflaqe-issue-62

And this is the issue: image

petrkoutnycz commented 2 years ago

Generally, I dont think it is necessary to change the original casing.

Zaid-Ajaj commented 2 years ago

Hi @petrkoutnycz thanks a lot for the sample repo. At first glance, I think this might be the culprit "serializer": "system" can you try using the default serializer (by removing this config option or setting it to "newtonsoft") and see if the runtime errors disappear? I will do my own testing in the meantime

petrkoutnycz commented 2 years ago

I confirm that there is no exception with newtonsoft serializer, though the result is not successful and contains null values.

Zaid-Ajaj commented 2 years ago

Hmm that's interesting. Not sure why this would happen, the newtonsoft serializer definitely understands CompiledName. The only way for me to debug this, is if I have the JSON content and the types that you are trying to deserialize to. Let me test things out and see why this is happening

petrkoutnycz commented 2 years ago

I'd say that it'd be easier to skip the casing changes altogether to avoid such problems.

Zaid-Ajaj commented 2 years ago

I'd say that it'd be easier to skip the casing changes altogether

@petrkoutnycz unfortunately, GraphQL enums are not always valid as union case names by default. For example, they could be lower-case.

Until I've figured out why the serializers are failing in runtime, I've added a boolean option called normalizeEnumCases which is set to true by default (preserving current behavior) that determines whether changes to enum values should applied. To fix your issue, set this option to false in your snowflaqe.json:

{
    "normalizeEnumCases": false
}
petrkoutnycz commented 2 years ago

Thank you, I really appreciate your quick response :-)