2018-07-10 10:19:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
class BaseMutation < GraphQL::Schema::RelayClassicMutation
|
2021-03-18 02:11:52 -04:00
|
|
|
include Gitlab::Graphql::Authorize::AuthorizeResource
|
2019-06-21 00:45:27 -04:00
|
|
|
prepend Gitlab::Graphql::CopyFieldDescription
|
2020-10-01 05:09:54 -04:00
|
|
|
prepend ::Gitlab::Graphql::GlobalIDCompatibility
|
2019-06-21 00:45:27 -04:00
|
|
|
|
2019-09-20 14:06:35 -04:00
|
|
|
ERROR_MESSAGE = 'You cannot perform write operations on a read-only instance'
|
|
|
|
|
2020-07-07 08:09:16 -04:00
|
|
|
field_class ::Types::BaseField
|
2021-03-18 14:09:09 -04:00
|
|
|
argument_class ::Types::BaseArgument
|
2020-07-07 08:09:16 -04:00
|
|
|
|
2021-07-23 11:09:21 -04:00
|
|
|
field :errors, [GraphQL::Types::String],
|
2018-07-10 10:19:45 -04:00
|
|
|
null: false,
|
2020-05-20 08:07:52 -04:00
|
|
|
description: 'Errors encountered during execution of the mutation.'
|
2018-07-10 10:19:45 -04:00
|
|
|
|
|
|
|
def current_user
|
|
|
|
context[:current_user]
|
|
|
|
end
|
2019-06-21 00:45:27 -04:00
|
|
|
|
2020-08-20 08:10:27 -04:00
|
|
|
def api_user?
|
|
|
|
context[:is_sessionless_user]
|
|
|
|
end
|
|
|
|
|
2019-06-21 00:45:27 -04:00
|
|
|
# Returns Array of errors on an ActiveRecord object
|
|
|
|
def errors_on_object(record)
|
|
|
|
record.errors.full_messages
|
|
|
|
end
|
2019-09-20 14:06:35 -04:00
|
|
|
|
|
|
|
def ready?(**args)
|
2021-03-30 14:10:47 -04:00
|
|
|
raise_resource_not_available_error! ERROR_MESSAGE if Gitlab::Database.read_only?
|
|
|
|
|
|
|
|
true
|
2019-09-20 14:06:35 -04:00
|
|
|
end
|
2021-03-18 02:11:52 -04:00
|
|
|
|
|
|
|
def load_application_object(argument, lookup_as_type, id, context)
|
|
|
|
::Gitlab::Graphql::Lazy.new { super }.catch(::GraphQL::UnauthorizedError) do |e|
|
|
|
|
Gitlab::ErrorTracking.track_exception(e)
|
|
|
|
# The default behaviour is to abort processing and return nil for the
|
|
|
|
# entire mutation field, but not set any top-level errors. We prefer to
|
|
|
|
# at least say that something went wrong.
|
|
|
|
raise_resource_not_available_error!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-28 11:09:35 -04:00
|
|
|
def self.authorizes_object?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2021-03-18 02:11:52 -04:00
|
|
|
def self.authorized?(object, context)
|
2021-04-28 11:09:35 -04:00
|
|
|
auth = ::Gitlab::Graphql::Authorize::ObjectAuthorization.new(:execute_graphql_mutation, :api)
|
|
|
|
|
|
|
|
return true if auth.ok?(:global, context[:current_user],
|
|
|
|
scope_validator: context[:scope_validator])
|
|
|
|
|
|
|
|
# in our mutations we raise, rather than returning a null value.
|
|
|
|
raise_resource_not_available_error!
|
2021-03-18 02:11:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# See: AuthorizeResource#authorized_resource?
|
|
|
|
def self.authorization
|
|
|
|
@authorization ||= ::Gitlab::Graphql::Authorize::ObjectAuthorization.new(authorize)
|
|
|
|
end
|
2018-07-10 10:19:45 -04:00
|
|
|
end
|
|
|
|
end
|