From 36f6ca2b08eb9ba1aba0f674ea591ffb37fafe98 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Tue, 6 Jun 2017 14:59:44 -0400 Subject: [PATCH 1/2] feat(GraphQL::Batch::SetupMultiplex) use multiplex instrumentation to share a batching context --- lib/graphql/batch.rb | 7 +++- lib/graphql/batch/setup_multiplex.rb | 14 ++++++++ test/multiplex_test.rb | 51 ++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 lib/graphql/batch/setup_multiplex.rb create mode 100644 test/multiplex_test.rb diff --git a/lib/graphql/batch.rb b/lib/graphql/batch.rb index 730224c..73b01b3 100644 --- a/lib/graphql/batch.rb +++ b/lib/graphql/batch.rb @@ -20,7 +20,11 @@ def self.batch end def self.use(schema_defn) - schema_defn.instrument(:query, GraphQL::Batch::Setup) + if GraphQL::VERSION >= "1.6.0" + schema_defn.instrument(:multiplex, GraphQL::Batch::SetupMultiplex) + else + schema_defn.instrument(:query, GraphQL::Batch::Setup) + end schema_defn.lazy_resolve(::Promise, :sync) end @@ -34,3 +38,4 @@ def self.use(schema_defn) require_relative "batch/executor" require_relative "batch/promise" require_relative "batch/setup" +require_relative "batch/setup_multiplex" diff --git a/lib/graphql/batch/setup_multiplex.rb b/lib/graphql/batch/setup_multiplex.rb new file mode 100644 index 0000000..a9bb473 --- /dev/null +++ b/lib/graphql/batch/setup_multiplex.rb @@ -0,0 +1,14 @@ +module GraphQL::Batch + module SetupMultiplex + extend self + + def before_multiplex(multiplex) + raise NestedError if GraphQL::Batch::Executor.current + GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new + end + + def after_multiplex(multiplex) + GraphQL::Batch::Executor.current = nil + end + end +end diff --git a/test/multiplex_test.rb b/test/multiplex_test.rb new file mode 100644 index 0000000..c120614 --- /dev/null +++ b/test/multiplex_test.rb @@ -0,0 +1,51 @@ +require_relative 'test_helper' + +class GraphQL::MultiplexTest < Minitest::Test + attr_reader :queries + + def setup + @queries = [] + QueryNotifier.subscriber = ->(query) { @queries << query } + end + + def teardown + QueryNotifier.subscriber = nil + end + + def schema_multiplex(*args) + ::Schema.multiplex(*args) + end + + + def test_batched_find_by_id + query_string = <<-GRAPHQL + query FetchTwoProducts($id1: ID!, $id2: ID!){ + first: product(id: $id1) { id, title } + second: product(id: $id2) { id, title } + } + GRAPHQL + + results = schema_multiplex([ + { query: query_string, variables: { "id1" => "1", "id2" => "2"} }, + { query: query_string, variables: { "id1" => "1", "id2" => "3"}}, + ]) + + expected_1 = { + "data" => { + "first" => { "id" => "1", "title" => "Shirt" }, + "second" => { "id" => "2", "title" => "Pants" }, + } + } + + expected_2 = { + "data" => { + "first" => { "id" => "1", "title" => "Shirt" }, + "second" => { "id" => "3", "title" => "Sweater" }, + } + } + + assert_equal expected_1, results.first + assert_equal expected_2, results.last + assert_equal ["Product/1,2,3"], queries + end +end From bcf1d87b67110468276783ee8a43adcd1896caa3 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Fri, 14 Jul 2017 09:44:03 -0400 Subject: [PATCH 2/2] refactor(tests) use Schema.multiplex directly --- test/multiplex_test.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/multiplex_test.rb b/test/multiplex_test.rb index c120614..466a0e6 100644 --- a/test/multiplex_test.rb +++ b/test/multiplex_test.rb @@ -12,11 +12,6 @@ def teardown QueryNotifier.subscriber = nil end - def schema_multiplex(*args) - ::Schema.multiplex(*args) - end - - def test_batched_find_by_id query_string = <<-GRAPHQL query FetchTwoProducts($id1: ID!, $id2: ID!){ @@ -25,7 +20,7 @@ def test_batched_find_by_id } GRAPHQL - results = schema_multiplex([ + results = Schema.multiplex([ { query: query_string, variables: { "id1" => "1", "id2" => "2"} }, { query: query_string, variables: { "id1" => "1", "id2" => "3"}}, ])