dillonkearns / elm-graphql

Autogenerate type-safe GraphQL queries in Elm.

Home Page:https://package.elm-lang.org/packages/dillonkearns/elm-graphql/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Encode function for enums is missing

ad-si opened this issue · comments

I have following GraphQL Enum:

enum Order {
  ASC
  DESC
}

It generates an src/Api/Enum/Order.elm file with following type:

type Order
    = Asc
    | Desc

However, the generated src/Api/InputObject.elm file references a function encodeOrder which isn't defined anywhere.

I was able to fix it by adding it manually:

encodeOrder : Order -> Value
encodeOrder =
    Encode.enum <|
        \val ->
            case val of
                Asc ->
                    "ASC"

                Desc ->
                    "DESC"

Is this a bug and it should be generated automatically, or am I missing something?

It looks like the generated code uses the enum's toString functions within the InputObject module. Here's an example of what is generated for the Star Wars API example:

[ ( "language", Encode.enum Swapi.Enum.Language.toString |> Encode.optional input____.language ), ( "name", Encode.string input____.name |> Just ), ( "options", encodeGreetingOptions |> Encode.optional input____.options ) ]

So it appears that the basic case of encoding an enum within an InputObject is working as expected.

If you think there might be a bug, I would be interested to see a minimum reproduction of the relevant schema. There are some instructions for making a minimum reproduction here: https://github.com/dillonkearns/elm-graphql/blob/master/reproducing-issues/README.md