Skip to content
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

Add new mutations and refactor old ones #1465

Draft
wants to merge 14 commits into
base: the-future
Choose a base branch
from

Conversation

shomykohai
Copy link
Contributor

What

Refactor old Mutations on top of FancyMutation.
Add new mutations to GraphQL:

  • Posts
    • Delete
    • Edit
    • Follow
    • Unfollow
    • Like
    • Unlike
  • Comments
    • Create
    • Delete
    • Edit
    • Like
    • Unlike

Add new Types:

  • Embed
  • Attachment (Uploads)
  • PostFollow
  • PostLike
  • CommentLike

Add AttachmentsLoader

Removed old Manga & Anime mutations in favor of WikiSubmissions

Why

Checklist

  • All files pass Rubocop
  • Any complex logic is commented
  • Any new systems have thorough documentation
  • Any user-facing changes are behind a feature flag (or: explain why they can't be)
  • All the tests pass
  • Tests have been added to cover the new code


def ready?(id:, **)
authenticate!
@post = Post.find_by(id:)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpected token tRPAREN

@@ -0,0 +1,4 @@
class Loaders::AttachmentsLoader < GraphQL::FancyLoader
from Upload
sort :upload_order
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Use 2 (not 1) spaces for indentation. (https://rubystyle.guide#spaces-indentation)

@@ -0,0 +1,4 @@
class Loaders::AttachmentsLoader < GraphQL::FancyLoader
from Upload
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Use 2 (not 1) spaces for indentation. (https://rubystyle.guide#spaces-indentation)

@@ -0,0 +1,4 @@
class Loaders::AttachmentsLoader < GraphQL::FancyLoader
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Missing frozen string literal comment.

field :like,
mutation: Mutations::Comment::Like,
description: 'Like a Comment.'

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

field :like,
mutation: Mutations::Post::Like,
description: 'Like a Post.'

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

field :follow,
mutation: Mutations::Post::Follow,
description: 'Follow a Post.'

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

field :delete,
mutation: Mutations::Post::Delete,
description: 'Delete a Post.'

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)


{ post: post }
def resolve(**)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Expected 1 empty line between method definitions; found 3. (https://rubystyle.guide#empty-lines-between-methods)

end

def resolve(post:)
post.save!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

}]
# Authorize it with the policy
authorize!(@post, :create?)
true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

elsif !input[:target_group_id].nil?
return errors << Types::Errors::NotFound.build(path: %w[input
target_group_id]) if Group.find_by(id: input[:target_group_id]).nil?
else
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Redundant else-clause.

target_user_id]) if User.find_by(id: input[:target_user_id]).nil?
elsif !input[:target_group_id].nil?
return errors << Types::Errors::NotFound.build(path: %w[input
target_group_id]) if Group.find_by(id: input[:target_group_id]).nil?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Align the elements of an array literal if they span more than one line. (https://rubystyle.guide#no-double-indent)

return errors << Types::Errors::NotFound.build(path: %w[input
target_user_id]) if User.find_by(id: input[:target_user_id]).nil?
elsif !input[:target_group_id].nil?
return errors << Types::Errors::NotFound.build(path: %w[input
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Favor a normal if-statement over a modifier clause in a multiline statement. (https://rubystyle.guide#no-multiline-if-modifiers)
[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

# And finally check if the post has a target and if they're valid
if !input[:target_user_id].nil?
return errors << Types::Errors::NotFound.build(path: %w[input
target_user_id]) if User.find_by(id: input[:target_user_id]).nil?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Align the elements of an array literal if they span more than one line. (https://rubystyle.guide#no-double-indent)

when 'Chapter' then @unit = Chapter.find_by(id: input[:spoiled_unit_id], media_id: input[:media_id])
else @unit = nil
end
return errors << Types::Errors::NotFound.build(path: %w[input
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Favor a normal if-statement over a modifier clause in a multiline statement. (https://rubystyle.guide#no-multiline-if-modifiers)
[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

unless input[:spoiled_unit_id].nil? ^ input[:spoiled_unit_type].nil?
case input[:spoiled_unit_type]
when 'Episode' then @unit = Episode.find_by(id: input[:spoiled_unit_id], media_id: input[:media_id])
when 'Chapter' then @unit = Chapter.find_by(id: input[:spoiled_unit_id], media_id: input[:media_id])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Indent when as deep as case. (https://rubystyle.guide#indent-when-to-case)
[Correctable] Line is too long. [108/100] (https://rubystyle.guide#max-line-length)

# Check if the spoiled unit is valid
unless input[:spoiled_unit_id].nil? ^ input[:spoiled_unit_type].nil?
case input[:spoiled_unit_type]
when 'Episode' then @unit = Episode.find_by(id: input[:spoiled_unit_id], media_id: input[:media_id])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Indent when as deep as case. (https://rubystyle.guide#indent-when-to-case)
[Correctable] Line is too long. [108/100] (https://rubystyle.guide#max-line-length)

end

# Check if the spoiled unit is valid
unless input[:spoiled_unit_id].nil? ^ input[:spoiled_unit_type].nil?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Do not use unless with else. Rewrite these with the positive case first. (https://rubystyle.guide#no-else-with-unless)
[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

media_id media_type]) if (@media.nil? && !input[:media_id].nil?)
else
return errors << Types::Errors::Validation.build(path: %w[input media_id media_type],
message: 'You have to provide both mediaId and mediaType.')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)

@shomykohai
Copy link
Contributor Author

shomykohai commented Nov 13, 2023

There should also be a validation in app/models/post.rb to first check if the unit id belongs to the media so we avoid situations like this
Screenshot from 2023-09-30 19-21-15

Unfortunately I wasn't able to make it work, so if someone could help me with it, it would be really appreciated :D

@shomykohai shomykohai changed the title Refactor mutations Add new mutations and refactor old ones Nov 13, 2023
@NuckChorris
Copy link
Member

I don't think there's any out-of-the-box Rails validators which can do that, but a custom validation could do it pretty easily, like:

validate :unit_in_media

def unit_in_media
  return if unit.blank?

  unless unit.media_type == media_type && unit.media_id == media_id
    errors.add(:unit, "must be from the tagged media")
  end
end


field :height, Integer,
null: true
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Final newline missing. (https://rubystyle.guide#newline-eof)

null: true

field :height, Integer,
null: true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Use one level of indentation for arguments following the first line of a multi-line method call. (https://rubystyle.guide#no-double-indent)

field :width, Integer,
null: true

field :height, Integer,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Use 2 (not 1) spaces for indentation. (https://rubystyle.guide#spaces-indentation)

null: false

field :width, Integer,
null: true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Use one level of indentation for arguments following the first line of a multi-line method call. (https://rubystyle.guide#no-double-indent)

field :type, String,
null: false

field :width, Integer,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Use 2 (not 1) spaces for indentation. (https://rubystyle.guide#spaces-indentation)

end

# Find the post
@post = Post.find_by(id:)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpected token tRPAREN

field :url, String,
null: true,
description: 'The url of the embedded website.'
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Final newline missing. (https://rubystyle.guide#newline-eof)


field :url, String,
null: true,
description: 'The url of the embedded website.'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)

description: 'The name of the embedded website.'

field :url, String,
null: true,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Use one level of indentation for arguments following the first line of a multi-line method call. (https://rubystyle.guide#no-double-indent)

null: false,
description: 'The name of the embedded website.'

field :url, String,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Correctable] Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Use 2 (not 1) spaces for indentation. (https://rubystyle.guide#spaces-indentation)
[Correctable] Inconsistent indentation detected. (https://rubystyle.guide#spaces-indentation, https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants