-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33d771b
commit b965381
Showing
2 changed files
with
106 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule UserPreferencesWeb.Resolvers.Users do | ||
alias UserPreferences.{Repo, User, Preferences} | ||
|
||
def get_user_by_id(_parent, args, _res), do: {:ok, Repo.get(User, args.id)} | ||
|
||
def get_all_users(_, args, _), | ||
do: | ||
Absinthe.Relay.Connection.from_query( | ||
User, | ||
&Repo.all/1, | ||
args | ||
) | ||
|
||
def create_user(_parent, args, _res) do | ||
{: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() | ||
|
||
res = Map.put(user, :preferences, pref) | ||
|
||
{:ok, res} | ||
end | ||
|
||
def update_user(_, args, _) do | ||
Repo.get(User, args.id) | ||
|> User.changeset(args) | ||
|> Repo.update() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,91 @@ | ||
defmodule UserPreferencesWeb.Schema do | ||
use Absinthe.Schema | ||
alias UserPreferences.{User, Preference, Repo} | ||
use Absinthe.Relay.Schema, :modern | ||
use Absinthe.Relay.Schema.Notation, :modern | ||
|
||
alias UserPreferencesWeb.Resolvers | ||
|
||
connection(node_type: :user) | ||
|
||
node interface do | ||
resolve_type(fn | ||
%UserPreferences.User{}, _ -> | ||
:user | ||
|
||
_, _ -> | ||
nil | ||
end) | ||
end | ||
|
||
query do | ||
field :user, list_of(:user) do | ||
resolve fn _, _, _ -> | ||
{:ok, Repo.all(User)} | ||
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 | ||
|
||
field :user, :user do | ||
arg(:id, non_null(:id)) | ||
resolve(&Resolvers.Users.get_user_by_id/3) | ||
end | ||
|
||
field :preference, list_of(:preference) do | ||
resolve fn _, _, _ -> | ||
{:ok, Repo.all(Preference)} | ||
end | ||
connection field :users, node_type: :user do | ||
arg(:before, :integer) | ||
arg(:after, :integer) | ||
arg(:first, :integer) | ||
arg(:name, :string) | ||
arg(:likes_emails, :boolean) | ||
arg(:likes_phone_calls, :boolean) | ||
resolve(&Resolvers.Users.get_all_users/3) | ||
end | ||
end | ||
|
||
mutation do | ||
field :create_user, type: :user do | ||
arg | ||
field :create_user, :user do | ||
arg(:name, non_null(:string)) | ||
arg(:email, non_null(:string)) | ||
arg(:preferences, :preferences_input) | ||
|
||
resolve(&Resolvers.Users.create_user/3) | ||
end | ||
|
||
field :update_user, :user do | ||
arg(:id, non_null(:id)) | ||
arg(:name, non_null(:string)) | ||
resolve(&Resolvers.Users.update_user/3) | ||
end | ||
|
||
field :update_user_preferences, :preferences do | ||
arg(:user_id, non_null(:id)) | ||
arg(:likes_emails, :boolean) | ||
arg(:likes_phone_calls, :boolean) | ||
resolve(&Resolvers.Preferences.update_preferences_by_id/3) | ||
end | ||
end | ||
|
||
object :user do | ||
node object(:user) do | ||
field :id, :id | ||
field :name, :string | ||
field :email, :string | ||
|
||
field :preferences, :preferences do | ||
arg(:user_id, :id) | ||
resolve(&Resolvers.Preferences.get_preferences/3) | ||
end | ||
end | ||
|
||
object :preference do | ||
node object(:preferences) do | ||
field :user_id, :id | ||
field :likes_emails, :boolean | ||
field :likes_phone_calls, :boolean | ||
end | ||
|
||
input_object :preferences_input do | ||
field :likes_emails, non_null(:boolean) | ||
field :likes_phone_calls, non_null(:boolean) | ||
end | ||
end |