Skip to content

Commit

Permalink
Wrap BatchLoader for GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Jan 30, 2019
1 parent 5775e52 commit 1328fee
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ To avoid this problem, all we have to do is to change the resolver to return `Ba
PostType = GraphQL::ObjectType.define do
name "Post"
field :user, !UserType, resolve: ->(post, args, ctx) do
BatchLoader.for(post.user_id).batch do |user_ids, loader|
BatchLoader::GraphQL.for(post.user_id).batch do |user_ids, loader|
User.where(id: user_ids).each { |user| loader.call(user.id, user) }
end
end
Expand Down
30 changes: 13 additions & 17 deletions lib/batch_loader/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@

class BatchLoader
class GraphQL
class Wrapper
def initialize(batch_loader)
@batch_loader = batch_loader
end
def self.use(schema_definition)
schema_definition.lazy_resolve(BatchLoader::GraphQL, :sync)
end

def sync
@batch_loader.__sync
end
def self.for(item)
new(item)
end

def self.use(schema_definition)
schema_definition.lazy_resolve(BatchLoader::GraphQL::Wrapper, :sync)
schema_definition.instrument(:field, self)
def initialize(item)
@batch_loader = BatchLoader.for(item)
end

def self.instrument(type, field)
old_resolve_proc = field.resolve_proc
new_resolve_proc = ->(object, arguments, context) do
result = old_resolve_proc.call(object, arguments, context)
result.respond_to?(:__sync) ? BatchLoader::GraphQL::Wrapper.new(result) : result
end
def batch(*args, &block)
@batch_loader.batch(*args, &block)
self
end

field.redefine { resolve(new_resolve_proc) }
def sync
@batch_loader.__sync
end
end
end
16 changes: 5 additions & 11 deletions spec/fixtures/graphql_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

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

class PostType < GraphQL::Schema::Object
field :user, UserType, null: false
field :user_id, Int, null: false

def user
object.user_lazy
end

def user_id
BatchLoader.for(object).batch do |posts, loader|
posts.each { |p| loader.call(p, p.user_lazy.id) }
BatchLoader::GraphQL.for(object.user_id).batch do |user_ids, loader|
User.where(id: user_ids).each { |user| loader.call(user.id, user) }
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions spec/graphql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
{
posts {
user { id }
userId
}
}
QUERY
Expand All @@ -21,8 +20,8 @@

expect(result['data']).to eq({
'posts' => [
{'user' => {'id' => "1"}, "userId" => 1},
{'user' => {'id' => "2"}, "userId" => 2}
{'user' => {'id' => "1"}},
{'user' => {'id' => "2"}}
]
})
end
Expand Down

0 comments on commit 1328fee

Please sign in to comment.