From c2e35b375fd42bfe3492fc7160155845822d5b45 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sat, 31 Aug 2019 11:49:37 +1200 Subject: [PATCH 1/2] Add support for brokered subscriptions When configuration `:websocket_subscriptions` is set to false, don't respond to subscriptions with a chunked event stream and enter a loop sending subscription updates; instead, send the subscription ID to the client and end the request. --- lib/absinthe/plug.ex | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/absinthe/plug.ex b/lib/absinthe/plug.ex index 2215ea6..caa4c89 100644 --- a/lib/absinthe/plug.ex +++ b/lib/absinthe/plug.ex @@ -142,6 +142,7 @@ defmodule Absinthe.Plug do - `:log_level` -- (Optional) Set the logger level for Absinthe Logger. Defaults to `:debug`. - `:analyze_complexity` -- (Optional) Set whether to calculate the complexity of incoming GraphQL queries. - `:max_complexity` -- (Optional) Set the maximum allowed complexity of the GraphQL query. If a document’s calculated complexity exceeds the maximum, resolution will be skipped and an error will be returned in the result detailing the calculated and maximum complexities. + - `:websocket_subscriptions` -- (Optional) Whether to assume subscriptions are sent through and receive updates from a WebSocket. (default: `true`). """ @type opts :: [ @@ -160,7 +161,8 @@ defmodule Absinthe.Plug do serializer: module | {module, Keyword.t()}, content_type: String.t(), before_send: {module, atom}, - log_level: Logger.level() + log_level: Logger.level(), + websocket_subscriptions: boolean ] @doc """ @@ -206,6 +208,8 @@ defmodule Absinthe.Plug do before_send = Keyword.get(opts, :before_send) + websocket_subscriptions = Keyword.get(opts, :websocket_subscriptions, true) + %{ adapter: adapter, context: context, @@ -219,7 +223,8 @@ defmodule Absinthe.Plug do content_type: content_type, log_level: log_level, pubsub: pubsub, - before_send: before_send + before_send: before_send, + websocket_subscriptions: websocket_subscriptions } end @@ -300,11 +305,15 @@ defmodule Absinthe.Plug do def subscribe(conn, topic, %{context: %{pubsub: pubsub}} = config) do pubsub.subscribe(topic) - - conn - |> put_resp_header("content-type", "text/event-stream") - |> send_chunked(200) - |> subscribe_loop(topic, config) + if config[:websocket_subscriptions] do + conn + |> put_resp_header("content-type", "text/event-stream") + |> send_chunked(200) + |> subscribe_loop(topic, config) + else + conn + |> encode(200, %{ meta: %{ subscriptionId: topic } }, config) + end end def subscribe_loop(conn, topic, config) do From c990c902b799af50d7f3fa6716dfb8bb34ca3f8d Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sat, 31 Aug 2019 12:27:19 +1200 Subject: [PATCH 2/2] Fix format --- lib/absinthe/plug.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/absinthe/plug.ex b/lib/absinthe/plug.ex index caa4c89..b1d278b 100644 --- a/lib/absinthe/plug.ex +++ b/lib/absinthe/plug.ex @@ -305,6 +305,7 @@ defmodule Absinthe.Plug do def subscribe(conn, topic, %{context: %{pubsub: pubsub}} = config) do pubsub.subscribe(topic) + if config[:websocket_subscriptions] do conn |> put_resp_header("content-type", "text/event-stream") @@ -312,7 +313,7 @@ defmodule Absinthe.Plug do |> subscribe_loop(topic, config) else conn - |> encode(200, %{ meta: %{ subscriptionId: topic } }, config) + |> encode(200, %{meta: %{subscriptionId: topic}}, config) end end