Commit 7c434021 authored by Alex Kalderimis's avatar Alex Kalderimis

Remove re-use of resolver in mutation

Re-using resolvers anywhere is an anti-pattern (and should be completely
banned with a cop) - they do not compose well at all, and calling them
correctly is practically impossible.

And in any case, mutations do not need their
features (laziness, parallel computation) - instead the re-usable
elements are the same as throughout our application: finders and
services.

This replaces the user of a resolver with a finder, and a plain
find_by_full_path, leaving us with simpler code that is easier to follow
and allocates less garbage.

This work was incidental to
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/40088
and has been extracted from there.
parent 2df95811
......@@ -4,7 +4,6 @@ module Mutations
module AlertManagement
class Base < BaseMutation
include Gitlab::Utils::UsageData
include ResolvesProject
argument :project_path, GraphQL::ID_TYPE,
required: true,
......@@ -33,13 +32,12 @@ module Mutations
private
def find_object(project_path:, iid:)
project = resolve_project(full_path: project_path)
def find_object(project_path:, **args)
project = Project.find_by_full_path(project_path)
return unless project
resolver = Resolvers::AlertManagement::AlertResolver.single.new(object: project, context: context, field: nil)
resolver.resolve(iid: iid)
::AlertManagement::AlertsFinder.new(current_user, project, args).execute.first
end
end
end
......
......@@ -9,9 +9,9 @@ module Mutations
required: true,
description: 'The status to set the alert'
def resolve(args)
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = update_status(alert, args[:status])
def resolve(project_path:, iid:, status:)
alert = authorized_find!(project_path: project_path, iid: iid)
result = update_status(alert, status)
track_usage_event(:incident_management_alert_status_changed, current_user.id)
......
......@@ -37,8 +37,8 @@ RSpec.describe Mutations::AlertManagement::UpdateAlertStatus do
context 'error occurs when updating' do
it 'returns the alert with errors' do
# Stub an error on the alert
allow_next_instance_of(Resolvers::AlertManagement::AlertResolver) do |resolver|
allow(resolver).to receive(:resolve).and_return(alert)
allow_next_instance_of(::AlertManagement::AlertsFinder) do |finder|
allow(finder).to receive(:execute).and_return([alert])
end
allow(alert).to receive(:save).and_return(false)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment