From 385f43ea28f571dc6b925ea19a91ddec27df6a9e Mon Sep 17 00:00:00 2001 From: Sawy Date: Sun, 12 Jul 2026 21:56:46 +0300 Subject: [PATCH] CV2-6729: fix N+1 query --- app/models/annotations/annotation.rb | 19 +++++++++++++++++++ config/initializers/graphql_extensions.rb | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/annotations/annotation.rb b/app/models/annotations/annotation.rb index e8274d62e..ccdd30554 100644 --- a/app/models/annotations/annotation.rb +++ b/app/models/annotations/annotation.rb @@ -13,6 +13,25 @@ def load klass.where(id: self.id).last end + def self.load_edge_nodes(edge_nodes) + edge_nodes + .group_by { |node| self.annotation_class(node) } + .flat_map do |klass, nodes| + records = klass.where(id: nodes.map(&:id)).index_by(&:id) + nodes.map { |node| records[node.id] } + end + end + + def self.annotation_class(node) + klass = nil + begin + klass = node.annotation_type.camelize.constantize + rescue NameError + klass = Dynamic + end + klass + end + def destroy dec = self.disable_es_callbacks skip_ability = self.skip_check_ability diff --git a/config/initializers/graphql_extensions.rb b/config/initializers/graphql_extensions.rb index 2ccdb8737..87f98aae5 100644 --- a/config/initializers/graphql_extensions.rb +++ b/config/initializers/graphql_extensions.rb @@ -26,7 +26,7 @@ def sliced_nodes def edge_nodes @edge_nodes ||= paged_nodes - @edge_nodes = @edge_nodes.map(&:load) if @field.name == 'annotations' + @edge_nodes = Annotation.load_edge_nodes(@edge_nodes) if @field.name == "annotations" @edge_nodes end