Commit 0b90a2d5 authored by Eugenia Grieff's avatar Eugenia Grieff

Add mutation concern to resolve single issuable

- Add Mutations::ResolvesIssuable to resolve a
single issue or merge request using existing
ResolvesProject
- Add concern specs
- Replace existing code with same functionality
- Rename resolver method in ResolvesGroup
and ResolvesProject to specify resolver class
parent e1587c3e
......@@ -5,10 +5,10 @@ module Mutations
extend ActiveSupport::Concern
def resolve_group(full_path:)
resolver.resolve(full_path: full_path)
group_resolver.resolve(full_path: full_path)
end
def resolver
def group_resolver
Resolvers::GroupResolver.new(object: nil, context: context)
end
end
......
# frozen_string_literal: true
module Mutations
module ResolvesIssuable
extend ActiveSupport::Concern
include Mutations::ResolvesProject
def resolve_issuable(type:, parent_path:, iid:)
parent = resolve_issuable_parent(parent_path)
issuable_resolver(type, parent, context).resolve(iid: iid.to_s)
end
def issuable_resolver(type, parent, context)
resolver_class = "Resolvers::#{type.to_s.classify.pluralize}Resolver".constantize
resolver_class.single.new(object: parent, context: context)
end
def resolve_issuable_parent(parent_path)
resolve_project(full_path: parent_path)
end
end
end
......@@ -5,10 +5,10 @@ module Mutations
extend ActiveSupport::Concern
def resolve_project(full_path:)
resolver.resolve(full_path: full_path)
project_resolver.resolve(full_path: full_path)
end
def resolver
def project_resolver
Resolvers::ProjectResolver.new(object: nil, context: context)
end
end
......
......@@ -3,7 +3,7 @@
module Mutations
module Issues
class Base < BaseMutation
include Mutations::ResolvesProject
include Mutations::ResolvesIssuable
argument :project_path, GraphQL::ID_TYPE,
required: true,
......@@ -23,11 +23,7 @@ module Mutations
private
def find_object(project_path:, iid:)
project = resolve_project(full_path: project_path)
resolver = Resolvers::IssuesResolver
.single.new(object: project, context: context)
resolver.resolve(iid: iid)
resolve_issuable(type: :issue, parent_path: project_path, iid: iid)
end
end
end
......
......@@ -3,7 +3,7 @@
module Mutations
module MergeRequests
class Base < BaseMutation
include Mutations::ResolvesProject
include Mutations::ResolvesIssuable
argument :project_path, GraphQL::ID_TYPE,
required: true,
......@@ -23,11 +23,7 @@ module Mutations
private
def find_object(project_path:, iid:)
project = resolve_project(full_path: project_path)
resolver = Resolvers::MergeRequestsResolver
.single.new(object: project, context: context)
resolver.resolve(iid: iid)
resolve_issuable(type: :merge_request, parent_path: project_path, iid: iid)
end
end
end
......
......@@ -3,7 +3,7 @@
module Mutations
module DesignManagement
class Base < ::Mutations::BaseMutation
include Mutations::ResolvesProject
include Mutations::ResolvesIssuable
argument :project_path, GraphQL::ID_TYPE,
required: true,
......@@ -16,10 +16,7 @@ module Mutations
private
def find_object(project_path:, iid:)
project = resolve_project(full_path: project_path)
Resolvers::IssuesResolver.single.new(object: project, context: context)
.resolve(iid: iid)
resolve_issuable(type: :issue, parent_path: project_path, iid: iid)
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Mutations::ResolvesIssuable do
let(:mutation_class) do
Class.new(Mutations::BaseMutation) do
include Mutations::ResolvesIssuable
end
end
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:context) { { current_user: user } }
let(:mutation) { mutation_class.new(object: nil, context: context) }
shared_examples 'resolving an issuable' do |type|
context 'when user has access' do
let(:source) { type == :merge_request ? 'source_project' : 'project' }
let(:issuable) { create(type, author: user, "#{source}" => project) }
subject { mutation.resolve_issuable(type: type, parent_path: project.full_path, iid: issuable.iid) }
before do
project.add_developer(user)
end
it 'resolves issuable by iid' do
result = type == :merge_request ? subject.sync : subject
expect(result).to eq(issuable)
end
it 'uses the correct Resolver to resolve issuable' do
resolver_class = "Resolvers::#{type.to_s.classify.pluralize}Resolver".constantize
resolved_project = mutation.resolve_project(full_path: project.full_path)
allow(mutation).to receive(:resolve_project)
.with(full_path: project.full_path)
.and_return(resolved_project)
expect(resolver_class).to receive(:new)
.with(object: resolved_project, context: context)
.and_call_original
subject
end
it 'uses the ResolvesProject to resolve project' do
expect(Resolvers::ProjectResolver).to receive(:new)
.with(object: nil, context: context)
.and_call_original
subject
end
it 'returns nil if issuable is not found' do
result = mutation.resolve_issuable(type: type, parent_path: project.full_path, iid: "100")
result = type == :merge_request ? result.sync : result
expect(result).to be_nil
end
end
end
context 'with issues' do
it_behaves_like 'resolving an issuable', :issue
end
context 'with merge requests' do
it_behaves_like 'resolving an issuable', :merge_request
end
end
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