dillonkearns / elm-graphql

Autogenerate type-safe GraphQL queries in Elm.
https://package.elm-lang.org/packages/dillonkearns/elm-graphql/latest
BSD 3-Clause "New" or "Revised" License
778 stars 107 forks source link

Encode function for enums is missing #634

Open ad-si opened 1 year ago

ad-si commented 1 year ago

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?

dillonkearns commented 11 months ago

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:

https://github.com/dillonkearns/elm-graphql/blob/3f9ccbedd879d6e564fe7728cd4cda0bf9d399b0/examples/src/Swapi/InputObject.elm#L58

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