-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GraphQL v1.8.7 not compatible with Batch Loader #22
Comments
I can't reproduce it, my Gemfile.lock contains:
While running a query that would potentially cause a N+1 I got only two queries as expected:
Am I missing something? Can you post the code on how you are using batch loader? |
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem "graphql", '1.8.7'
gem "batch-loader"
gem 'pg'
gem 'rails', '5.2.1'
end
require 'pg'
require 'active_record'
require 'active_support'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "hacky", host: 'localhost')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
t.timestamps(null: false)
end
create_table :comments, force: true do |t|
t.string :title
t.string :comment
t.integer :post_id
t.timestamps(null: false)
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
# Data setup
post = Post.create!(title: 'AAAA1')
post.comments << Comment.create!(title: 'AAAA1')
post.comments << Comment.create!(title: 'AAAA2')
post.comments << Comment.create!(title: 'AAAA3')
post = Post.create!(title: 'BBBB1')
post.comments << Comment.create!(title: 'BBBB1')
post.comments << Comment.create!(title: 'BBBB2')
post.comments << Comment.create!(title: 'BBBB3')
class CommentType < GraphQL::Schema::Object
field :id, ID, null: false
field :title, String, null: false
end
class PostType < GraphQL::Schema::Object
field :id, ID, null: false
field :title, String, null: false
field :comments, [CommentType], null: true
def comments
BatchLoader.for(object.id).batch(default_value: []) do |post_ids, loader|
Comment.where(post_id: post_ids).each do |comment|
loader.call(comment.post_id) { |memo| memo << comment }
end
end
end
end
class QueryType < GraphQL::Schema::Object
field :posts, [PostType], null: false
def posts
Post.all
end
end
class Schema < GraphQL::Schema
query QueryType
use BatchLoader::GraphQL
end
query_string = <<~GRAPHQL
query posts {
posts {
comments {
id
title
}
}
}
GRAPHQL
context = {}
variables = {}
puts Schema.execute(query_string, context: context, variables: variables).to_json You can change the graphql version from Output for 1.8.6
Output for 1.8.7
|
It seems that the API changed of GraphQL Ruby and basically the gist is rmosolgo/graphql-ruby#1778 (comment) Any reason why |
@JanStevens I was able to reproduce it, thanks for the code example! That's sad that
It's not undefined, it's being proxied to the resolved value. For example: id = 1
result = BatchLoader.for(id).batch { |ids, loader| loader.call(ids.first, 1) }
result.class # => Fixnum
result = BatchLoader.for(id).batch { |ids, loader| loader.call(ids.first, nil) }
result.class # => NilClass |
There are some ideas on how to make graphql-ruby more flexible in terms of this "lazy" functionality. Maybe they'll be implemented one day. In a meantime, here is a quick fix: def comments
batch_loader = BatchLoader.for(object.id).batch(default_value: []) do |post_ids, loader|
Comment.where(post_id: post_ids).each do |comment|
loader.call(comment.post_id) { |memo| memo << comment }
end
end
BatchLoader::GraphQL::Wrapper.new(batch_loader) # it is done automatically with graphql-ruby 1.8.6
end Just wrap That way the new graphql-ruby version will be able to understand that it's a lazy object, before wrapping it into another lazy |
@rmosolgo is so crazy fast! He already started working on rmosolgo/graphql-ruby#1784 🚀 That potentially will allow DRYing the |
Has this been fixed on |
I added tests and set up a build matrix to test the latest GraphQL 1.8 and 1.7 versions in this PR #28. batch-loader/spec/graphql_spec.rb Lines 4 to 28 in a726994
It seems like it works fine with |
Using Batch Loader and GraphQL v1.8.6
Using GraphQL v1.8.7
Probably something has changed in GraphQL but not sure if BatchLoader should adapt or GraphQL has a bug. Related issue on GraphQL: rmosolgo/graphql-ruby#1778
Changelog: rmosolgo/graphql-ruby@v1.8.6...master
The text was updated successfully, but these errors were encountered: