Help implementing graphql-ws subprotocol support in Absinthe


Many GraphQL clients implement subscriptions using the graphql-ws WebSockets subprotocol. I have been experimenting with adding support in Absinthe-based servers, and could use some help getting it over the finish line.

The basic idea is to define a separate mount point, e.g. /graphql-ws, which gets routed to a socket with a different JSON serializer:

  socket "/graphql-ws", GrapixDemo.UserSocket,
    websocket: [
      subprotocols: ["graphql-ws"],
      serializer: [{Grapix.GraphqlWS.V1.JSONSerializer, ">= 1.0.0"}]
    longpoll: false

What I can’t seem to figure out is how to inject the client-defined reference ID into the subscription data updates processed by fastlane!(%{Broadcast} = msg) in the serializer implementation:

  def fastlane!(%Broadcast{} = msg) do
    msg |> IO.inspect(label: "#{__MODULE__}/fastlane -- broadcast msg")

    # "1" should be client-defined refId
    data = phoenix2gql(nil, "1", msg.topic, msg.event, msg.payload.result)

    {:socket_push, :text, Phoenix.json_library().encode_to_iodata!(data)}

I’m assuming that the Broadcast message omits the client-specific reference ID by design – it’s a broadcast, after all. But Absinthe clearly keeps track of per-client and per-subscription data with a unique topic:sub-topic identifier:

Elixir.Grapix.GraphqlWS.V1.JSONSerializer/fastlane -- broadcast msg: %Phoenix.Socket.Broadcast{
  event: "subscription:data",
  payload: %{
    result: %{
      data: %{
        "serviceStatus" => %{
          "__typename" => "ServiceStatus",
          "messages" => [
              "__typename" => "ServiceStatusMessage",
              "level" => "ADVISORY",
              "message" => "Wake up, dude!",
              "timestamp" => "2020-03-17T15:25:25.170999Z"
    subscriptionId: "__absinthe__:doc:73257102"
  topic: "__absinthe__:doc:73257102"

Any thoughts on how to proceed? Thanks.