Skip to content

Commit

Permalink
Fix ruby 2.7 keyword arguments warning (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith committed Apr 21, 2020
1 parent 3099e1e commit 4a9feba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,7 +1,7 @@
language: ruby
rvm:
- 2.3
- 2.6
- 2.7
env:
-
GRAPHQL_VERSION: 1.9.19
Expand Down
42 changes: 29 additions & 13 deletions 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)
Expand All @@ -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)
Expand Down

0 comments on commit 4a9feba

Please sign in to comment.