Class: Mutations::BaseMutation

Inherits:
GraphQL::Schema::RelayClassicMutation
  • Object
show all
Includes:
Gitlab::Graphql::Authorize::AuthorizeResource, Gitlab::Graphql::CopyFieldDescription, Gitlab::Graphql::GlobalIDCompatibility
Defined in:
app/graphql/mutations/base_mutation.rb

Direct Known Subclasses

Admin::SidekiqQueues::DeleteJobs, AlertManagement::Base, AlertManagement::HttpIntegration::HttpIntegrationBase, AlertManagement::PrometheusIntegration::PrometheusIntegrationBase, AwardEmojis::Base, Mutations::Boards::Create, Mutations::Boards::Destroy, Mutations::Boards::Lists::BaseCreate, Mutations::Boards::Lists::BaseUpdate, Mutations::Boards::Lists::Destroy, Mutations::Boards::Update, Mutations::Branches::Create, Ci::CiCdSettingsUpdate, Ci::Job::Base, Ci::JobTokenScope::AddProject, Ci::JobTokenScope::RemoveProject, Ci::Pipeline::Base, Ci::Runner::Delete, Ci::Runner::Update, Ci::RunnersRegistrationToken::Reset, Clusters::AgentTokens::Create, Clusters::AgentTokens::Revoke, Clusters::Agents::Create, Clusters::Agents::Delete, Commits::Create, ContainerExpirationPolicies::Update, ContainerRepositories::DestroyBase, CustomEmoji::Create, CustomEmoji::Destroy, CustomerRelations::Contacts::Create, CustomerRelations::Contacts::Update, CustomerRelations::Organizations::Create, CustomerRelations::Organizations::Update, DependencyProxy::GroupSettings::Update, DependencyProxy::ImageTtlGroupPolicy::Update, DesignManagement::Base, DesignManagement::Move, Discussions::ToggleResolve, Echo, Environments::CanaryIngress::Update, Groups::Update, Issues::Base, Issues::Create, Issues::SetSubscription, JiraImport::ImportUsers, JiraImport::Start, Labels::Create, MergeRequests::Base, MergeRequests::Create, MergeRequests::SetSubscription, Metrics::Dashboard::Annotations::Base, Metrics::Dashboard::Annotations::Create, Namespace::PackageSettings::Update, Notes::Base, Packages::Destroy, Packages::DestroyFile, ReleaseAssetLinks::Create, ReleaseAssetLinks::Delete, ReleaseAssetLinks::Update, Releases::Base, SavedReplies::Base, Security::CiConfiguration::BaseSecurityAnalyzer, Snippets::Base, Snippets::Create, Terraform::State::Base, Timelogs::Delete, Todos::Base, UserCallouts::Create, UserPreferences::Update, WorkItems::Create, WorkItems::CreateFromTask, WorkItems::Delete, WorkItems::DeleteTask, WorkItems::Update

Constant Summary collapse

ERROR_MESSAGE =
'You cannot perform write operations on a read-only instance'

Constants included from Gitlab::Graphql::Authorize::AuthorizeResource

Gitlab::Graphql::Authorize::AuthorizeResource::ConfigurationError, Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Graphql::Authorize::AuthorizeResource

#authorize!, #authorized_find!, #authorized_resource?, #find_object, #raise_resource_not_available_error!

Methods included from Gitlab::Graphql::GlobalIDCompatibility

#coerce_global_id_arguments!

Class Method Details

.authorizationObject

See: AuthorizeResource#authorized_resource?


68
69
70
# File 'app/graphql/mutations/base_mutation.rb', line 68

def self.authorization
  @authorization ||= ::Gitlab::Graphql::Authorize::ObjectAuthorization.new(authorize)
end

.authorized?(object, context) ⇒ Boolean

Returns:

  • (Boolean)

57
58
59
60
61
62
63
64
65
# File 'app/graphql/mutations/base_mutation.rb', line 57

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

.authorizes_object?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'app/graphql/mutations/base_mutation.rb', line 53

def self.authorizes_object?
  true
end

Instance Method Details

#api_user?Boolean

Returns:

  • (Boolean)

22
23
24
# File 'app/graphql/mutations/base_mutation.rb', line 22

def api_user?
  context[:is_sessionless_user]
end

#current_userObject


18
19
20
# File 'app/graphql/mutations/base_mutation.rb', line 18

def current_user
  context[:current_user]
end

#errors_on_object(record) ⇒ Object

Returns Array of errors on an ActiveRecord object


27
28
29
# File 'app/graphql/mutations/base_mutation.rb', line 27

def errors_on_object(record)
  record.errors.full_messages
end

#load_application_object(argument, lookup_as_type, id, context) ⇒ Object


43
44
45
46
47
48
49
50
51
# File 'app/graphql/mutations/base_mutation.rb', line 43

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

#ready?(**args) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

31
32
33
34
35
36
37
38
39
40
41
# File 'app/graphql/mutations/base_mutation.rb', line 31

def ready?(**args)
  raise_resource_not_available_error! ERROR_MESSAGE if Gitlab::Database.read_only?

  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?

  true
end