diff --git a/README.md b/README.md index c75e023..4748cb3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/batch_loader/graphql.rb b/lib/batch_loader/graphql.rb index 5cba1df..a5b3300 100644 --- a/lib/batch_loader/graphql.rb +++ b/lib/batch_loader/graphql.rb @@ -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 diff --git a/spec/fixtures/graphql_schema.rb b/spec/fixtures/graphql_schema.rb index 7940b37..284b110 100644 --- a/spec/fixtures/graphql_schema.rb +++ b/spec/fixtures/graphql_schema.rb @@ -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 @@ -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 diff --git a/spec/graphql_spec.rb b/spec/graphql_spec.rb index 0e9514a..ef92b82 100644 --- a/spec/graphql_spec.rb +++ b/spec/graphql_spec.rb @@ -10,7 +10,6 @@ { posts { user { id } - userId } } QUERY @@ -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