grantila / typeconv

Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType
MIT License
424 stars 8 forks source link

json-schema enums should translate to GraphQL enums #6

Closed mdesousa closed 2 years ago

mdesousa commented 3 years ago

Hi, this is a great library... thanks for creating it! We are running into an issue where we have types in json-schema defined as

"Color": {
  "type": "string",
  "enum": [
    "yellow",
    "blue",
    "red"
  ]
},

Those should convert to an enum in GraphQL. However, it is converting into a union of a String.

Expected:

enum Color {
  yellow
  blue
  red
}

Received:

union Color = String

Thanks

grantila commented 3 years ago

This is not always possible, e.g. a string union of 'yellow' | 'dark-blue' can't be converted into a GraphQL union without transforming those strings (e.g. darkBlue or darkblue).

What is possible, is to add an option to try to convert into enums when all the strings are formed in an enum-compatible way.. I'll have a look at that!

grantila commented 2 years ago

Closing for now, might be possible to introduce some kind of programmatic transform function, but then the enums don't look the same. I suggest using string unions instead, which is compatible with TypeScript, JSON Schema and GraphQL.