-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GraphQL - Post Lock #875
GraphQL - Post Lock #875
Changes from 25 commits
64295f7
2e27bce
4d0e7e7
6149464
f3fa15b
36b3c97
7aef6ec
203338d
b7611c7
c4b3bad
5f1b240
dee308e
28e9641
72555ae
737f14a
127d606
19c788d
8825662
6d75e95
84ee233
7fde5b4
d46a850
4c9d42c
3c3d98a
c6d1064
915fdee
5e9c031
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Mutations::Post::LockPost < Mutations::Base | ||
argument :input, | ||
Types::Input::Post::Lock, | ||
required: true, | ||
description: 'Lock a Post.', | ||
as: :post | ||
|
||
field :post, Types::Post, null: true | ||
|
||
def load_post(value) | ||
post = ::Post.find(value.id) | ||
post.assign_attributes(value.to_model) | ||
post | ||
end | ||
|
||
def authorized?(post:) | ||
super(post, :update_lock?) | ||
end | ||
|
||
def resolve(post:) | ||
post.save | ||
|
||
if post.errors.any? | ||
Errors::RailsModel.graphql_error(post) | ||
else | ||
{ | ||
post: post | ||
} | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Mutations::Post::UnlockPost < Mutations::Base | ||
argument :input, | ||
Types::Input::Post::Unlock, | ||
required: true, | ||
description: 'Unlock a Post.', | ||
as: :post | ||
|
||
field :post, Types::Post, null: true | ||
|
||
def load_post(value) | ||
post = ::Post.find(value.id) | ||
post.assign_attributes(value.to_model) | ||
post | ||
end | ||
|
||
def authorized?(post:) | ||
super(post, :update_lock?) | ||
end | ||
|
||
def resolve(post:) | ||
post.save | ||
|
||
if post.errors.any? | ||
Errors::RailsModel.graphql_error(post) | ||
else | ||
{ | ||
post: post | ||
} | ||
end | ||
Comment on lines
+21
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW I added an We can change it once both are merged |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Types::Enum::LockedReason < Types::Enum::Base | ||
value 'SPAM', value: 'spam' | ||
toyhammered marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value 'TOO_HEATED', value: 'too_heated' | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,8 @@ def self.default_graphql_name | |
def to_model | ||
to_h | ||
end | ||
|
||
def current_user | ||
User.current | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Types::Input::Post::Lock < Types::Input::Base | ||
argument :id, ID, required: true | ||
argument :locked_reason, Types::Enum::LockedReason, required: true | ||
|
||
def to_model | ||
to_h.merge(locked_at: DateTime.current, locked_by: current_user) | ||
end | ||
Comment on lines
+5
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels way too magical for me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Types::Input::Post::Unlock < Types::Input::Base | ||
argument :id, ID, required: true | ||
|
||
def to_model | ||
to_h.merge(locked_at: nil, locked_by: nil, locked_reason: nil) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Types::Mutations::PostMutation < Types::BaseObject | ||
field :lock, | ||
mutation: ::Mutations::Post::LockPost, | ||
description: 'Lock a Post.' | ||
|
||
field :unlock, | ||
mutation: ::Mutations::Post::UnlockPost, | ||
description: 'Unlock a Post.' | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AddLockUnlockToPost < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :posts, :locked_by_id, :integer | ||
add_column :posts, :locked_at, :datetime | ||
add_column :posts, :locked_reason, :integer | ||
add_index :posts, :locked_by_id | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm distinctly not a fan of this thing where you get the changes from some other file and then shove them in over here... It seems like indirection without purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny enough this is how it was originally done (by Matt). I liked it so I kinda just started using it also lol.