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