Skip to content

Commit

Permalink
feat(neuron_subscription.ex): abshinthe subscriptions
Browse files Browse the repository at this point in the history
Abshinthe subscriptions implementation based on elixir library abshinthe websocket.

uesteibar#33
  • Loading branch information
karlosmid committed Dec 15, 2021
1 parent 3169e4f commit 2c1825c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 53 deletions.
38 changes: 16 additions & 22 deletions lib/neuron_subscription.ex
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
defmodule Neuron.Subscription do
use GenServer

def start_link(opts) do
GenServer.start_link(Neuron.Subscription, opts)
end

@impl :true
@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, url: url) do

def supervisor(subscriber: subscriber, url: url, token: token) do
{AbsintheWebSocket.Supervisor,
[
subscriber: subscriber,
url: url,
token: nil,
token: token,
base_name: subscriber,
async: true
]}
end

def subscribe(module, query) do
callback = fn result ->
apply(module, :handle_update, [result])
end

AbsintheWebSocket.SubscriptionServer.subscribe(
Module.concat(module, SubscriptionServer),
Neuron.Subscription,
callback,
query,
[]
)
end
end
31 changes: 0 additions & 31 deletions test/neuron_subscription_test.exs

This file was deleted.

0 comments on commit 2c1825c

Please sign in to comment.