Commit 8b94cb69 authored by charlieablett's avatar charlieablett

Rename aggregate file to block

- Apply reviewer comments
parent 8c391ad3
......@@ -6,7 +6,7 @@ module EE
prepended do
lazy_resolve ::Gitlab::Graphql::Aggregations::Epics::LazyEpicAggregate, :epic_aggregate
lazy_resolve ::Gitlab::Graphql::Aggregations::Issues::LazyIssueLinkAggregate, :issue_link_aggregate
lazy_resolve ::Gitlab::Graphql::Aggregations::Issues::LazyBlockAggregate, :block_aggregate
end
end
end
......@@ -20,7 +20,7 @@ module EE
field :blocked, GraphQL::BOOLEAN_TYPE, null: false,
description: 'Indicates the issue is blocked',
resolve: -> (obj, _args, ctx) {
::Gitlab::Graphql::Aggregations::Issues::LazyIssueLinkAggregate.new(ctx, obj.id)
::Gitlab::Graphql::Aggregations::Issues::LazyBlockAggregate.new(ctx, obj.id)
}
field :health_status,
......
......@@ -4,7 +4,7 @@ module Gitlab
module Graphql
module Aggregations
module Issues
class LazyIssueLinkAggregate
class LazyBlockAggregate
attr_reader :issue_id, :lazy_state
def initialize(query_ctx, issue_id)
......@@ -12,40 +12,34 @@ module Gitlab
# Initialize the loading state for this query,
# or get the previously-initiated state
@lazy_state = query_ctx[:lazy_issue_link_aggregate] ||= {
pending_ids: Set.new,
loaded_objects: {}
@lazy_state = query_ctx[:lazy_block_aggregate] ||= {
pending_ids: Set.new,
loaded_objects: {}
}
# Register this ID to be loaded later:
@lazy_state[:pending_ids] << issue_id
end
# Return the loaded record, hitting the database if needed
def issue_link_aggregate
# Check if the record was already loaded:
# load from loaded_objects by issue
unless @lazy_state[:loaded_objects][@issue_id]
def block_aggregate
# Check if the record was already loaded
if @lazy_state[:pending_ids].present?
load_records_into_loaded_objects
end
loaded_objects[@issue_id].any?
!!@lazy_state[:loaded_objects][@issue_id]
end
private
def loaded_objects
@lazy_state[:loaded_objects]
end
def load_records_into_loaded_objects
# The record hasn't been loaded yet, so
# hit the database with all pending IDs to prevent N+1
pending_ids = @lazy_state[:pending_ids].to_a
blocked = IssueLink.blocked_issues_for_collection(pending_ids).compact.flatten
pending_ids.each do |id|
# result is either [] or an array with a link aggregate object
@lazy_state[:loaded_objects][id] = blocked.select { |o| o.blocked_issue_id == id }
blocked.each do |o|
@lazy_state[:loaded_objects][o.blocked_issue_id] = true
end
@lazy_state[:pending_ids].clear
......
......@@ -2,23 +2,25 @@
require 'spec_helper'
RSpec.describe Gitlab::Graphql::Aggregations::Issues::LazyIssueLinkAggregate do
RSpec.describe Gitlab::Graphql::Aggregations::Issues::LazyBlockAggregate do
let(:query_ctx) do
{}
end
let(:issue_id) { 37 }
let(:blocks_issue_id) { 18 }
let(:blocking_issue_id) { 38 }
describe '#initialize' do
it 'adds the issue_id to lazy state' do
described_class.new(query_ctx, issue_id)
it 'adds the issue_id to the lazy state' do
subject = described_class.new(query_ctx, issue_id)
expect(query_ctx[:lazy_issue_link_aggregate][:pending_ids]).to match [issue_id]
expect(subject.lazy_state[:pending_ids]).to match [issue_id]
expect(subject.issue_id).to match issue_id
end
end
describe '#issue_link_aggregate' do
describe '#block_aggregate' do
subject { described_class.new(query_ctx, issue_id) }
before do
......@@ -27,13 +29,13 @@ RSpec.describe Gitlab::Graphql::Aggregations::Issues::LazyIssueLinkAggregate do
context 'if the record has already been loaded' do
let(:fake_state) do
{ pending_ids: Set.new, loaded_objects: { issue_id => [] } }
{ pending_ids: Set.new, loaded_objects: { issue_id => true } }
end
it 'does not make the query again' do
expect(IssueLink).not_to receive(:blocked_issues_for_collection)
subject.issue_link_aggregate
subject.block_aggregate
end
end
......@@ -42,6 +44,7 @@ RSpec.describe Gitlab::Graphql::Aggregations::Issues::LazyIssueLinkAggregate do
let(:fake_state) do
{ pending_ids: Set.new([issue_id]), loaded_objects: {} }
end
let(:fake_data) do
[
double(blocked_issue_id: 1745, count: 1.0),
......@@ -54,11 +57,9 @@ RSpec.describe Gitlab::Graphql::Aggregations::Issues::LazyIssueLinkAggregate do
end
it 'clears the pending IDs' do
subject.issue_link_aggregate
lazy_state = subject.instance_variable_get(:@lazy_state)
subject.block_aggregate
expect(lazy_state[:pending_ids]).to be_empty
expect(subject.lazy_state[:pending_ids]).to be_empty
end
end
end
......
......@@ -89,8 +89,8 @@ RSpec.describe 'getting an issue list for a project' do
it_behaves_like 'a working graphql query'
end
it 'uses the LazyIssueLinkAggregate service' do
expect(::Gitlab::Graphql::Aggregations::Issues::LazyIssueLinkAggregate).to receive(:new)
it 'uses the LazyBlockAggregate service' do
expect(::Gitlab::Graphql::Aggregations::Issues::LazyBlockAggregate).to receive(:new)
post_graphql(single_issue_query, current_user: current_user)
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