Skip to content

Commit

Permalink
feat(GraphQL::Batch::SetupMultiplex) use multiplex instrumentation to…
Browse files Browse the repository at this point in the history
… share a batching context (#57)
  • Loading branch information
Robert Mosolgo authored and dylanahsmith committed Jul 24, 2017
1 parent bbe92c3 commit 69a14c5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/graphql/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
14 changes: 14 additions & 0 deletions lib/graphql/batch/setup_multiplex.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions test/multiplex_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 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

0 comments on commit 69a14c5

Please sign in to comment.