I recently switched from Poison
to Jason
and was wondering what is Absinthe using for encoding (and decoding) JSON? Is there an option to specify a library? Thanks!
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
I think you need to set json_codec
option to Jason
if you want absinthe_plug
to use jason.
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!
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: GitHub - michalmuskala/jason: A blazing fast JSON parser and generator in pure Elixir.
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?
https://hexdocs.pm/absinthe/plug-phoenix.html#phoenix
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.