diff options
author | Nick Thomas <nick@gitlab.com> | 2018-02-23 18:36:40 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-06-05 21:47:42 +0300 |
commit | 287c34ca1f9af4e395493c99623af8437f82d919 (patch) | |
tree | a2748dc20c4a159d93564a42dddec7e768e22a43 /app/graphql | |
parent | 9c6c17cbcdb8bf8185fc1b873dcfd08f723e4df5 (diff) |
Convert from GraphQL::Batch to BatchLoader
Diffstat (limited to 'app/graphql')
-rw-r--r-- | app/graphql/gitlab_schema.rb | 2 | ||||
-rw-r--r-- | app/graphql/loaders/base_loader.rb | 21 | ||||
-rw-r--r-- | app/graphql/loaders/full_path_loader.rb | 24 | ||||
-rw-r--r-- | app/graphql/loaders/iid_loader.rb | 40 |
4 files changed, 32 insertions, 55 deletions
diff --git a/app/graphql/gitlab_schema.rb b/app/graphql/gitlab_schema.rb index 7392bf6f503..143ccf92c85 100644 --- a/app/graphql/gitlab_schema.rb +++ b/app/graphql/gitlab_schema.rb @@ -1,7 +1,7 @@ Gitlab::Graphql::Authorize.register! GitlabSchema = GraphQL::Schema.define do - use GraphQL::Batch + use BatchLoader::GraphQL enable_preloading enable_authorization diff --git a/app/graphql/loaders/base_loader.rb b/app/graphql/loaders/base_loader.rb index c32c4daa91a..aad435ea09b 100644 --- a/app/graphql/loaders/base_loader.rb +++ b/app/graphql/loaders/base_loader.rb @@ -1,8 +1,10 @@ # Helper methods for all loaders -class Loaders::BaseLoader < GraphQL::Batch::Loader - # Convert a class method into a resolver proc. The method should follow the - # (obj, args, ctx) calling convention - class << self +module Loaders::BaseLoader + extend ActiveSupport::Concern + + class_methods do + # Convert a class method into a resolver proc. The method should follow the + # (obj, args, ctx) calling convention def [](sym) resolver = method(sym) raise ArgumentError.new("#{self}.#{sym} is not a resolver") unless resolver.arity == 3 @@ -10,15 +12,4 @@ class Loaders::BaseLoader < GraphQL::Batch::Loader resolver end end - - # Fulfill all keys. Pass a block that converts each result into a key. - # Any keys not in results will be fulfilled with nil. - def fulfill_all(results, keys, &key_blk) - results.each do |result| - key = yield result - fulfill(key, result) - end - - keys.each { |key| fulfill(key, nil) unless fulfilled?(key) } - end end diff --git a/app/graphql/loaders/full_path_loader.rb b/app/graphql/loaders/full_path_loader.rb index f99b487ce5d..27ada90b82a 100644 --- a/app/graphql/loaders/full_path_loader.rb +++ b/app/graphql/loaders/full_path_loader.rb @@ -1,23 +1,21 @@ -class Loaders::FullPathLoader < Loaders::BaseLoader +module Loaders::FullPathLoader + include Loaders::BaseLoader + class << self def project(obj, args, ctx) project_by_full_path(args[:full_path]) end def project_by_full_path(full_path) - self.for(Project).load(full_path) + model_by_full_path(Project, full_path) end - end - - attr_reader :model - def initialize(model) - @model = model - end - - def perform(keys) - # `with_route` prevents relation.all.map(&:full_path)` from being N+1 - relation = model.where_full_path_in(keys).with_route - fulfill_all(relation, keys, &:full_path) + def model_by_full_path(model, full_path) + BatchLoader.for(full_path).batch(key: "#{model.model_name.param_key}:full_path") do |full_paths, loader| + # `with_route` avoids an N+1 calculating full_path + results = model.where_full_path_in(full_paths).with_route + results.each { |project| loader.call(project.full_path, project) } + end + end end end diff --git a/app/graphql/loaders/iid_loader.rb b/app/graphql/loaders/iid_loader.rb index e89031da0c2..f9827765a92 100644 --- a/app/graphql/loaders/iid_loader.rb +++ b/app/graphql/loaders/iid_loader.rb @@ -1,35 +1,23 @@ -class Loaders::IidLoader < Loaders::BaseLoader +class Loaders::IidLoader + include Loaders::BaseLoader + class << self def merge_request(obj, args, ctx) iid = args[:iid] - promise = Loaders::FullPathLoader.project_by_full_path(args[:project]) + project = Loaders::FullPathLoader.project_by_full_path(args[:project]) + merge_request_by_project_and_iid(project, iid) + end + + def merge_request_by_project_and_iid(project_loader, iid) + project_id = project_loader.__sync&.id - promise.then do |project| - if project - merge_request_by_project_and_iid(project.id, iid) - else - nil + # IIDs are represented as the GraphQL `id` type, which is a string + BatchLoader.for(iid.to_s).batch(key: "merge_request:target_project:#{project_id}:iid") do |iids, loader| + if project_id + results = MergeRequest.where(target_project_id: project_id, iid: iids) + results.each { |mr| loader.call(mr.iid.to_s, mr) } end end end - - def merge_request_by_project_and_iid(project_id, iid) - self.for(MergeRequest, target_project_id: project_id.to_s).load(iid.to_s) - end - end - - attr_reader :model, :restrictions - - def initialize(model, restrictions = {}) - @model = model - @restrictions = restrictions - end - - def perform(keys) - relation = model.where(iid: keys) - relation = relation.where(restrictions) if restrictions.present? - - # IIDs are represented as the GraphQL `id` type, which is a string - fulfill_all(relation, keys) { |instance| instance.iid.to_s } end end |