From 648e49c3bb880e58079d21f5230e1d9efad6704d Mon Sep 17 00:00:00 2001 From: Joel Abshier Date: Mon, 4 Oct 2021 21:42:31 -0500 Subject: [PATCH] Add :created_user subscription --- lib/gql_preferences_web/endpoint.ex | 4 ++++ lib/gql_preferences_web/resolvers/users.ex | 2 ++ lib/gql_preferences_web/router.ex | 6 ++++-- lib/gql_preferences_web/schema.ex | 10 ++++++++++ lib/gql_preferences_web/user_socket.ex | 19 ++++--------------- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/gql_preferences_web/endpoint.ex b/lib/gql_preferences_web/endpoint.ex index 1a8abe6..e5797b2 100644 --- a/lib/gql_preferences_web/endpoint.ex +++ b/lib/gql_preferences_web/endpoint.ex @@ -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 diff --git a/lib/gql_preferences_web/resolvers/users.ex b/lib/gql_preferences_web/resolvers/users.ex index b378401..26742f7 100644 --- a/lib/gql_preferences_web/resolvers/users.ex +++ b/lib/gql_preferences_web/resolvers/users.ex @@ -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) diff --git a/lib/gql_preferences_web/router.ex b/lib/gql_preferences_web/router.ex index 4b22ae9..c09dbdb 100644 --- a/lib/gql_preferences_web/router.ex +++ b/lib/gql_preferences_web/router.ex @@ -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. diff --git a/lib/gql_preferences_web/schema.ex b/lib/gql_preferences_web/schema.ex index 6768357..ecbf90b 100644 --- a/lib/gql_preferences_web/schema.ex +++ b/lib/gql_preferences_web/schema.ex @@ -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 diff --git a/lib/gql_preferences_web/user_socket.ex b/lib/gql_preferences_web/user_socket.ex index 4687e61..3c526bb 100644 --- a/lib/gql_preferences_web/user_socket.ex +++ b/lib/gql_preferences_web/user_socket.ex @@ -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