Skip to content

Commit

Permalink
✨ Expose like ratings through GQL
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvy committed Nov 27, 2018
1 parent 67644f1 commit 9f7cd79
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/dojinlist_web/mutations/like.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule DojinlistWeb.Mutations.Like do
use Absinthe.Schema.Notation

alias Dojinlist.{
Repo,
Likes
}

object :like_mutations do
field :like_rating, type: :rating_like do
arg(:rating_id, non_null(:id))

middleware(DojinlistWeb.Middlewares.Authorization)
middleware(Absinthe.Relay.Node.ParseIDs, rating_id: :rating)

resolve(&like_rating/2)
end
end

def like_rating(%{rating_id: rating_id}, %{context: %{current_user: user}}) do
case Likes.like_rating(user.id, rating_id) do
{:ok, like} ->
loaded_like = Repo.preload(like, [:user, :rating])
{:ok, loaded_like}

{:error, _} ->
{:error, "Could not like rating"}
end
end
end
2 changes: 2 additions & 0 deletions lib/dojinlist_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule DojinlistWeb.Schema do
import_types(DojinlistWeb.Mutations.Permission)
import_types(DojinlistWeb.Mutations.Me)
import_types(DojinlistWeb.Mutations.Blog)
import_types(DojinlistWeb.Mutations.Like)

query do
connection field :albums, node_type: :album do
Expand Down Expand Up @@ -114,6 +115,7 @@ defmodule DojinlistWeb.Schema do
import_fields(:permission_mutations)
import_fields(:me_mutations)
import_fields(:blog_mutations)
import_fields(:like_mutations)
end

node interface do
Expand Down
5 changes: 5 additions & 0 deletions lib/dojinlist_web/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ defmodule DojinlistWeb.Types do
object :register_response do
field :user, :user
end

object :rating_like do
field :user_id, :user
field :rating_id, :rating
end
end

0 comments on commit 9f7cd79

Please sign in to comment.