lib/graphql/batch/loader.rb in graphql-batch-0.4.2 vs lib/graphql/batch/loader.rb in graphql-batch-0.4.3

- old
+ new

@@ -1,30 +1,46 @@ module GraphQL::Batch class Loader - def self.for(*group_args) - loader_key = loader_key_for(*group_args) - executor = Executor.current - - unless executor - raise GraphQL::Batch::NoExecutorError, 'Cannot create loader without'\ - ' an Executor. Wrap the call to `for` with `GraphQL::Batch.batch`'\ - ' or use `GraphQL::Batch::Setup` as a query instrumenter if'\ - ' using with `graphql-ruby`' + # Use new argument forwarding syntax if available as an optimization + if RUBY_ENGINE && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7") + class_eval(<<~RUBY, __FILE__, __LINE__ + 1) + def self.for(...) + current_executor.loader(loader_key_for(...)) { new(...) } + end + RUBY + else + def self.for(*group_args) + current_executor.loader(loader_key_for(*group_args)) { new(*group_args) } end - - executor.loader(loader_key) { new(*group_args) } end - def self.loader_key_for(*group_args) - [self].concat(group_args) + def self.loader_key_for(*group_args, **group_kwargs) + [self, group_kwargs, group_args] end def self.load(key) self.for.load(key) end def self.load_many(keys) self.for.load_many(keys) + end + + class << self + private + + def current_executor + executor = Executor.current + + unless executor + raise GraphQL::Batch::NoExecutorError, 'Cannot create loader without'\ + ' an Executor. Wrap the call to `for` with `GraphQL::Batch.batch`'\ + ' or use `GraphQL::Batch::Setup` as a query instrumenter if'\ + ' using with `graphql-ruby`' + end + + executor + end end attr_accessor :loader_key, :executor def load(key)