forked from uesteibar/neuron
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(neuron_subscription.ex): support for subscriptions where transpo…
…rt is Phoenix Channel Implementation is based on abshinte_websocket library. fix uesteibar#33
- Loading branch information
Showing
4 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Neuron.Subscription do | ||
use GenServer | ||
|
||
def start_link(opts) do | ||
GenServer.start_link(Neuron.Subscription, opts) | ||
end | ||
|
||
@impl :true | ||
def init(opts) do | ||
query = Keyword.fetch!(opts, :query) | ||
name = Keyword.fetch!(opts, :name) | ||
variables = Keyword.fetch!(opts, :variables) | ||
callback = Keyword.fetch!(opts, :callback) | ||
|
||
subscription_server_name = Module.concat([name, Caller, SubscriptionServer]) | ||
|
||
AbsintheWebSocket.SubscriptionServer.subscribe( | ||
subscription_server_name, | ||
name, | ||
callback, | ||
query, | ||
variables | ||
) | ||
{:ok, opts} | ||
end | ||
|
||
def supervisor(subscriber: subscriber) do | ||
|
||
{AbsintheWebSocket.Supervisor, | ||
[ | ||
subscriber: subscriber, | ||
url: subscriber.url, | ||
token: nil, | ||
base_name: subscriber, | ||
async: true | ||
]} | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule SubscriptionExample do | ||
|
||
@url "https://my.awesome.graphql/endpoint" | ||
@query """ | ||
subscription { | ||
user { | ||
name | ||
} | ||
} | ||
""" | ||
|
||
def start_link() do | ||
Neuron.Subscription.start_link(ws_url: @url, query: @query, name: __MODULE__, subscriber: __MODULE__, variables: %{}, callback: :handle_update) | ||
end | ||
|
||
def handle_update(data, state) do | ||
IO.puts("Received Update - #{inspect(data)}") | ||
|
||
{:ok, state} | ||
end | ||
end | ||
|
||
defmodule NeuronSubscriptionTest do | ||
use ExUnit.Case | ||
|
||
describe "subscription" do | ||
test "ok" do | ||
{:ok, _pid} = SubscriptionExample.start_link() | ||
end | ||
end | ||
end |