From 0ec245ce10657fcb573a5dda52a98ca12bcaa240 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Tue, 21 Apr 2020 14:43:16 -0400 Subject: [PATCH] Fix ruby 2.7 keyword arguments warning (#127) --- .travis.yml | 2 +- lib/graphql/batch/loader.rb | 42 +++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0af2eaa..c5ffb7a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: ruby rvm: - 2.3 - - 2.6 + - 2.7 env: - GRAPHQL_VERSION: 1.9.19 diff --git a/lib/graphql/batch/loader.rb b/lib/graphql/batch/loader.rb index 2b720a9..b0a7c11 100644 --- a/lib/graphql/batch/loader.rb +++ b/lib/graphql/batch/loader.rb @@ -1,21 +1,20 @@ 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) @@ -26,6 +25,23 @@ 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)