Skip to content

Commit

Permalink
Merge pull request #68 from Shopify/allow-custom-executor
Browse files Browse the repository at this point in the history
Allow custom executors
  • Loading branch information
chrisbutcher authored Nov 9, 2017
2 parents dd49492 + acf575c commit 09b136a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/graphql/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def self.batch
end
end

def self.use(schema_defn)
def self.use(schema_defn, executor_class: GraphQL::Batch::Executor)
schema = schema_defn.target
if GraphQL::VERSION >= "1.6.0"
instrumentation = GraphQL::Batch::SetupMultiplex.new(schema)
instrumentation = GraphQL::Batch::SetupMultiplex.new(schema, executor_class: executor_class)
schema_defn.instrument(:multiplex, instrumentation)
schema_defn.instrument(:field, instrumentation)
else
instrumentation = GraphQL::Batch::Setup.new(schema)
instrumentation = GraphQL::Batch::Setup.new(schema, executor_class: executor_class)
schema_defn.instrument(:query, instrumentation)
schema_defn.instrument(:field, instrumentation)
end
Expand Down
11 changes: 6 additions & 5 deletions lib/graphql/batch/setup.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module GraphQL::Batch
class Setup
class << self
def start_batching
def start_batching(executor_class)
raise NestedError if GraphQL::Batch::Executor.current
GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new
GraphQL::Batch::Executor.current = executor_class.new
end

def end_batching
Expand All @@ -27,20 +27,21 @@ def instrument_field(schema, type, field)

def before_query(query)
warn "Deprecated graphql-batch setup `instrument(:query, GraphQL::Batch::Setup)`, replace with `use GraphQL::Batch`"
start_batching
start_batching(GraphQL::Batch::Executor)
end

def after_query(query)
end_batching
end
end

def initialize(schema)
def initialize(schema, executor_class:)
@schema = schema
@executor_class = executor_class
end

def before_query(query)
Setup.start_batching
Setup.start_batching(@executor_class)
end

def after_query(query)
Expand Down
5 changes: 3 additions & 2 deletions lib/graphql/batch/setup_multiplex.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module GraphQL::Batch
class SetupMultiplex
def initialize(schema)
def initialize(schema, executor_class:)
@schema = schema
@executor_class = executor_class
end

def before_multiplex(multiplex)
Setup.start_batching
Setup.start_batching(@executor_class)
end

def after_multiplex(multiplex)
Expand Down
31 changes: 31 additions & 0 deletions test/custom_executor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative 'test_helper'

class GraphQL::Batch::CustomExecutorTest < Minitest::Test
class MyCustomExecutor < GraphQL::Batch::Executor
@@call_count = 0

def self.call_count
@@call_count
end

def around_promise_callbacks
@@call_count += 1

super
end
end

def test_custom_executor_class
schema = GraphQL::Schema.define do
query ::QueryType
mutation ::MutationType

use GraphQL::Batch, executor_class: MyCustomExecutor
end

query_string = '{ product(id: "1") { id } }'
schema.execute(query_string)

assert MyCustomExecutor.call_count > 0
end
end

0 comments on commit 09b136a

Please sign in to comment.