2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
class BaseMutation < GraphQL::Schema::RelayClassicMutation
|
2021-04-29 21:17:54 +05:30
|
|
|
include Gitlab::Graphql::Authorize::AuthorizeResource
|
2019-09-30 21:07:59 +05:30
|
|
|
prepend Gitlab::Graphql::CopyFieldDescription
|
2021-01-03 14:25:43 +05:30
|
|
|
prepend ::Gitlab::Graphql::GlobalIDCompatibility
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
ERROR_MESSAGE = 'You cannot perform write operations on a read-only instance'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
field_class ::Types::BaseField
|
2021-04-29 21:17:54 +05:30
|
|
|
argument_class ::Types::BaseArgument
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
field :errors, [GraphQL::Types::String],
|
2018-11-18 11:00:15 +05:30
|
|
|
null: false,
|
2020-06-23 00:09:42 +05:30
|
|
|
description: 'Errors encountered during execution of the mutation.'
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
def current_user
|
|
|
|
context[:current_user]
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def api_user?
|
|
|
|
context[:is_sessionless_user]
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
# Returns Array of errors on an ActiveRecord object
|
|
|
|
def errors_on_object(record)
|
|
|
|
record.errors.full_messages
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
def ready?(**args)
|
2021-04-29 21:17:54 +05:30
|
|
|
raise_resource_not_available_error! ERROR_MESSAGE if Gitlab::Database.read_only?
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
missing_args = self.class.arguments.values
|
|
|
|
.reject { |arg| arg.accepts?(args.fetch(arg.keyword, :not_given)) }
|
|
|
|
.map(&:graphql_name)
|
|
|
|
|
|
|
|
raise ArgumentError, "Arguments must be provided: #{missing_args.join(", ")}" if missing_args.any?
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
true
|
|
|
|
end
|
2021-04-28 17:22:55 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
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.
|
2021-04-28 17:22:55 +05:30
|
|
|
raise_resource_not_available_error!
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
def self.authorizes_object?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.authorized?(object, context)
|
|
|
|
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!
|
|
|
|
end
|
|
|
|
|
|
|
|
# See: AuthorizeResource#authorized_resource?
|
|
|
|
def self.authorization
|
|
|
|
@authorization ||= ::Gitlab::Graphql::Authorize::ObjectAuthorization.new(authorize)
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|