forked from zauberware/rails-devise-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scope mutations belong to users under Users module
- Loading branch information
1 parent
26b86cc
commit 3498e55
Showing
27 changed files
with
166 additions
and
150 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Users | ||
# Deletes an user as an admin. | ||
class DeleteUser < Mutations::BaseMutation | ||
description 'Deletes an user as an admin.' | ||
argument :id, ID, required: true | ||
payload_type Boolean | ||
|
||
def resolve(id:) | ||
user = ::User.accessible_by(current_ability).find_by(id: id) | ||
if user.nil? | ||
raise ActiveRecord::RecordNotFound, I18n.t('errors.messages.resource_not_found', resource: ::User.model_name.human) | ||
end | ||
|
||
current_ability.authorize! :destroy, user | ||
return true if user.destroy! | ||
|
||
false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Users | ||
# Updates an existing user as an admin. | ||
class UpdateUser < Mutations::BaseMutation | ||
description 'Updates an existing user as an admin.' | ||
argument :id, ID, required: true | ||
argument :attributes, Types::Users::UserInputType, required: true | ||
payload_type Types::Users::UserType | ||
|
||
def resolve(id:, attributes:) | ||
user = ::User.accessible_by(current_ability).find_by(id: id) | ||
raise ActiveRecord::RecordNotFound, I18n.t('errors.messages.resource_not_found', resource: ::User.model_name.human) if user.nil? | ||
|
||
user.attributes = attributes.to_h | ||
current_ability.authorize! :update, user | ||
return user if user.save! | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Users | ||
# Updates the role for an user as an admin. | ||
class UpdateUserRole < Mutations::BaseMutation | ||
description 'Updates the role for an user as an admin.' | ||
argument :id, ID, required: true | ||
argument :role, String, required: true, description: '"user" or "admin"' | ||
payload_type Boolean | ||
|
||
def resolve(id:, role:) | ||
user = ::User.accessible_by(current_ability).find_by(id: id) | ||
if user.nil? | ||
raise ActiveRecord::RecordNotFound, I18n.t('errors.messages.resource_not_found', resource: ::User.model_name.human) | ||
end | ||
|
||
if %w[admin user].include?(role) | ||
user.role = role | ||
current_ability.authorize! :update, user | ||
user.save! | ||
return true | ||
end | ||
|
||
false | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Resolvers | ||
module Users | ||
# Get current user object | ||
class Me < Resolvers::BaseResolver | ||
type Types::Users::UserType, null: true | ||
description 'Returns the current user' | ||
|
||
def resolve | ||
context[:current_user] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Resolvers | ||
module Users | ||
# Resolver to return a user | ||
class User < Resolvers::BaseResolver | ||
type Types::Users::UserType, null: true | ||
description 'Returns the user for a requested id' | ||
|
||
argument :id, ID, required: true | ||
|
||
def resolve(id:) | ||
::User.accessible_by(current_ability).find_by(id: id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Resolvers | ||
module Users | ||
# Resolver to return a user | ||
class Users < Resolvers::BaseResolver | ||
type Types::Users::UserType.connection_type, null: true | ||
description 'Returns all user for the current user company' | ||
|
||
def resolve(**_args) | ||
::User.accessible_by(current_ability).includes(:company) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
module Users | ||
# Input type for user | ||
class UserInputType < Types::BaseInputObject | ||
description 'Attributes to create a user.' | ||
argument :email, String, 'Email of user', required: true | ||
argument :first_name, String, 'Firstname of user', required: true | ||
argument :last_name, String, 'Lastname of user', required: true | ||
argument :password, String, 'Password of user', required: false | ||
argument :password_confirmation, String, 'Password confirmation', required: false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
module Users | ||
# GraphQL type for a user | ||
class UserType < Types::BaseModel | ||
field :name, String, null: false | ||
field :first_name, String, null: false | ||
field :last_name, String, null: false | ||
field :email, String, null: true | ||
field :role, String, null: false | ||
field :company, Types::Companies::CompanyType, null: false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.