What JSON encoder is Absinthe using? Can you configure it?

I recently switched from Poison to Jason and was wondering what is Absinthe using for encoding (and decoding) JSON? :thinking:Is there an option to specify a library? Thanks!

1 Like

Assuming you use :absinthe_plug it uses whatever :json_decoder is configured for Plug.Parsers.

At least thats how I understand it after quickly skimming that readme.

Absinthe itself will never see the JSON, but only the query string from it.

Yeah I noticed the decoder option, but what about encoding to JSON when replying to the client? The docs tell you to just install a library, but I have both Poison and Jason installed as dependencies, so I’m guessing it would just end up using Poison :thinking:

I think you need to set json_codec option to Jason if you want absinthe_plug to use jason.

1 Like

Ah I see, I didn’t look at the absinthe_plug repo at all. That would be an option for Absinthe.Plug then? E.g.

forward "/", Absinthe.Plug, schema: GraphQL.Schema, json_codec: Jason

That doesn’t seem to produce any errors so I’ll try it out! Thanks! :beers:

It’s documented here: https://hexdocs.pm/absinthe_plug/Absinthe.Plug.html#t:opts/0:

:json_codec — (Optional) A module or {module, Keyword.t} dictating which JSON codec should be used (default: Poison). The codec module should implement encode!/2 (e.g., module.encode!(body, opts)).


Jason also documents how to do switch default codec in Absinthe: https://github.com/michalmuskala/jason#absinthe

You need to pass the :json_codec option to Absinthe.Plug

plug Absinthe.Plug,
  schema: MyApp.Schema,
  json_codec: Jason

# When used in phoenix router:
forward "/api",
  to: Absinthe.Plug,
  init_opts: [schema: MyApp.Schema, json_codec: Jason]

Hm, I noticed you pass the options using init_opts and you use to: which is different from Absinthe’s docs?


I tried this out anyway and got:

** (ArgumentError) cannot convert the given list to a string.

To be converted to a string, a list must contain only:

  * strings
  * integers representing Unicode codepoints
  * or a list containing one of these three elements

Please check the given list or call inspect/1 to get the list representation, got:

[to: Absinthe.Plug, init_opts: [schema: GraphQL.Schema, json_codec: Jason]]

    (elixir) lib/list.ex:821: List.to_string/1
    (phoenix) lib/phoenix/naming.ex:21: Phoenix.Naming.resource_name/2
    (phoenix) lib/phoenix/router/scope.ex:25: Phoenix.Router.Scope.route/8
    lib/galileo_web/router.ex:53: (module)
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

I’m using Plug 1.5.1 and (I do have 1.5.1 in mix.lock but also noticed that Phoenix has :plug, "~> 1.3.3 or ~> 1.4" it’s probably using older Plug?) and Phoenix 1.3.2. Do you think this would work at all?

forward "/", Absinthe.Plug, schema: GraphQL.Schema, json_codec: Jason

Please note that absinthe_plug 1.4.6 cannot use Jason on error response. The fix is in master, but wasn’t released to Hex yet.