Commit 272143b8 authored by Alex Kalderimis's avatar Alex Kalderimis Committed by Luke Duncalfe

Simplify preloading in BoardListType.title

https://gitlab.com/gitlab-org/gitlab/-/issues/342889
parent ceb9438a
......@@ -10,8 +10,10 @@ module Types
alias_method :list, :object
field :id, GraphQL::Types::ID, null: false,
field :id, GraphQL::Types::ID,
null: false,
description: 'ID (global ID) of the list.'
field :title, GraphQL::Types::String, null: false,
description: 'Title of the list.'
field :list_type, GraphQL::Types::String, null: false,
......@@ -50,14 +52,11 @@ module Types
# board lists have a data dependency on label - so we batch load them here
def title
if object.association(:label).loaded? && object.label_id.present?
object.title
else
loader = Gitlab::Graphql::Loaders::BatchModelLoader.new(Label, object.label_id)
Gitlab::Graphql::Lazy.with_value(loader.find) do |label|
object.label = label
object.title
end
BatchLoader::GraphQL.for(object).batch do |lists, callback|
ActiveRecord::Associations::Preloader.new.preload(lists, :label) # rubocop: disable CodeReuse/ActiveRecord
# all list titles are preloaded at this point
lists.each { |list| callback.call(list, list.title) }
end
end
end
......
......@@ -3,10 +3,13 @@
require 'spec_helper'
RSpec.describe GitlabSchema.types['BoardList'] do
include GraphqlHelpers
include Gitlab::Graphql::Laziness
specify { expect(described_class.graphql_name).to eq('BoardList') }
it 'has specific fields' do
expected_fields = %w[id list_type position label issues_count issues]
expected_fields = %w[id title list_type position label issues_count issues]
expect(described_class).to include_graphql_fields(*expected_fields)
end
......@@ -18,4 +21,18 @@ RSpec.describe GitlabSchema.types['BoardList'] do
is_expected.to have_graphql_extension(Gitlab::Graphql::Board::IssuesConnectionExtension)
end
end
describe 'title' do
subject(:field) { described_class.fields['title'] }
it 'preloads the label association' do
a, b, c = create_list(:list, 3).map { _1.class.find(_1.id) }
baseline = ActiveRecord::QueryRecorder.new { force(resolve_field(field, a)) }
expect do
[resolve_field(field, b), resolve_field(field, c)].each { force _1 }
end.not_to exceed_query_limit(baseline)
end
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