Skip to content

Commit

Permalink
Add :created_user subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
abshierjoel committed Oct 5, 2021
1 parent dac639c commit 648e49c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
4 changes: 4 additions & 0 deletions lib/gql_preferences_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ defmodule UserPreferencesWeb.Endpoint do

socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]

socket "/websocket", UserPreferencesWeb.UserSocket,
websocket: true,
longpoll: false

# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phx.digest
Expand Down
2 changes: 2 additions & 0 deletions lib/gql_preferences_web/resolvers/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ defmodule UserPreferencesWeb.Resolvers.Users do
|> User.changeset(args)
|> Repo.insert()

Absinthe.Subscription.publish(UserPreferencesWeb.Endpoint, user, created_user: "*")

{:ok, pref} =
args.preferences
|> Map.put(:user_id, user.id)
Expand Down
6 changes: 4 additions & 2 deletions lib/gql_preferences_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ defmodule UserPreferencesWeb.Router do
scope "/" do
pipe_through :api

forward "/api", Absinthe.Plug, schema: UserPreferencesWeb.Schema

forward "/graphiql", Absinthe.Plug.GraphiQL,
schema: UserPreferencesWeb.Schema,
socket: UserPreferencesWeb.UserSocket,
interface: :simple

# forward "/", Absinthe.Plug.GraphiQL,
# schema: UserPreferencesWeb.Schema,
# socket: UserPreferencesWeb.UserSocket
end

# Other scopes may use custom stacks.
Expand Down
10 changes: 10 additions & 0 deletions lib/gql_preferences_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ defmodule UserPreferencesWeb.Schema do
end
end

subscription do
field :created_user, :user do
config(fn args, info ->
IO.inspect(args, label: "ARGS")
IO.inspect(info, label: "INFO")
{:ok, topic: "*"}
end)
end
end

connection(node_type: :user)

node interface do
Expand Down
19 changes: 4 additions & 15 deletions lib/gql_preferences_web/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@ defmodule UserPreferencesWeb.UserSocket do
use Absinthe.Phoenix.Socket,
schema: UserPreferencesWeb.Schema

def connect(params, socket) do
current_user = current_user(params)

socket =
Absinthe.Phoenix.Socket.put_options(socket,
context: %{
current_user: current_user
}
)

{:ok, socket}
end

defp current_user(%{"user_id" => id}) do
MyApp.Repo.get(User, id)
def connect(_params, socket) do
socket
|> Absinthe.Phoenix.Socket.put_options(context: %{})
|> then(&{:ok, &1})
end

def id(_socket), do: nil
Expand Down

0 comments on commit 648e49c

Please sign in to comment.