Convert from GraphQL::Batch to BatchLoader
This commit is contained in:
parent
9c6c17cbcd
commit
287c34ca1f
12 changed files with 43 additions and 87 deletions
1
Gemfile
1
Gemfile
|
@ -95,7 +95,6 @@ gem 'rack-cors', '~> 1.0.0', require: 'rack/cors'
|
|||
|
||||
# GraphQL API
|
||||
gem 'graphql', '~> 1.7.14'
|
||||
gem 'graphql-batch', '~> 0.3.9'
|
||||
gem 'graphql-preload', '~> 2.0.0'
|
||||
gem 'graphiql-rails', '~> 1.4.10'
|
||||
|
||||
|
|
|
@ -1068,7 +1068,6 @@ DEPENDENCIES
|
|||
grape_logging (~> 1.7)
|
||||
graphiql-rails (~> 1.4.10)
|
||||
graphql (~> 1.7.14)
|
||||
graphql-batch (~> 0.3.9)
|
||||
graphql-preload (~> 2.0.0)
|
||||
grpc (~> 1.11.0)
|
||||
haml_lint (~> 0.26.0)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
Gitlab::Graphql::Authorize.register!
|
||||
|
||||
GitlabSchema = GraphQL::Schema.define do
|
||||
use GraphQL::Batch
|
||||
use BatchLoader::GraphQL
|
||||
|
||||
enable_preloading
|
||||
enable_authorization
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
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
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
||||
promise.then do |project|
|
||||
if project
|
||||
merge_request_by_project_and_iid(project.id, iid)
|
||||
else
|
||||
nil
|
||||
def merge_request_by_project_and_iid(project_loader, iid)
|
||||
project_id = project_loader.__sync&.id
|
||||
|
||||
# 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
|
||||
|
|
|
@ -534,9 +534,3 @@
|
|||
:why: https://github.com/squaremo/bitsyntax-js/blob/master/LICENSE-MIT
|
||||
:versions: []
|
||||
:when: 2018-02-20 22:20:25.958123000 Z
|
||||
- promise.rb
|
||||
- Unlicense
|
||||
- :who:
|
||||
:why:
|
||||
:versions: []
|
||||
:when: 2017-09-05 13:10:22.752422892 Z
|
||||
|
|
|
@ -47,6 +47,8 @@ module Gitlab
|
|||
|
||||
def build_checker(current_user, abilities)
|
||||
proc do |obj|
|
||||
# Load the elements if they weren't loaded by BatchLoader yet
|
||||
obj = obj.sync if obj.respond_to?(:sync)
|
||||
obj if abilities.all? { |ability| Ability.allowed?(current_user, ability, obj) }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
|
||||
describe GitlabSchema do
|
||||
it 'uses batch loading' do
|
||||
expect(described_class.instrumenters[:multiplex]).to include(GraphQL::Batch::SetupMultiplex)
|
||||
expect(field_instrumenters).to include(BatchLoader::GraphQL)
|
||||
end
|
||||
|
||||
it 'enables the preload instrumenter' do
|
||||
|
|
|
@ -24,12 +24,6 @@ describe Loaders::FullPathLoader do
|
|||
|
||||
expect(result).to be_nil
|
||||
end
|
||||
|
||||
it 'returns a promise' do
|
||||
batch do
|
||||
expect(resolve_project(project1.full_path)).to be_a(Promise)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def resolve_project(full_path)
|
||||
|
|
|
@ -50,12 +50,6 @@ describe Loaders::IidLoader do
|
|||
|
||||
expect(result).to be_nil
|
||||
end
|
||||
|
||||
it 'returns a promise' do
|
||||
batch do
|
||||
expect(resolve_mr(full_path, iid_1)).to be_a(Promise)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def resolve_mr(full_path, iid)
|
||||
|
|
|
@ -4,20 +4,17 @@ module GraphqlHelpers
|
|||
kls[name].call(obj, args, ctx)
|
||||
end
|
||||
|
||||
# Runs a block inside a GraphQL::Batch wrapper
|
||||
# Runs a block inside a BatchLoader::Executor wrapper
|
||||
def batch(max_queries: nil, &blk)
|
||||
wrapper = proc do
|
||||
GraphQL::Batch.batch do
|
||||
result = yield
|
||||
|
||||
if result.is_a?(Array)
|
||||
Promise.all(result)
|
||||
else
|
||||
result
|
||||
end
|
||||
begin
|
||||
BatchLoader::Executor.ensure_current
|
||||
blk.call
|
||||
ensure
|
||||
BatchLoader::Executor.clear_current
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
if max_queries
|
||||
result = nil
|
||||
expect { result = wrapper.call }.not_to exceed_query_limit(max_queries)
|
||||
|
|
Loading…
Reference in a new issue