Skip to content

Commit

Permalink
Set link user to current_user on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaspar committed Aug 3, 2017
1 parent 879fe4f commit 22309ca
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
11 changes: 6 additions & 5 deletions app/graphql/resolvers/create_link.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
class Resolvers::CreateLink < GraphQL::Function
# arguments passed as "args"
# Arguments passed as "args"
argument :description, !types.String
argument :url, !types.String

# return type from the mutation
# Return type from the mutation
type Types::LinkType

# the mutation method
# The mutation method
# _obj - is the parent object, which in this case is nil
# args - are the arguments passed
# _ctx - is the GraphQL context
def call(_obj, args, _ctx)
# ctx - is the GraphQL context
def call(_obj, args, ctx)
Link.create!(
description: args[:description],
url: args[:url],
user: ctx[:current_user]
)
end
end
1 change: 1 addition & 0 deletions app/graphql/types/link_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
field :id, !types.ID
field :url, !types.String
field :description, !types.String
field :postedBy, -> { Types::UserType }, property: :user
end
1 change: 1 addition & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Link < ApplicationRecord
belongs_to :user
end
7 changes: 7 additions & 0 deletions db/migrate/20170803015541_add_user_id_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddUserIdLink < ActiveRecord::Migration[5.1]
def change
change_table :links do |t|
t.references :user, foreign_key: true
end
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170801015006) do
ActiveRecord::Schema.define(version: 20170803015541) do

create_table "links", force: :cascade do |t|
t.string "url"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["user_id"], name: "index_links_on_user_id"
end

create_table "users", force: :cascade do |t|
Expand Down
11 changes: 10 additions & 1 deletion test/graphql/resolvers/create_link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

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

setup do
@user = User.create!(
name: 'test',
email: 'email@example.com',
password: 'something'
)
end

test 'creating new link' do
Expand All @@ -14,5 +22,6 @@ def perform(args = {})
assert link.persisted?
assert_equal link.description, 'description'
assert_equal link.url, 'http://example.com'
assert_equal link.user, @user
end
end

0 comments on commit 22309ca

Please sign in to comment.