/
setup.rb
54 lines (46 loc) · 1.33 KB
/
setup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module GraphQL::Batch
class Setup
class << self
def start_batching(executor_class)
GraphQL::Batch::Executor.start_batch(executor_class)
end
def end_batching
GraphQL::Batch::Executor.end_batch
end
def instrument_field(schema, type, field)
return field unless type == schema.mutation
old_resolve_proc = field.resolve_proc
field.redefine do
resolve ->(obj, args, ctx) {
GraphQL::Batch::Executor.current.clear
begin
::Promise.sync(old_resolve_proc.call(obj, args, ctx))
ensure
GraphQL::Batch::Executor.current.clear
end
}
end
end
def before_query(query)
warn "Deprecated graphql-batch setup `instrument(:query, GraphQL::Batch::Setup)`, replace with `use GraphQL::Batch`"
start_batching(GraphQL::Batch::Executor)
end
def after_query(query)
end_batching
end
end
def initialize(schema, executor_class:)
@schema = schema
@executor_class = executor_class
end
def before_query(query)
Setup.start_batching(@executor_class)
end
def after_query(query)
Setup.end_batching
end
def instrument(type, field)
Setup.instrument_field(@schema, type, field)
end
end
end