Skip to content

Commit

Permalink
Add VoteType & CreateVote mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaspar committed Aug 5, 2017
1 parent c3f7451 commit ff482bb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/graphql/resolvers/create_vote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Resolvers::CreateVote < GraphQL::Function
argument :linkId, !types.ID

type Types::VoteType

def call(_obj, args, ctx)
Vote.create!(
user: ctx[:current_user],
link: Link.find_by(id: args[:linkId]),
)
end
end
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

field :createLink, function: Resolvers::CreateLink.new
field :createUser, function: Resolvers::CreateUser.new
field :createVote, function: Resolvers::CreateVote.new
field :signInUser, function: Resolvers::SignInUser.new
end
7 changes: 7 additions & 0 deletions app/graphql/types/vote_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Types::VoteType = GraphQL::ObjectType.define do
name 'Vote'

field :id, !types.ID
field :link, -> { !Types::LinkType }
field :user, -> { !Types::UserType }
end
30 changes: 30 additions & 0 deletions test/graphql/resolvers/create_vote_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'test_helper'

class Resolvers::CreateVoteTest < ActiveSupport::TestCase
def perform(args = {})
Resolvers::CreateVote.new.call(nil, args, { current_user: @user })
end

setup do
@user = User.create!(
name: 'test',
email: 'email@example.com',
password: 'something'
)
@link = Link.create!(
url: 'http://google.com',
description: 'description',
user: @user,
)
end

test 'creating new vote' do
vote = perform(
linkId: @link.id,
)

assert vote.persisted?
assert_equal vote.link, @link
assert_equal vote.user, @user
end
end

0 comments on commit ff482bb

Please sign in to comment.