Commit 76007c72 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'batch-load-user-notes-count' into 'master'

Preload user notes count in GraphQL

See merge request gitlab-org/gitlab!44894
parents 92b51294 acd04842
...@@ -159,6 +159,18 @@ module Types ...@@ -159,6 +159,18 @@ module Types
object.approved_by_users object.approved_by_users
end end
# rubocop: disable CodeReuse/ActiveRecord
def user_notes_count
BatchLoader::GraphQL.for(object.id).batch(key: :merge_request_user_notes_count) do |ids, loader, args|
counts = Note.where(noteable_type: 'MergeRequest', noteable_id: ids).user.group(:noteable_id).count
ids.each do |id|
loader.call(id, counts[id] || 0)
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
def diff_stats(path: nil) def diff_stats(path: nil)
stats = Array.wrap(object.diff_stats&.to_a) stats = Array.wrap(object.diff_stats&.to_a)
......
---
title: Preload `user_notes_count` in MergeRequest GraphQL API
merge_request: 44894
author:
type: performance
...@@ -232,7 +232,19 @@ RSpec.describe 'getting merge request listings nested in a project' do ...@@ -232,7 +232,19 @@ RSpec.describe 'getting merge request listings nested in a project' do
include_examples 'N+1 query check' include_examples 'N+1 query check'
end end
context 'when requesting `user_notes_count`' do
let(:requested_fields) { [:user_notes_count] }
before do
create_list(:note_on_merge_request, 2, noteable: merge_request_a, project: project)
create(:note_on_merge_request, noteable: merge_request_c, project: project)
end
include_examples 'N+1 query check'
end
end end
describe 'sorting and pagination' do describe 'sorting and pagination' do
let(:data_path) { [:project, :mergeRequests] } let(:data_path) { [:project, :mergeRequests] }
......
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