Absinthe and storing JSONB

Hey there!

I think you’re misunderstanding the answer in each of those issues. The second issue is largely unrelated, as it’s essentially just a matter of how the syntax of custom scalars works broadly. In the first issue all we’re saying is that we aren’t going to include the custom scalar type by default, not that it’s impossible to have one.

In fact this first issue supplies a code snippet that you can just add to your schema to get the result you want:

scalar :json do
  parse fn input ->
    case Poison.decode(input.value) do
      {:ok, result} -> result
      _ -> :error
    end
  end

  serialize &Poison.encode!/1
end

The only caveat here is that Absinthe expects that you’ll send this JSON blob as an actual string since the idea is that it’s treated as an opaque blob as far as Absinthe is concerned. It’ll get decoded inside the parse function above and then it’s just a regular map w/ string keys that you can do with internally as you wish.

2 Likes