Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom JSON library support #3

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Config

config :absinthe_graphql_ws, :json_library, Jason

if config_env() == :test do
import_config("test.exs")
end
4 changes: 3 additions & 1 deletion lib/absinthe/graphql_ws/message/complete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion lib/absinthe/graphql_ws/message/connection_ack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion lib/absinthe/graphql_ws/message/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion lib/absinthe/graphql_ws/message/next.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion lib/absinthe/graphql_ws/message/pong.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions lib/absinthe/graphql_ws/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions lib/absinthe/graphql_ws/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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