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

Change SSE response structure so that it's compatible with GraphQL SSE standard #292

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 1 addition & 1 deletion lib/absinthe/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ defmodule Absinthe.Plug do
def subscribe_loop(conn, topic, config) do
receive do
%{event: "subscription:data", payload: %{result: result}} ->
case chunk(conn, "#{encode_json!(result, config)}\n\n") do
case chunk(conn, "event: next\ndata: #{encode_json!(result, config)}\n\n") do
{:ok, conn} ->
subscribe_loop(conn, topic, config)

Expand Down
15 changes: 7 additions & 8 deletions test/lib/absinthe/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,13 @@ defmodule Absinthe.PlugTest do
conn = Task.await(request)
{_module, state} = conn.adapter

events =
state.chunks
|> String.split()
|> Enum.map(&Jason.decode!/1)

assert length(events) == 2
assert Enum.member?(events, %{"data" => %{"update" => "FOO"}})
assert Enum.member?(events, %{"data" => %{"update" => "BAR"}})
[event1, event2] = String.split(state.chunks, "\n\n", trim: true)

assert "event: next\ndata: " <> event1_data = event1
assert "event: next\ndata: " <> event2_data = event2

assert Jason.decode!(event1_data) == %{"data" => %{"update" => "FOO"}}
assert Jason.decode!(event2_data) == %{"data" => %{"update" => "BAR"}}
end

@query """
Expand Down