Skip to content

Commit

Permalink
docs: update GenServer example
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Mar 31, 2024
1 parent 1687dd1 commit 9fbf938
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions examples/gen_server.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule EventLogger do
use GenServer

alias GraphQLWSClient.Event

def start_link(socket) do
GenServer.start_link(__MODULE__, socket)
end
Expand Down Expand Up @@ -45,34 +47,23 @@ defmodule EventLogger do
end

def handle_info(
%GraphQLWSClient.Event{
type: :complete,
subscription_id: subscription_id
},
%Event{type: :complete, subscription_id: subscription_id},
%{subscription_id: subscription_id} = state
) do
IO.puts("complete")
{:noreply, state}
end

def handle_info(
%GraphQLWSClient.Event{
type: :next,
subscription_id: subscription_id,
payload: payload
},
%Event{type: :next, subscription_id: subscription_id, payload: payload},
%{subscription_id: subscription_id} = state
) do
IO.inspect(payload)
{:noreply, state}
end

def handle_info(
%GraphQLWSClient.Event{
type: :error,
subscription_id: subscription_id,
payload: error
},
%Event{type: :error, subscription_id: subscription_id, payload: error},
%{subscription_id: subscription_id} = state
) do
IO.inspect(error, label: "error")
Expand All @@ -82,14 +73,12 @@ defmodule EventLogger do
def handle_info(_msg, state), do: {:noreply, state}
end

IO.puts("Waiting for events...")

{:ok, socket} =
GraphQLWSClient.start_link(url: "ws://localhost:8080/subscriptions")

EventLogger.start_link(socket)

Process.sleep(2000)
Process.sleep(1000)

mutation = """
mutation CreatePost($author: String!, $body: String!) {
Expand Down

0 comments on commit 9fbf938

Please sign in to comment.