diff --git a/config/config.exs b/config/config.exs index 22df26e..30ad7f1 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,5 +1,7 @@ import Config +config :absinthe_graphql_ws, :json_library, Jason + if config_env() == :test do import_config("test.exs") end diff --git a/lib/absinthe/graphql_ws/message/complete.ex b/lib/absinthe/graphql_ws/message/complete.ex index 21aa0db..843a67c 100644 --- a/lib/absinthe/graphql_ws/message/complete.ex +++ b/lib/absinthe/graphql_ws/message/complete.ex @@ -5,7 +5,9 @@ defmodule Absinthe.GraphqlWS.Message.Complete do message, no Complete message will be emitted. """ + alias Absinthe.GraphqlWS.Util + def new(id, payload \\ %{}) do - Jason.encode!(%{id: id, type: "complete", payload: payload}) + Util.json_library().encode!(%{id: id, type: "complete", payload: payload}) end end diff --git a/lib/absinthe/graphql_ws/message/connection_ack.ex b/lib/absinthe/graphql_ws/message/connection_ack.ex index 4dad62b..3b5ef98 100644 --- a/lib/absinthe/graphql_ws/message/connection_ack.ex +++ b/lib/absinthe/graphql_ws/message/connection_ack.ex @@ -3,7 +3,9 @@ defmodule Absinthe.GraphqlWS.Message.ConnectionAck do Must be sent in response to a ConnectionInit message from the client. """ + alias Absinthe.GraphqlWS.Util + def new(payload \\ %{}) do - Jason.encode!(%{type: "connection_ack", payload: payload}) + Util.json_library().encode!(%{type: "connection_ack", payload: payload}) end end diff --git a/lib/absinthe/graphql_ws/message/error.ex b/lib/absinthe/graphql_ws/message/error.ex index bce8ac7..7608337 100644 --- a/lib/absinthe/graphql_ws/message/error.ex +++ b/lib/absinthe/graphql_ws/message/error.ex @@ -4,7 +4,9 @@ defmodule Absinthe.GraphqlWS.Message.Error do the actual execution, usually due to validation errors. """ + alias Absinthe.GraphqlWS.Util + def new(id, payload \\ %{}) do - Jason.encode!(%{id: id, type: "error", payload: payload}) + Util.json_library().encode!(%{id: id, type: "error", payload: payload}) end end diff --git a/lib/absinthe/graphql_ws/message/next.ex b/lib/absinthe/graphql_ws/message/next.ex index b93bd51..a83bf0e 100644 --- a/lib/absinthe/graphql_ws/message/next.ex +++ b/lib/absinthe/graphql_ws/message/next.ex @@ -5,7 +5,9 @@ defmodule Absinthe.GraphqlWS.Message.Next do will follow indicating stream completion. """ + alias Absinthe.GraphqlWS.Util + def new(id, payload \\ %{}) do - Jason.encode!(%{id: id, type: "next", payload: payload}) + Util.json_library().encode!(%{id: id, type: "next", payload: payload}) end end diff --git a/lib/absinthe/graphql_ws/message/pong.ex b/lib/absinthe/graphql_ws/message/pong.ex index 9bffb9c..a0727e7 100644 --- a/lib/absinthe/graphql_ws/message/pong.ex +++ b/lib/absinthe/graphql_ws/message/pong.ex @@ -3,7 +3,9 @@ defmodule Absinthe.GraphqlWS.Message.Pong do Reply to a Ping message. """ + alias Absinthe.GraphqlWS.Util + def new(payload \\ %{}) do - Jason.encode!(%{type: "pong", payload: payload}) + Util.json_library().encode!(%{type: "pong", payload: payload}) end end diff --git a/lib/absinthe/graphql_ws/transport.ex b/lib/absinthe/graphql_ws/transport.ex index 3d7a627..3abd93d 100644 --- a/lib/absinthe/graphql_ws/transport.ex +++ b/lib/absinthe/graphql_ws/transport.ex @@ -11,8 +11,7 @@ defmodule Absinthe.GraphqlWS.Transport do their codebase, but is documented to help understand the intentions of the code. """ - alias Absinthe.GraphqlWS.Message - alias Absinthe.GraphqlWS.Socket + alias Absinthe.GraphqlWS.{Message, Socket, Util} alias Phoenix.Socket.Broadcast require Logger @@ -48,7 +47,7 @@ defmodule Absinthe.GraphqlWS.Transport do """ @spec handle_in({binary(), [opcode: :text]}, socket()) :: reply_inbound() def handle_in({text, [opcode: :text]}, socket) do - Jason.decode(text) + Util.json_library().decode(text) |> case do {:ok, json} -> handle_inbound(json, socket) diff --git a/lib/absinthe/graphql_ws/util.ex b/lib/absinthe/graphql_ws/util.ex index 387b056..a1cb223 100644 --- a/lib/absinthe/graphql_ws/util.ex +++ b/lib/absinthe/graphql_ws/util.ex @@ -62,4 +62,9 @@ defmodule Absinthe.GraphqlWS.Util do absinthe = %{socket.absinthe | opts: opts} %{socket | absinthe: absinthe} end + + @doc """ + Retrieves the JSON library module used to encode and decode transport messages + """ + def json_library, do: Application.get_env(:absinthe_graphql_ws, :json_library) end