Commit 8b94cb69 authored by charlieablett's avatar charlieablett

Rename aggregate file to block

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