elm-graphql icon indicating copy to clipboard operation
elm-graphql copied to clipboard

Enums with numbers at the beginning generate invalid code

Open Munksgaard opened this issue 6 years ago • 6 comments

I have the following graphql enum:

enum Timeframe {
  _1MONTH
  _2MONTHS
  _3MONTHS
  _6MONTHS
  _1YEAR
  _2YEARS
  _3YEARS
  _5YEARS
  _10YEARS
  INDEFINITE
}

which elm-graphql 1.0.7 turns into the following elm code:


type IdeaTimeframe
    = 1month_
    | 2months_
    | 3months_
    | 6months_
    | 1year_
    | 2years_
    | 3years_
    | 5years_
    | 10years_
    | Indefinite

Unfortunately, Elm doesn't support numbers at the beginning of enum variants, so it doesn't compile.

I'm not sure what the best fix is. If Elm supported it, I would put an underscore at the beginning of the type variants, but that's a no go too.

Munksgaard avatar Nov 23 '18 11:11 Munksgaard

This is relatively straightforward to fix once we decide on a way to normalize it.

Here are some ideas:

  1. N__1month
  2. Scalar__1month
  3. Number__1month
  4. NUMBER__1month

I'm open to other ideas, too. Unfortunately I think it's a case where there's no ideal solution, it'll probably end up looking a little bit ugly. 🤷‍♂️

Let me know what you think and I'll publish a fix! Thanks for reporting the problem.

dillonkearns avatar Nov 27 '18 16:11 dillonkearns

@dillonkearns. You're probably right about there not being an ideal solution. Of the options described above I'd probably prefer the first one (N_1month).

I actually ran into a related problem with lowercase SQL enums, that elm-graphql turned into uppercase values, but that meant that the decoder and encoder didn't work. I'll write up a proper issue at some point.

Munksgaard avatar Nov 27 '18 16:11 Munksgaard

@Munksgaard thanks for the feedback! N_1month seems okay to me, too.

Yes, if you give me a schema that can reproduce the problem you encountered that would be super helpful!

dillonkearns avatar Nov 27 '18 23:11 dillonkearns

A little late to the party here, but I'm having issues with a schema.gql like

type Query { 
  _0: String!
}

Thanks again for the brilliant library! :rocket:

hovind avatar Mar 12 '20 14:03 hovind

When generating elm-graphql types using Wordpress (through wp-graphql) as source, I came across this particular custom type:

type MediaItemSizeEnum
    = Large
    | Medium
    | MediumLarge
    | PostThumbnail
    | Thumbnail
    | 1536x1536_
    | 2048x2048_

It follows a similar issue as the first one described above but I'm leaving it here as reference.

georgesboris avatar Sep 28 '21 13:09 georgesboris

And I'm also in favor of N_{type} 🙂

georgesboris avatar Sep 28 '21 13:09 georgesboris