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

Issue with Jsonb field?

silky opened this issue · comments

Hello!

I'm using this library while I'm following along on the hasura elm tutorial - https://hasura.io/learn/graphql/elm-graphql/elm-graphql/

My table has a jsonb field in it (the field is called tags), and when I try and retrieve it, I get the following error:

Error: HttpError (
    BadPayload (
      OneOf [
        OneOf [
          Field "data" (
            Field "jobs" (
              Index 0 (
                Field "tags3220756317" (
                  OneOf [
                    Failure "Expecting a STRING" <internals>
                  ,Failure "Expecting a FLOAT" <internals>
                  ,Failure "Expecting an INT" <internals>
                  ,Failure "Expecting a BOOL" <internals>
                ]
              )
            )
          )
        )
        ,Failure "Expecting an OBJECT with a field named `errors`" <internals>
        ,Failure "Expecting an OBJECT with a field named `errors`" <internals>
      ]
      ,Field "data" (
          Field "jobs" (
            Index 0 (
              Field "tags3220756317" (
                OneOf [
                  Failure "Expecting a STRING" <internals>
                ,Failure "Expecting a FLOAT" <internals>
                ,Failure "Expecting an INT" <internals>
                ,Failure "Expecting a BOOL" <internals>
              ]
            )
          )
        )
      )
    ]
  )
)

My query is something like this:

selectJobs : SelectionSet Job Thing.Object.Jobs
selectJobs =
  SelectionSet.map7 Job .... (Jobs.tags identity)

where Job is a record who's last element is tags : Jsonb.

I don't think I'm doing anything wrong?

Hello!

You'll need to use the custom scalar codecs feature for that. There's an example in the examples folder that uses that:

https://github.com/dillonkearns/elm-graphql/blob/02c59a90ba497d9e2e88aeb181bc941239b23214/examples/src/CustomScalarCodecs.elm

And I have a short video tutorial on setting it up:

https://incrementalelm.com/scalar-codecs-tutorial/

I've had some discussions on Slack. To summarize, Custom Scalars in GraphQL are a blind spot in the type system. There is no type information. The default generated elm-graphql code has a simple default decoder for Custom Scalars that tries to decode them as either a String, Float, Int, or Bool, and then turns them into a String and wraps it in a custom type wrapper.

If the custom scalar is anything besides those primitive types, then it will fail. I think it would make sense for elm-graphql to eventually have the Custom Scalar Codecs feature be the default and only way to deal with scalars, since it's a more precise representation.

Another option would be to decode Custom Scalar values in the default version as a Json.Decode.Value. That way, the user needs to run a JSON decoder on the result to get a value (whether it's a String, Int, or a complex JSON value).

I created a discussion, #561, to capture the conversation about different ways to improve the default custom scalar handling. Thanks for the discussion!