Skip to content

Commit 1328fee

Browse files
committed
Wrap BatchLoader for GraphQL
1 parent 5775e52 commit 1328fee

File tree

4 files changed

+21
-32
lines changed

4 files changed

+21
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ To avoid this problem, all we have to do is to change the resolver to return `Ba
264264
PostType = GraphQL::ObjectType.define do
265265
name "Post"
266266
field :user, !UserType, resolve: ->(post, args, ctx) do
267-
BatchLoader.for(post.user_id).batch do |user_ids, loader|
267+
BatchLoader::GraphQL.for(post.user_id).batch do |user_ids, loader|
268268
User.where(id: user_ids).each { |user| loader.call(user.id, user) }
269269
end
270270
end

lib/batch_loader/graphql.rb

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22

33
class BatchLoader
44
class GraphQL
5-
class Wrapper
6-
def initialize(batch_loader)
7-
@batch_loader = batch_loader
8-
end
5+
def self.use(schema_definition)
6+
schema_definition.lazy_resolve(BatchLoader::GraphQL, :sync)
7+
end
98

10-
def sync
11-
@batch_loader.__sync
12-
end
9+
def self.for(item)
10+
new(item)
1311
end
1412

15-
def self.use(schema_definition)
16-
schema_definition.lazy_resolve(BatchLoader::GraphQL::Wrapper, :sync)
17-
schema_definition.instrument(:field, self)
13+
def initialize(item)
14+
@batch_loader = BatchLoader.for(item)
1815
end
1916

20-
def self.instrument(type, field)
21-
old_resolve_proc = field.resolve_proc
22-
new_resolve_proc = ->(object, arguments, context) do
23-
result = old_resolve_proc.call(object, arguments, context)
24-
result.respond_to?(:__sync) ? BatchLoader::GraphQL::Wrapper.new(result) : result
25-
end
17+
def batch(*args, &block)
18+
@batch_loader.batch(*args, &block)
19+
self
20+
end
2621

27-
field.redefine { resolve(new_resolve_proc) }
22+
def sync
23+
@batch_loader.__sync
2824
end
2925
end
3026
end

spec/fixtures/graphql_schema.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
PostType = GraphQL::ObjectType.define do
99
name "Post"
10-
field :user, !UserType, resolve: ->(object, args, ctx) { object.user_lazy }
11-
field :userId, !types.Int, resolve: ->(object, args, ctx) do
12-
BatchLoader.for(object).batch do |posts, loader|
13-
posts.each { |p| loader.call(p, p.user_lazy.id) }
10+
field :user, !UserType, resolve: ->(object, args, ctx) do
11+
BatchLoader::GraphQL.for(object.user_id).batch do |user_ids, loader|
12+
User.where(id: user_ids).each { |user| loader.call(user.id, user) }
1413
end
1514
end
1615
end
@@ -31,15 +30,10 @@ class UserType < GraphQL::Schema::Object
3130

3231
class PostType < GraphQL::Schema::Object
3332
field :user, UserType, null: false
34-
field :user_id, Int, null: false
3533

3634
def user
37-
object.user_lazy
38-
end
39-
40-
def user_id
41-
BatchLoader.for(object).batch do |posts, loader|
42-
posts.each { |p| loader.call(p, p.user_lazy.id) }
35+
BatchLoader::GraphQL.for(object.user_id).batch do |user_ids, loader|
36+
User.where(id: user_ids).each { |user| loader.call(user.id, user) }
4337
end
4438
end
4539
end

spec/graphql_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
{
1111
posts {
1212
user { id }
13-
userId
1413
}
1514
}
1615
QUERY
@@ -21,8 +20,8 @@
2120

2221
expect(result['data']).to eq({
2322
'posts' => [
24-
{'user' => {'id' => "1"}, "userId" => 1},
25-
{'user' => {'id' => "2"}, "userId" => 2}
23+
{'user' => {'id' => "1"}},
24+
{'user' => {'id' => "2"}}
2625
]
2726
})
2827
end

0 commit comments

Comments
 (0)