Commit 97944f44 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch...

Merge branch '34408-reducing-the-data-being-loaded-per-design-on-first-load-of-design-management' into 'master'

[BE] Reduce the data being loaded per Design on first load

See merge request gitlab-org/gitlab!18997
parents 49b37da3 43d2d32d
......@@ -2,8 +2,11 @@
module DesignManagement
class DesignsFinder
attr_reader :issue, :current_user, :params
include Gitlab::Allowable
# Params:
# ids: integer[]
# visible_at_version: ?version
def initialize(issue, current_user, params = {})
@issue = issue
@current_user = current_user
......@@ -11,18 +14,33 @@ module DesignManagement
end
def execute
unless Ability.allowed?(current_user, :read_design, issue)
return ::DesignManagement::Design.none
end
items = init_collection
items = by_visible_at_version(items)
items = by_ids(items)
by_visible_at_version(issue.designs)
items
end
private
attr_reader :issue, :current_user, :params
def init_collection
return ::DesignManagement::Design.none unless can?(current_user, :read_design, issue)
issue.designs
end
# Returns all designs that existed at a particular design version
def by_visible_at_version(items)
items.visible_at_version(params[:visible_at_version])
end
def by_ids(items)
return items unless params[:ids].present?
items.id_in(params[:ids])
end
end
end
......@@ -3,19 +3,31 @@
module Resolvers
module DesignManagement
class DesignResolver < BaseResolver
argument :ids, [GraphQL::ID_TYPE], required: false, description: 'The list of IDs of designs.'
argument :at_version,
GraphQL::ID_TYPE,
required: false,
description: 'Filters designs to only those that existed at the version. ' \
'If argument is omitted or nil then all designs will reflect the latest version.'
def resolve(at_version: nil)
version = at_version ? GitlabSchema.object_from_id(at_version)&.sync : nil
def resolve(**args)
find_designs(args)
end
def version(args)
args[:at_version] ? GitlabSchema.object_from_id(args[:at_version])&.sync : nil
end
def design_ids(args)
args[:ids] ? args[:ids].map { |id| GlobalID.parse(id).model_id } : nil
end
def find_designs(args)
::DesignManagement::DesignsFinder.new(
object.issue,
context[:current_user],
visible_at_version: version
ids: design_ids(args),
visible_at_version: version(args)
).execute
end
end
......
......@@ -52,6 +52,26 @@ describe DesignManagement::DesignsFinder do
it { is_expected.to eq([design1]) }
end
context 'when argument is the ids of designs' do
let(:params) { { ids: [design1.id] } }
it { is_expected.to eq([design1]) }
end
context 'when arguments are version and id' do
context 'when id is absent at version' do
let(:params) { { visible_at_version: first_version, ids: [design2.id] } }
it { is_expected.to eq([]) }
end
context 'when id is present at version' do
let(:params) { { visible_at_version: second_version, ids: [design2.id] } }
it { is_expected.to eq([design2]) }
end
end
context 'when argument is the second version' do
let(:params) { { visible_at_version: second_version } }
......
# frozen_string_literal: true
require "spec_helper"
describe Resolvers::DesignManagement::DesignResolver do
include GraphqlHelpers
include DesignManagementTestHelpers
before do
enable_design_management
end
describe "#resolve" do
set(:issue) { create(:issue) }
set(:project) { issue.project }
set(:first_version) { create(:design_version) }
set(:first_design) { create(:design, issue: issue, versions: [first_version]) }
set(:current_user) { create(:user) }
before do
project.add_developer(current_user)
end
context "when the user cannot see designs" do
it "returns nothing" do
expect(resolve_designs(issue.design_collection, {}, current_user: create(:user))).to be_empty
end
end
context "for a design collection" do
it "returns designs" do
expect(resolve_designs(issue.design_collection, {}, current_user: current_user)).to eq([first_design])
end
it "returns all designs" do
second_version = create(:design_version)
second_design = create(:design, issue: issue, versions: [second_version])
expect(resolve_designs(issue.design_collection, {}, current_user: current_user)).to eq([first_design, second_design])
end
end
end
def resolve_designs(obj, args = {}, context = { current_user: current_user })
resolve(described_class, obj: obj, args: args, ctx: context)
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