{"id":332626,"date":"2022-04-30T15:00:07","date_gmt":"2022-04-30T15:00:07","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=332626"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=332626","title":{"rendered":"<span>GraphQL \u0431\u0435\u0437 N+1 \u044d\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f N1Loader<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><a href=\"https:\/\/github.com\/djezzzl\/n1_loader#killer-feature-for-graphql-api\" rel=\"noopener noreferrer nofollow\">N1Loader<\/a> \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d \u0434\u043b\u044f \u043b\u0435\u0433\u043a\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f <a href=\"https:\/\/stackoverflow.com\/questions\/97197\/what-is-the-n1-selects-problem-in-orm-object-relational-mapping\" rel=\"noopener noreferrer nofollow\">N+1 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/a><br \/>\u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430. \u041a \u0441\u0447\u0430\u0442\u044c\u044e, \u0433\u0435\u043c \u043e\u0447\u0435\u043d\u044c \u043b\u0435\u0433\u043a\u043e \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 GraphQL<br \/>API. \u0411\u0435\u0437 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u043e\u0442\u043b\u0430\u0433\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043d\u043e \u0441\u0430\u043c\u043e\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440.<\/p>\n<pre><code class=\"ruby\"># \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c N1Loader \u0441 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0435\u0439 ArLazyPreload  require \"n1_loader\/ar_lazy_preload\" require 'graphql'  # \u0421\u043e\u0437\u0434\u0430\u0435\u043c SQLite \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u0435 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435. require_relative 'context\/setup_database' # ArLazyPreload \u0442\u0440\u0435\u0431\u0443\u0435\u0442 Rails \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0433\u043e. require_relative 'context\/setup_ar_lazy'  class User &lt; ActiveRecord::Base   has_many :payments    n1_optimized :payments_total do |users|     # Fetch payments for a group of users we will preload in a single query     total_per_user = Payment.group(:user_id).where(user: users).sum(:amount).tap { |h| h.default = 0 }      users.each do |user|       total = total_per_user[user.id]       # No promises here, simply add a value for to a user.       fulfill(user, total)     end   end end  class Payment &lt; ActiveRecord::Base   belongs_to :user    validates :amount, presence: true end  # \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c ArLazyPreload \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e ArLazyPreload.config.auto_preload = true # \u0418\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c preload_associations_lazily \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445  class UserType &lt; GraphQL::Schema::Object   field :payments_total, Integer end  class QueryType &lt; GraphQL::Schema::Object   field  :users, [UserType]    def users     User.all   end end  class Schema &lt; GraphQL::Schema   query QueryType end  query_string = &lt;&lt;~GQL   {     users {       paymentsTotal     }   } GQL  # \u0418\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 N+1! p Schema.execute(query_string)['data']<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u043e\u043b\u0448\u0435\u0431\u0441\u0442\u0432\u043e <a href=\"https:\/\/github.com\/djezzzl\/n1_loader\" rel=\"noopener noreferrer nofollow\">N1Loader<\/a> \u043d\u0435 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0442\u0430\u043a \u043a\u0430\u043a \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c. \u0418 \u0432\u0441\u0435 \u044d\u0442\u043e \u0431\u0435\u0437 N+1! <\/p>\n<pre><code class=\"ruby\">class User &lt; ActiveRecord::Base   n1_optimized :payments_total do     argument :from     argument :to      def perform(users)       total_per_user =         Payment           .group(:user_id)           .where(created_at: from..to)           .where(user: users)           .sum(:amount)           .tap { |h| h.default = 0 }        users.each do |user|         total = total_per_user[user.id]         fulfill(user, total)       end     end   end end  class UserType &lt; GraphQL::Schema::Object   field :payments_total, Integer do     argument :from, Time     argument :to, Time   end end  query_string = &lt;&lt;~GQL   {     users {       paymentsTotal     }   } GQL  # \u041d\u0435\u0442 N+1 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442! p Schema.execute(query_string, variables: {from: 3.days.ago, to: 1.day.ago})['data']<\/code><\/pre>\n<p><em>\u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0434\u0430\u043d\u043d\u044b\u0435 \u0435\u0434\u0438\u043d\u043e\u0436\u0434\u044b &#8212; \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432 \u043b\u044e\u0431\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0435\u0437 N+1! <\/em><\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 <a href=\"https:\/\/github.com\/djezzzl\/n1_loader\" rel=\"noopener noreferrer nofollow\">N1Loader<\/a> \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0432 \u0441\u0432\u043e\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/663834\/\"> https:\/\/habr.com\/ru\/post\/663834\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><a href=\"https:\/\/github.com\/djezzzl\/n1_loader#killer-feature-for-graphql-api\" rel=\"noopener noreferrer nofollow\">N1Loader<\/a> \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d \u0434\u043b\u044f \u043b\u0435\u0433\u043a\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f <a href=\"https:\/\/stackoverflow.com\/questions\/97197\/what-is-the-n1-selects-problem-in-orm-object-relational-mapping\" rel=\"noopener noreferrer nofollow\">N+1 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/a><br \/>\u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430. \u041a \u0441\u0447\u0430\u0442\u044c\u044e, \u0433\u0435\u043c \u043e\u0447\u0435\u043d\u044c \u043b\u0435\u0433\u043a\u043e \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 GraphQL<br \/>API. \u0411\u0435\u0437 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u043e\u0442\u043b\u0430\u0433\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043d\u043e \u0441\u0430\u043c\u043e\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440.<\/p>\n<pre><code class=\"ruby\"># \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c N1Loader \u0441 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0435\u0439 ArLazyPreload  require \"n1_loader\/ar_lazy_preload\" require 'graphql'  # \u0421\u043e\u0437\u0434\u0430\u0435\u043c SQLite \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u0435 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435. require_relative 'context\/setup_database' # ArLazyPreload \u0442\u0440\u0435\u0431\u0443\u0435\u0442 Rails \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0433\u043e. require_relative 'context\/setup_ar_lazy'  class User &lt; ActiveRecord::Base   has_many :payments    n1_optimized :payments_total do |users|     # Fetch payments for a group of users we will preload in a single query     total_per_user = Payment.group(:user_id).where(user: users).sum(:amount).tap { |h| h.default = 0 }      users.each do |user|       total = total_per_user[user.id]       # No promises here, simply add a value for to a user.       fulfill(user, total)     end   end end  class Payment &lt; ActiveRecord::Base   belongs_to :user    validates :amount, presence: true end  # \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c ArLazyPreload \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e ArLazyPreload.config.auto_preload = true # \u0418\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c preload_associations_lazily \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445  class UserType &lt; GraphQL::Schema::Object   field :payments_total, Integer end  class QueryType &lt; GraphQL::Schema::Object   field  :users, [UserType]    def users     User.all   end end  class Schema &lt; GraphQL::Schema   query QueryType end  query_string = &lt;&lt;~GQL   {     users {       paymentsTotal     }   } GQL  # \u0418\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 N+1! p Schema.execute(query_string)['data']<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u043e\u043b\u0448\u0435\u0431\u0441\u0442\u0432\u043e <a href=\"https:\/\/github.com\/djezzzl\/n1_loader\" rel=\"noopener noreferrer nofollow\">N1Loader<\/a> \u043d\u0435 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0442\u0430\u043a \u043a\u0430\u043a \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c. \u0418 \u0432\u0441\u0435 \u044d\u0442\u043e \u0431\u0435\u0437 N+1! <\/p>\n<pre><code class=\"ruby\">class User &lt; ActiveRecord::Base   n1_optimized :payments_total do     argument :from     argument :to      def perform(users)       total_per_user =         Payment           .group(:user_id)           .where(created_at: from..to)           .where(user: users)           .sum(:amount)           .tap { |h| h.default = 0 }        users.each do |user|         total = total_per_user[user.id]         fulfill(user, total)       end     end   end end  class UserType &lt; GraphQL::Schema::Object   field :payments_total, Integer do     argument :from, Time     argument :to, Time   end end  query_string = &lt;&lt;~GQL   {     users {       paymentsTotal     }   } GQL  # \u041d\u0435\u0442 N+1 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442! p Schema.execute(query_string, variables: {from: 3.days.ago, to: 1.day.ago})['data']<\/code><\/pre>\n<p><em>\u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0434\u0430\u043d\u043d\u044b\u0435 \u0435\u0434\u0438\u043d\u043e\u0436\u0434\u044b &#8212; \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432 \u043b\u044e\u0431\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0435\u0437 N+1! <\/em><\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 <a href=\"https:\/\/github.com\/djezzzl\/n1_loader\" rel=\"noopener noreferrer nofollow\">N1Loader<\/a> \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0432 \u0441\u0432\u043e\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/663834\/\"> https:\/\/habr.com\/ru\/post\/663834\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-332626","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/332626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=332626"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/332626\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=332626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=332626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=332626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}