Skip to content

Commit

Permalink
Auth: associate Links to Users
Browse files Browse the repository at this point in the history
  • Loading branch information
ztratify committed Dec 1, 2020
1 parent d321a7d commit 8a5afd0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/graphql/mutations/create_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def resolve(description: nil, url: nil)
Link.create!(
description: description,
url: url,
user: context[:current_user]
)
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/graphql/types/link_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ class LinkType < Types::BaseObject
field :id, ID, null: false
field :url, String, null: false
field :description, String, null: false
# `posted_by` is automatically camelcased as `postedBy`
# field can be nil, because we added users relationship later
# "method" option remaps field to an attribute of Link model
field :posted_by, UserType, null: true, method: :user
end
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/20201201040432_add_user_id_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddUserIdLink < ActiveRecord::Migration[6.0]
def change
change_table :links do |t|
t.references :user, foreign_key: true
end
end
end
5 changes: 4 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: 2020_11_30_014852) do
ActiveRecord::Schema.define(version: 2020_12_01_040432) do

create_table "links", force: :cascade do |t|
t.string "url"
t.text "description"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, 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 All @@ -27,4 +29,5 @@
t.datetime "updated_at", precision: 6, null: false
end

add_foreign_key "links", "users"
end

0 comments on commit 8a5afd0

Please sign in to comment.