ekobi

ekobi

Help implementing graphql-ws subprotocol support in Absinthe

Hi,

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)}
  end

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.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement