Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/graphql/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ def current_authentication
context[:current_authentication]
end

def create_message_error(message)
Sagittarius::Graphql::ErrorMessageContainer.new(message: message)
def create_error(code, message)
ErrorCode.validate_error_code!(code)

Sagittarius::Graphql::ErrorContainer.new(
code,
[{ message: message }]
)
end
end
end
10 changes: 8 additions & 2 deletions app/graphql/mutations/namespaces/members/assign_roles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ def resolve(member_id:, role_ids:)
member = SagittariusSchema.object_from_id(member_id)
roles = role_ids.map { |id| SagittariusSchema.object_from_id(id) }

return { namespace_member_roles: nil, errors: [create_message_error('Invalid member')] } if member.nil?
return { namespace_member_roles: nil, errors: [create_message_error('Invalid role')] } if roles.any?(&:nil?)
if member.nil?
return { namespace_member_roles: nil,
errors: [create_error(:namespace_member_not_found, 'Invalid member')] }
end
if roles.any?(&:nil?)
return { namespace_member_roles: nil,
errors: [create_error(:namespace_role_not_found, 'Invalid role')] }
end

::Namespaces::Members::AssignRolesService.new(
current_authentication,
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/namespaces/members/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def resolve(namespace_member_id:)

if namespace_member.nil?
return { namespace_member: nil,
errors: [create_message_error('Invalid member')] }
errors: [create_error(:namespace_member_not_found, 'Invalid member')] }
end

::Namespaces::Members::DeleteService.new(
Expand Down
7 changes: 5 additions & 2 deletions app/graphql/mutations/namespaces/members/invite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ def resolve(namespace_id:, user_id:)
namespace = SagittariusSchema.object_from_id(namespace_id)
user = SagittariusSchema.object_from_id(user_id)

return { namespace_member: nil, errors: [create_message_error('Invalid namespace')] } if namespace.nil?
return { namespace_member: nil, errors: [create_message_error('Invalid user')] } if user.nil?
if namespace.nil?
return { namespace_member: nil,
errors: [create_error(:namespace_not_found, 'Invalid namespace')] }
end
return { namespace_member: nil, errors: [create_error(:user_not_found, 'Invalid user')] } if user.nil?

::Namespaces::Members::InviteService.new(
current_authentication,
Expand Down
10 changes: 8 additions & 2 deletions app/graphql/mutations/namespaces/projects/assign_runtimes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ def resolve(namespace_project_id:, runtime_ids:)
namespace_project = SagittariusSchema.object_from_id(namespace_project_id)
runtimes = runtime_ids.map { |runtime_id| SagittariusSchema.object_from_id(runtime_id) }

return { namespace_project: nil, errors: [create_message_error('Invalid project')] } if namespace_project.nil?
return { namespace_project: nil, errors: [create_message_error('Invalid runtime')] } if runtimes.any?(&:nil?)
if namespace_project.nil?
return { namespace_project: nil,
errors: [create_error(:project_not_found, 'Invalid project')] }
end
if runtimes.any?(&:nil?)
return { namespace_project: nil,
errors: [create_error(:runtime_not_found, 'Invalid runtime')] }
end

::Namespaces::Projects::AssignRuntimesService.new(
current_authentication,
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/namespaces/projects/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def resolve(namespace_id:, **params)

if namespace.nil?
return { organization_project: nil,
errors: [create_message_error('Invalid namespace')] }
errors: [create_error(:namespace_not_found, 'Invalid namespace')] }
end

::Namespaces::Projects::CreateService.new(
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/namespaces/projects/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def resolve(namespace_project_id:)

if project.nil?
return { organization_project: nil,
errors: [create_message_error('Invalid project')] }
errors: [create_error(:namespace_project_not_found, 'Invalid project')] }
end

::Namespaces::Projects::DeleteService.new(
Expand Down
21 changes: 12 additions & 9 deletions app/graphql/mutations/namespaces/projects/flows/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ class Create < BaseMutation
def resolve(project_id:, flow:, **_params)
project = SagittariusSchema.object_from_id(project_id)

return error('Invalid project id') if project.nil?
if project.nil?
return {
flow: nil,
errors: [create_error(:namespace_project_not_found, 'Invalid project id')],
}
end

flow_type = SagittariusSchema.object_from_id(flow.type)
return error('Invalid flow type id') if flow_type.nil?
if flow_type.nil?
return {
flow: nil,
errors: [create_error(:flow_type_not_found, 'Invalid flow type id')],
}
end

::Namespaces::Projects::Flows::CreateService.new(
current_authentication,
Expand All @@ -31,13 +41,6 @@ def resolve(project_id:, flow:, **_params)
name: flow.name
).execute.to_mutation_response(success_key: :flow)
end

def error(message)
{
flow: nil,
errors: [create_message_error(message)],
}
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/namespaces/projects/flows/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def resolve(flow_id:)

if flow.nil?
return { flow: nil,
errors: [create_message_error('Invalid flow')] }
errors: [create_error(:flow_not_found, 'Invalid flow')] }
end

::Namespaces::Projects::Flows::DeleteService.new(
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/namespaces/projects/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def resolve(namespace_project_id:, **params)

if project.nil?
return { organization_project: nil,
errors: [create_message_error('Invalid project')] }
errors: [create_error(:project_not_found, 'Invalid project')] }
end

params[:primary_runtime_id] = params[:primary_runtime_id]&.model_id if params.key?(:primary_runtime_id)
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/namespaces/roles/assign_abilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AssignAbilities < BaseMutation
def resolve(role_id:, abilities:)
role = SagittariusSchema.object_from_id(role_id)

return { abilities: nil, errors: [create_message_error('Invalid role')] } if role.nil?
return { abilities: nil, errors: [create_error(:namespace_role_not_found, 'Invalid role')] } if role.nil?

::Namespaces::Roles::AssignAbilitiesService.new(
current_authentication,
Expand Down
8 changes: 6 additions & 2 deletions app/graphql/mutations/namespaces/roles/assign_projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ def resolve(role_id:, project_ids:)
role = SagittariusSchema.object_from_id(role_id)
projects = project_ids.map { |id| SagittariusSchema.object_from_id(id) }

return { projects: nil, errors: [create_message_error('Invalid role')] } if role.nil?
return { projects: nil, errors: [create_message_error('Invalid project')] } if projects.any?(&:nil?)
return { projects: nil, errors: [create_error(:namespace_role_not_found, 'Invalid role')] } if role.nil?

if projects.any?(&:nil?)
return { projects: nil,
errors: [create_error(:namespace_project_not_found, 'Invalid project')] }
end

::Namespaces::Roles::AssignProjectsService.new(
current_authentication,
Expand Down
5 changes: 4 additions & 1 deletion app/graphql/mutations/namespaces/roles/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class Create < BaseMutation
def resolve(namespace_id:, **params)
namespace = SagittariusSchema.object_from_id(namespace_id)

return { namespace_role: nil, errors: [create_message_error('Invalid namespace')] } if namespace.nil?
if namespace.nil?
return { namespace_role: nil,
errors: [create_error(:namespace_not_found, 'Invalid namespace')] }
end

::Namespaces::Roles::CreateService.new(
current_authentication,
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/namespaces/roles/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def resolve(namespace_role_id:)

if namespace_role.nil?
return { namespace_role: nil,
errors: [create_message_error('Invalid namespace role')] }
errors: [create_error(:namespace_role_not_found, 'Invalid namespace role')] }
end

::Namespaces::Roles::DeleteService.new(
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/namespaces/roles/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def resolve(namespace_role_id:, **params)

if namespace_role.nil?
return { namespace_role: nil,
errors: [create_message_error('Invalid namespace role')] }
errors: [create_error(:namespace_role_not_found, 'Invalid namespace role')] }
end

::Namespaces::Roles::UpdateService.new(
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/organizations/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def resolve(organization_id:)

if organization.nil?
return { organization_role: nil,
errors: [create_message_error('Invalid organization')] }
errors: [create_error(:organization_not_found, 'Invalid organization')] }
end

response = ::Organizations::DeleteService.new(
Expand Down
5 changes: 4 additions & 1 deletion app/graphql/mutations/organizations/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class Update < BaseMutation
def resolve(organization_id:, **params)
organization = SagittariusSchema.object_from_id(organization_id)

return { organization: nil, errors: [create_message_error('Invalid organization')] } if organization.nil?
if organization.nil?
return { organization: nil,
errors: [create_error(:organization_not_found, 'Invalid organization')] }
end

::Organizations::UpdateService.new(
current_authentication,
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/runtimes/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def resolve(name:, namespace_id: nil, description: '')

if namespace.nil? && namespace_id.present?
return { runtime: nil,
errors: [create_message_error('Invalid namespace')] }
errors: [create_error(:namespace_not_found, 'Invalid namespace')] }
end

::Runtimes::CreateService.new(
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/runtimes/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def resolve(runtime_id:)

if runtime.nil?
return { runtime: nil,
errors: [create_message_error('Invalid runtime')] }
errors: [create_error(:runtime_not_found, 'Invalid runtime')] }
end

::Runtimes::DeleteService.new(
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/runtimes/rotate_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def resolve(runtime_id:)

if runtime.nil?
return { runtime: nil,
errors: [create_message_error('Invalid runtime')] }
errors: [create_error(:runtime_not_found, 'Invalid runtime')] }
end

::Runtimes::RotateTokenService.new(
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/runtimes/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Update < BaseMutation
def resolve(runtime_id:, **params)
runtime = SagittariusSchema.object_from_id(runtime_id)

return { runtime: nil, errors: [create_message_error('Invalid runtime')] } if runtime.nil?
return { runtime: nil, errors: [create_error(:runtime_not_found, 'Invalid runtime')] } if runtime.nil?

::Runtimes::UpdateService.new(
current_authentication,
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/users/identity/unlink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def resolve(identity_id:)

if user_identity.nil?
return { user_identity: nil,
errors: [create_message_error('Invalid identity')] }
errors: [create_error(:identity_not_found, 'Invalid identity')] }
end

::Users::Identity::UnlinkService.new(
Expand Down
5 changes: 4 additions & 1 deletion app/graphql/mutations/users/logout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class Logout < BaseMutation
def resolve(user_session_id:)
user_session = SagittariusSchema.object_from_id(user_session_id)

return { user_session: nil, errors: [create_message_error('Invalid user session')] } if user_session.nil?
if user_session.nil?
return { user_session: nil,
errors: [create_error(:user_session_not_found, 'Invalid user session')] }
end

::Users::LogoutService.new(
current_authentication,
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/users/password_reset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PasswordReset < BaseMutation
def resolve(reset_token:, new_password:, new_password_confirmation:)
if new_password != new_password_confirmation
return { user: nil,
errors: [create_message_error('Invalid password repeat')] }
errors: [create_error(:invalid_password_repeat, 'Invalid password repeat')] }
end

message = ::Users::PasswordResetService.new(
Expand Down
4 changes: 3 additions & 1 deletion app/graphql/mutations/users/register.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Register < BaseMutation
argument :username, String, required: true, description: 'Username of the user'

def resolve(username:, email:, password:, password_repeat:)
return { user: nil, errors: [create_message_error('Invalid password repeat')] } if password != password_repeat
if password != password_repeat
return { user: nil, errors: [create_error(:invalid_password_repeat, 'Invalid password repeat')] }
end

response = ::Users::RegisterService.new(
username,
Expand Down
4 changes: 2 additions & 2 deletions app/graphql/mutations/users/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class Update < BaseMutation
def resolve(user_id:, mfa: nil, **params)
user = SagittariusSchema.object_from_id(user_id)

return { user: nil, errors: [create_message_error('Invalid user')] } if user.nil?
return { user: nil, errors: [create_error(:user_not_found, 'Invalid user with provided id')] } if user.nil?

if params[:password] != params.delete(:password_repeat)
return { user: nil, errors: [create_message_error('Invalid password repeat')] }
return { user: nil, errors: [create_error(:invalid_password_repeat, 'Invalid password repeat')] }
end

::Users::UpdateService.new(
Expand Down
27 changes: 27 additions & 0 deletions app/graphql/types/errors/detailed_error_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Types
module Errors
# rubocop:disable GraphQL/GraphqlName -- we don't want the module prefix
class DetailedErrorType < Types::BaseUnion
graphql_name 'DetailedError'
# rubocop:enable GraphQL/GraphqlName
description 'Represents a detailed error with either a message or an active model error'
possible_types Types::Errors::ActiveModelErrorType, Types::Errors::MessageErrorType,
Types::Errors::FlowValidationErrorType

def self.resolve_type(object, _ctx)
case object
when Namespaces::Projects::Flows::Validation::ValidationResult
Types::Errors::FlowValidationErrorType
when ActiveModel::Error
Types::Errors::ActiveModelErrorType
when Hash
Types::Errors::MessageErrorType
else
raise 'Unsupported DetailedErrorType'
end
end
end
end
end
14 changes: 0 additions & 14 deletions app/graphql/types/errors/error_code_type.rb

This file was deleted.

17 changes: 3 additions & 14 deletions app/graphql/types/errors/error_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@
module Types
module Errors
# rubocop:disable GraphQL/GraphqlName -- we don't want the module prefix
class ErrorType < BaseUnion
class ErrorType < Types::BaseObject
graphql_name 'Error'
# rubocop:enable GraphQL/GraphqlName
description 'Objects that can present an error'
possible_types Errors::ActiveModelErrorType, Errors::MessageErrorType, Errors::ErrorCodeType

def self.resolve_type(object, _ctx)
case object
when ActiveModel::Error
Errors::ActiveModelErrorType
when Sagittarius::Graphql::ErrorMessageContainer
Errors::MessageErrorType
when Sagittarius::Graphql::ServiceResponseErrorContainer
Errors::ErrorCodeType
else
raise 'Unsupported ErrorType'
end
end
field :details, [Errors::DetailedErrorType], null: true, description: 'Detailed validation errors if applicable'
field :error_code, ErrorCodeEnum, null: false, description: 'The code representing the error type'
end
end
end
Loading
Loading