diff --git a/lib/gql_preferences_web/resolvers/users.ex b/lib/gql_preferences_web/resolvers/users.ex new file mode 100644 index 0000000..64428f3 --- /dev/null +++ b/lib/gql_preferences_web/resolvers/users.ex @@ -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 diff --git a/lib/gql_preferences_web/schema.ex b/lib/gql_preferences_web/schema.ex index 7227944..826162d 100644 --- a/lib/gql_preferences_web/schema.ex +++ b/lib/gql_preferences_web/schema.ex @@ -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