Skip to content

Commit

Permalink
Move actvity monitoring state into a GenSever and add calls from ea…
Browse files Browse the repository at this point in the history
…ch GQL field
  • Loading branch information
Joel Abshier committed Oct 4, 2021
1 parent 4d032a8 commit 12bf5ce
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 66 deletions.
26 changes: 0 additions & 26 deletions lib/gql_preferences/activity.ex

This file was deleted.

7 changes: 4 additions & 3 deletions lib/gql_preferences/activity_monitor.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule ActivityMonitor do
@activity_server GenServer.whereis(ActivityServer)
def update_resolver_activity(key), do: @activity_server |> GenServer.cast({:update, key})
def fetch_resolver_activity(key), do: @activity_server |> GenServer.call({:get, key})
def update_resolver_activity(key), do: context() |> GenServer.cast({:update, key})
def fetch_resolver_activity(key), do: context() |> GenServer.call({:get, key})

defp context(), do: GenServer.whereis(ActivityServer)
end
18 changes: 18 additions & 0 deletions lib/gql_preferences/activity_server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule ActivityServer do
use GenServer

def start_link(_), do: GenServer.start_link(__MODULE__, %{}, name: ActivityServer)
def init(_), do: {:ok, %{}}

def handle_cast({:update, key}, state) do
{:noreply, Map.update(state, key, 1, &(&1 + 1))}
end

def handle_call({:get, key}, _from, state) do
if Map.has_key?(state, key) do
{:reply, {:ok, Map.get(state, key)}, state}
else
{:reply, {:error, "Requested key: #{key} is invalid"}, state}
end
end
end
4 changes: 3 additions & 1 deletion lib/gql_preferences/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule UserPreferences.Application do
@moduledoc false

use Application
alias ActivityServer

@impl true
def start(_type, _args) do
Expand All @@ -15,9 +16,10 @@ defmodule UserPreferences.Application do
# Start the PubSub system
{Phoenix.PubSub, name: UserPreferences.PubSub},
# Start the Endpoint (http/https)
UserPreferencesWeb.Endpoint
UserPreferencesWeb.Endpoint,
# Start a worker by calling: UserPreferences.Worker.start_link(arg)
# {UserPreferences.Worker, arg}
ActivityServer
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
6 changes: 6 additions & 0 deletions lib/gql_preferences_web/resolvers/activity.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule UserPreferencesWeb.Resolvers.Activity do
def get_activity(_, args, _) do
ActivityMonitor.update_resolver_activity("get_activity")
ActivityMonitor.fetch_resolver_activity(args.key)
end
end
10 changes: 10 additions & 0 deletions lib/gql_preferences_web/resolvers/preferences.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ defmodule UserPreferencesWeb.Resolvers.Preferences do
alias UserPreferences.{Repo, Preferences}

def get_preferences(parent, _args, _) do
ActivityMonitor.update_resolver_activity("get_preferences")

Preferences
|> Repo.get_by(user_id: parent.id)
|> then(&{:ok, &1})
end

def create_preferences(args) do
%Preferences{}
|> Preferences.changeset(args)
|> Repo.insert()
end

def update_preferences_by_id(_, args, _) do
ActivityMonitor.update_resolver_activity("update_preferences_by_id")

Repo.get_by(Preferences, user_id: args.user_id)
|> Preferences.changeset(args)
|> Repo.update()
Expand Down
36 changes: 22 additions & 14 deletions lib/gql_preferences_web/resolvers/users.ex
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
defmodule UserPreferencesWeb.Resolvers.Users do
alias UserPreferences.{Repo, User, Preferences}
alias UserPreferences.{Repo, User}
alias UserPreferencesWeb.Resolvers

def get_user_by_id(_parent, args, _res), do: {:ok, Repo.get(User, args.id)}
def get_user_by_id(_parent, args, _res) do
ActivityMonitor.update_resolver_activity("get_user_by_id")
{:ok, Repo.get(User, args.id)}
end

def get_all_users(_, args, _) do
ActivityMonitor.update_resolver_activity("get_all_users")

def get_all_users(_, args, _),
do:
Absinthe.Relay.Connection.from_query(
User,
&Repo.all/1,
args
)
Absinthe.Relay.Connection.from_query(
User,
&Repo.all/1,
args
)
end

def create_user(_parent, args, _res) do
ActivityMonitor.update_resolver_activity("create_user")

{:ok, user} =
%User{}
|> User.changeset(args)
|> Repo.insert()

pref_args = Map.put(args.preferences, :user_id, user.id)

{:ok, pref} =
%Preferences{}
|> Preferences.changeset(pref_args)
|> Repo.insert()
args.preferences
|> Map.put(:user_id, user.id)
|> Resolvers.Preferences.create_preferences()

res = Map.put(user, :preferences, pref)

{:ok, res}
end

def update_user(_, args, _) do
ActivityMonitor.update_resolver_activity("update_user")

Repo.get(User, args.id)
|> User.changeset(args)
|> Repo.update()
Expand Down
54 changes: 32 additions & 22 deletions lib/gql_preferences_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,17 @@ defmodule UserPreferencesWeb.Schema do

alias UserPreferencesWeb.Resolvers

connection(node_type: :user)

node interface do
resolve_type(fn
%UserPreferences.User{}, _ ->
:user

_, _ ->
nil
end)
end

query do
node field do
resolve(fn
%{type: :user, id: local_id}, _ ->
{:ok, UserPreferences.Repo.get(UserPreferences.User, local_id)}

_, _ ->
{:error, "Unknown node"} |> IO.inspect()
end)
end

field :user, :user do
arg(:id, non_null(:id))
resolve(&Resolvers.Users.get_user_by_id/3)
end

field :resolver_hits, :integer do
arg(:key, non_null(:string))
resolve(&Resolvers.Activity.get_activity/3)
end

connection field :users, node_type: :user do
arg(:before, :integer)
arg(:after, :integer)
Expand All @@ -42,6 +25,16 @@ defmodule UserPreferencesWeb.Schema do
arg(:likes_phone_calls, :boolean)
resolve(&Resolvers.Users.get_all_users/3)
end

node field do
resolve(fn
%{type: :user, id: local_id}, _ ->
{:ok, UserPreferences.Repo.get(UserPreferences.User, local_id)}

_, _ ->
{:error, "Unknown node"} |> IO.inspect()
end)
end
end

mutation do
Expand All @@ -67,6 +60,18 @@ defmodule UserPreferencesWeb.Schema do
end
end

connection(node_type: :user)

node interface do
resolve_type(fn
%UserPreferences.User{}, _ ->
:user

_, _ ->
nil
end)
end

node object(:user) do
field :id, :id
field :name, :string
Expand All @@ -84,6 +89,11 @@ defmodule UserPreferencesWeb.Schema do
field :likes_phone_calls, :boolean
end

node object(:resolver_hits) do
field :key, :string
field :hits, :integer
end

input_object :preferences_input do
field :likes_emails, non_null(:boolean)
field :likes_phone_calls, non_null(:boolean)
Expand Down

0 comments on commit 12bf5ce

Please sign in to comment.