From d7af2cc4b47c4a3f238dffff0f5edf94e3d81534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Wed, 22 Feb 2017 21:40:15 -0500 Subject: [PATCH 1/3] Raise with Helpful message when no executor is set --- lib/graphql/batch/loader.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/graphql/batch/loader.rb b/lib/graphql/batch/loader.rb index 84f4cc9..a370abc 100644 --- a/lib/graphql/batch/loader.rb +++ b/lib/graphql/batch/loader.rb @@ -1,8 +1,17 @@ module GraphQL::Batch class Loader + class NoExecutorError < StandardError; end + def self.for(*group_args) loader_key = [self].concat(group_args) executor = Executor.current + + unless executor + raise 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.loaders[loader_key] ||= new(*group_args).tap do |loader| loader.loader_key = loader_key loader.executor = executor From d19198d33183bf5175788386cef6e10319cda5a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Wed, 22 Feb 2017 21:40:35 -0500 Subject: [PATCH 2/3] rm extra space --- test/loader_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/loader_test.rb b/test/loader_test.rb index c49e30d..09e1f17 100644 --- a/test/loader_test.rb +++ b/test/loader_test.rb @@ -41,7 +41,6 @@ def teardown GraphQL::Batch::Executor.current = nil end - def test_single_query assert_equal 1, GroupCountLoader.for('single').load('first').sync end From 2bf0b64aced12bfecb242f455a875092f68c7419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Wed, 22 Feb 2017 21:43:24 -0500 Subject: [PATCH 3/3] add test for NoExecutorError --- test/loader_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/loader_test.rb b/test/loader_test.rb index 09e1f17..1490b3b 100644 --- a/test/loader_test.rb +++ b/test/loader_test.rb @@ -41,6 +41,14 @@ def teardown GraphQL::Batch::Executor.current = nil end + def test_no_executor + GraphQL::Batch::Executor.current = nil + + assert_raises(GraphQL::Batch::Loader::NoExecutorError) do + EchoLoader.for + end + end + def test_single_query assert_equal 1, GroupCountLoader.for('single').load('first').sync end